summaryrefslogtreecommitdiff
path: root/CPP/Basics/files.cpp
blob: 12422246d6895cffea9a4a55a3ac85ecf7f09e80 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include <iostream>
#include <string>
#include <fstream>

using namespace std;

void read_from_file()
{
	ifstream myFile;
	string str, str2;
	int number;
	char letter;

	myFile.open("people.txt");

	if (myFile.fail()) {
		cout << endl << "File not found!" << endl;
	} else {
		while (!myFile.eof()) {
			getline(myFile, str);
			cout << str << ", ";
			getline(myFile, str);
			number = stoi(str);
			cout << number << ", ";
			getline(myFile, str);
			cout << str << endl;
		}
		myFile.close();
	}
}

void write_to_file()
{
	ofstream outFile;
	float a = 4.333f;
	float b = 5.302f;

	outFile.open("calc.txt");
	if (outFile.fail()) {
		cout << endl << "Couldn't open the file!" << endl;
	} else {
		outFile << "Ronaldo" << endl;
		outFile << "25" << endl;
		outFile << "J" << endl;
		outFile << "Cretino" << endl;
		outFile << "23" << endl;
		outFile << "C" << endl;
		outFile << "Mano dos Pano" << endl;
		outFile << "23" << endl;
		outFile << "M" << endl;
		outFile.close();
		cout << "File written successfully!" << endl;

	}
}
int main(void)
{

	write_to_file();
	read_from_file();

	// Reading a whole line from the terminal
//	getline(cin, str2);
//	cout << str2 << endl;
	return 0;
}