Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <string>
- using namespace std;
- void read_by_char(string filename) {
- ifstream fin;
- fin.open(filename);
- if (fin.is_open()) {
- char ch;
- while (!fin.eof()) {
- ch = NULL;
- fin.get(ch);
- cout << ch;
- }
- fin.close();
- }
- else {
- cout << "Can not open the file\n";
- }
- }
- void read_by_line(string filename) {
- ifstream fin;
- fin.open(filename);
- if (fin.is_open()) {
- string str;
- while (!fin.eof()) {
- str = "";
- getline(fin, str);
- //fin.getline(); это не удобно использовать
- cout << str;
- }
- fin.close();
- }
- else {
- cout << "Can not open the file\n";
- }
- }
- void write_to_file(string filename) {
- ofstream fout;
- fout.open(filename);
- if (fout.is_open()) {
- int x = 0;
- while (x <= 10)
- fout << x++ << " ";
- fout.close();
- cout << "File ready\n";
- } else {
- cout << "Error with the file\n";
- }
- }
- void read_from_file(string filename) {
- ifstream fin;
- fin.open(filename);
- if (fin.is_open()) {
- string x;
- int res = 0;
- while (!fin.eof()) {
- fin >> x;
- if((x[0] > '0') && (x[0] < '9'))
- res += stoi(x);
- }
- fin.close();
- cout << "File ready\n" << "Res: " << res << endl;
- }
- else {
- cout << "Error with the file\n";
- }
- }
- void main() {
- setlocale(LC_ALL, "Russian");
- read_from_file("out.txt");
- //write_to_file("out.txt");
- /*string filename = "my_file.txt";
- //read_by_char(filename);
- string filename2 = "x64/Release/my_file_for_write1.txt";
- ofstream fout; // cout
- fout.open(filename2, ios_base::app);
- int x = 14;
- if (fout.is_open()) {
- fout << "Hello all\n Кирилица Это Была" << endl;
- fout << x << endl;
- fout.close();
- }
- else {
- cout << "Error with the file\n";
- }
- */
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement