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:\n";
- outf << i << ' ' << val << '\n';
- }
- outf.close();
- std::ifstream inf;
- inf.open("file.txt");
- if (!inf) { 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{};
- while (!inf.eof())
- {
- inf.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');
- }
- inf.close();
- }
Advertisement
Add Comment
Please, Sign In to add comment