// Copy one file to another, a line at a time
#include <string>
#include <fstream>
using std::string;
using std::ifstream;
using std::ofstream;
int main()
{
ifstream in("Scopy.cpp"); // Open for reading
ofstream out("Scopy2.cpp"); // Open for writing
string s;
while(getline(in, s)) // Discards newline char
{out << s << "\n";} // must add it back
}
/*
g++ Scopy.cpp -o Scopy
./Scopy
g++ Scopy2.cpp -o Scopy2
./Scopy2
rm Scopy2* // clean
*/
You may compile and run Scopy2.cpp, with the same result (Scopy2.cpp will be overwritten with exactly the same content):
g++ Scopy2.cpp -o Scopy2
./Scopy2
At the end we remove the created files Scopy2.cpp and Scopy2:
rm Scopy2*
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
// Read an entire file into a single string
#include <iostream>
#include <string>
#include <fstream>
using std::cout;
using std::string;
using std::ifstream;
using std::ofstream;
int main()
{
ifstream in("FillString.cpp");
string s, line;
while(getline(in, line)) // getline() removes '\n'
{s += line + '\n';} // char '\n' is converted into string "\n"
cout << s;
}
/*
g++ FillString.cpp -o FillString
./FillString
*/
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
// Copy an entire file into a vector of string
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
using std::cout;
using std::endl;
using std::string;
using std::vector;
using std::ifstream;
int main()
{
vector<string> v;
ifstream in("Fillvector.cpp");
string line;
while(getline(in, line)) // getline() removes newline char
{v.push_back(line);} // Add the line to the end
int size = v.size();
// Add line numbers:
for(int i = 0; i < size; i++)
{cout << i+1 << ": " << v[i] << endl;} // first line is line 1, not 0
}
/*
g++ Fillvector.cpp -o Fillvector
./Fillvector
*/
We print the line numbers starting at 1, so we write cout << i+1 instead of cout << i .
The function call v.size() is expensive inside a loop, so we store the size of v in an int variable and then refer to this variable inside the loop:
int size = v.size();
for(int i = 0; i < size; i++)
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
// Break a file into whitespace-separated words
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
using std::cout;
using std::endl;
using std::string;
using std::vector;
using std::ifstream;
int main()
{
vector<string> words;
ifstream in("GetWords.cpp");
string word;
while(in >> word)
{words.push_back(word);}
int size = words.size();
for(int i = 0; i < size; i++)
{cout << words[i] << endl;} // one word per line
}
/*
g++ GetWords.cpp -o GetWords
./GetWords
*/
for (string word : words) // for all strings in vector<string> words
{cout << word << endl;} // one word per line
In this case, we no longer need the size variable:
int size = words.size();
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
Exercise 2-3. Create a program that opens a file and counts the whitespace-separated words in that file.
// Counts the whitespace-separated words in the file
#include <iostream>
#include <string>
#include <fstream>
using std::cout;
using std::endl;
using std::string;
using std::ifstream;
int main()
{
int words = 0;
ifstream in("CountWords.cpp");
string word;
while(in >> word)
{words++;}
cout << "Number of whitespace-separated words: "
<< words << endl;
}
/*
g++ CountWords.cpp -o CountWords
./CountWords
Number of whitespace-separated words: 61
*/
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
Exercise 2-4. Create a program that counts the occurrence of a particular word in a file (use the string class' operator '==' to find the word).
// Counts the occurrences of a whitespace-separated word in the file
#include <iostream>
#include <string>
#include <fstream>
using std::cout;
using std::cin;
using std::endl;
using std::string;
using std::ifstream;
int main()
{
int occurrences = 0;
string word, lookfor;
cout << "Word to look for in the file: ";
cin >> lookfor;
ifstream in("CountWord.cpp");
while(in >> word)
{
if (word == lookfor)
{occurrences++;}
}
cout << "Number of occurrences of '" << lookfor
<< "': " << occurrences << endl;
}
/*
g++ CountWord.cpp -o CountWord
./CountWord
Word to look for in the file: word
Number of occurrences of 'word': 2
./CountWord
Word to look for in the file: word,
Number of occurrences of 'word,': 2
./CountWord
Word to look for in the file: word)
Number of occurrences of 'word)': 2
./CountWord
Word to look for in the file: (word
Number of occurrences of '(word': 2
./CountWord
Word to look for in the file: ==
Number of occurrences of '==': 2
./CountWord
Word to look for in the file: >>
Number of occurrences of '>>': 3
./CountWord
Word to look for in the file: <<
Number of occurrences of '<<': 7
./CountWord
Word to look for in the file: occurrences
Number of occurrences of 'occurrences': 13
*/
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
Exercise 2-5. Change Fillvector.cpp so that it prints the lines (backwards) from last to first.
// Copy an entire file into a vector of string (in reverse order of lines)
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
using std::cout;
using std::endl;
using std::string;
using std::vector;
using std::ifstream;
int main()
{
vector<string> v;
ifstream in("RevFillvector.cpp");
string line;
while(getline(in, line))
{v.push_back(line);} // Add line to the end, preserving line order
// Add line numbers and print in reverse, from last to first line:
for(int i = v.size()-1; i >= 0; i--)
{cout << i+1 << ": " << v[i] << endl;}
}
/*
g++ RevFillvector.cpp -o RevFillvector
./RevFillvector
*/
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
Exercise 2-6. Change Fillvector.cpp so that it concatenates all the elements in the vector into a single string before printing it out, but don’t try to add line numbering.
// Copy an entire file into a vector of string, concatenate strings
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
using std::cout;
using std::string;
using std::vector;
using std::ifstream;
int main()
{
vector<string> v;
ifstream in("CatFillvector.cpp");
string line;
while(getline(in, line))
{v.push_back(line);} // Add the line to the end
line = ""; // reset line
int size = v.size();
// Concatenation:
for(int i = 0; i < size; i++)
{line += v[i] + "\n";}
cout << line;
}
/*
g++ CatFillvector.cpp -o CatFillvector
./CatFillvector
*/
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
Exercise 2-7. Display a file a line at a time, waiting for the user to press the "Enter" key after each line.
// Displays the file one line at a time. Press Enter to continue.
// Press Enter after each line is displayed.
#include <cstdio> // for getchar()
#include <string>
#include <iostream>
#include <fstream>
using std::cout;
using std::cin;
using std::string;
using std::ifstream;
int main()
{
char c;
ifstream in("DisplayFile.cpp");
string line;
while(getline(in, line))
{
cout << line; // no newline here
cin.ignore(); // getchar(); // Press Enter to continue
}
}
/*
g++ DisplayFile.cpp -o DisplayFile
./DisplayFile
*/
You should only hit ENTER (not other key) for the file display to work as intended. Each line is displayed without newline at the end, whose place it taken by hitting ENTER.
User input/output is normally buffered and the buffer is cleared after hitting ENTER (or printing newline). As such, if you hit the space bar two times and then ENTER, three lines will then be displayed (with no newlines ending the first two of them).