Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <fstream>
- #include <vector>
- //This program demonstrates using ifstream and ostream objects.
- //It opens a file with purely numeric input, separated by spaces, then
- //loads them into a vector, increments them, and overwrites the file
- //with new data formatted into three columns.
- int main()
- {
- std::ifstream readFile("Text.txt");
- std::vector<int> vec;
- int temp;
- while(readFile >> temp)
- {
- vec.push_back(temp);
- };
- readFile.close();
- for (auto& i : vec)
- ++i;
- std::ofstream writeFile("Text.txt"); //overwrites contents!
- for(unsigned i = 0; i < vec.size(); ++i)
- {
- writeFile << vec[i] << ' ';
- if ((i + 1) % 3 == 0)
- writeFile << '\n';
- }
- writeFile.close();
- }
- /* Contents of "Text.txt":
- 0 0 0
- 1 1 1
- 2 2 2
- 3 3 3
- 4 4 4
- */
Add Comment
Please, Sign In to add comment