Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- int main()
- {
- std::ofstream outf;
- //std::ios::app - append to file
- //std::ios::ate - at the end - go to the end of file
- //std::ios::binary - open file in binary mode
- //std::ios::in - open file to read - input mode default for ifstream
- //std::ios::out - open file for writing - output mode default for ofstream
- //std::ios::trunc - delete file if exist
- outf.open("file.txt");
- if (!outf) { std::cout << "IO ERROR! cannot open file!\n"; return 1; };
- //outf << "Hello, from C++ file io app!\n";
- //outf << "One more line of text saved in file\n";
- //outf << "Last line! Thats all for now!\n";
- //int num{ 100500 };
- //for (int i{0}; i < 4; ++i)
- //{
- // std::cout << (int)*((uint8_t*)(&num + i)) << '\n';
- //};
- float val{};
- for (int i{0}; i < 10; ++i)
- {
- val = ((float)i / (rand() % 100 + 1));
- std::cout << "Line number: ";
- std::cout << i << '\n';
- //<< val << '\n';
- outf << "Line number: ";
- outf << i << '\n';
- //' ' << val << '\n';
- }
- outf.close();
- std::fstream iof;
- iof.open("file.txt", std::ios::in | std::ios::out | std::ios::ate);
- if (!iof) { std::cout << "IO ERROR! cannot open file!\n"; return 1; };
- std::cout << "\nTry to read file content: \n";
- char buf[200];
- int numI{};
- float numF{};
- // seekg(pos, rel) - move read (get) position
- // seekp(pos. rel) - move write (put) position
- // tellg() - say read (get) position
- // tellp() - say write (put) position
- // REl
- // std::ios:beg
- // std::ios:end
- // std::ios:cur
- std::cout << "\nRead pos: " << iof.tellg() << "\nWrite pos: " << iof.tellp() << '\n';
- iof.seekp(-(iof.tellp()), std::ios::end);
- iof << "Some line of text! :)";
- iof.seekg(0, std::ios::beg);
- std::cout << "\nRead pos: " << iof.tellg() << "\nWrite pos: " << iof.tellp() << '\n';
- while (!iof.eof())
- {
- iof.getline(buf,200);
- std::cout << buf << '\n';
- //inf >> numI;
- //if (inf.fail()) break;
- //inf >> numF;
- //if (inf.fail()) break;
- //std::cout << buf << ' ';
- //std::cout << numI << ' ' << numF << '\n';
- //inf.ignore(100, '\n');
- }
- iof.close();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement