Reading from a text file

Hi! I'm trying to read from a file but I'm doing something wrong. If someone can help me please.

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
   #include <iostream>
#include <fstream>
using namespace std;
int main(){
	int number;
	ofstream uDat;
	uDat.open("numbers.txt");
	ofstream flow("numbers.txt",ios::app);
	cout<<"Enter 10 integer numbers:"<<endl;
	
	for(int i=0; i<10; i++){
		cout<<"Number "<<i+1<<":";
		cin>>number;
		flow<<number<<endl;
	}
	uDat.close();
	ifstream oDat;
	oDat.open("numbers.txt");
	ifstream flow2 ("numbers.txt");
	for(int i=0; i<10; i++){
		flow2>>number>>endl;
		if(broj%3==0) cout<<"Number "<<number<<" is divisible by 3."<<endl;
	
	}
	oDat.close();
	return 0;
}
So what does 'something wrong' mean?

Notice that you open the file for input/output twice.
Hello Trixypu,

Welcome to the forum.

First you should make sure that the program compiles before you post it. The variable "broj" is not defined and I am not sure what it should be.

At the begining of main you open "uDat" for output then "flow" for out, but you never use "uDat" before you close it.

On line 17 you open "oDat" for input. the "o" makes the variable name misleading because it is for input and the "o" would lead you to believe that the variable is to be used for output.

You are opening files that you do not need and also not using these files.

Here is your program that does work:

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
#include <iostream>
#include <fstream>

//using namespace std;  // <--- Best not to use.

int main()
{
	int number;
	//ofstream uDat;
	//uDat.open("numbers.txt");
	std::ofstream flow("numbers.txt", std::ios::app);

	std::cout << "Enter 10 integer numbers:" << std::endl;

	for (int i = 0; i<10; i++) {
		std::cout << "Number " << i + 1 << ":";
		std::cin >> number;
		flow << number << ' ';
	}

	flow << std::endl;

	flow.close();

	std::ifstream inFile("numbers.txt");

	//oDat.open("numbers.txt");

	//ifstream flow2("numbers.txt");

	for (int i = 0; i < 10; i++)
	{
		inFile >> number;
		if (number % 3 == 0) std::cout << "Number " << number << " is divisible by 3." << std::endl;
	}

	inFile.close();

	return 0;
}


I did not do it here, but when you open a file for input you need to chck that the file did open.

Any questions just let me know.

Hope that helps,

Andy
Thanks! Sorry, I forgot to translate that "broj" variable.
Hello Trixypu,

That alright I replaced it with "number" and it worked fine.

When I was first learning about using files I found this code useful:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
	std::string iFileName{ "" };  // <--- Insert file name between the "".

	std::ifstream inFile;

	inFile.open(iFileName);

	if (inFile.is_open())
	{
		std::cout << "\n File " << iFileName << " is open" << std::endl;
		std::this_thread::sleep_for(std::chrono::seconds(2));  // <--- Needs header files chrono" and "thread".
	}
	else
	{
		std::cout << "\n File " << iFileName << " did not open" << std::endl;
		std::this_thread::sleep_for(std::chrono::seconds(3));  // <--- Needs header files chrono" and "thread".
		exit(1);
	}


This can be shortened when you have a better understanding of using files like what you have done with std::ofstream flow("numbers.txt", std::ios::app);. This creates the file stream object and opens the file at the same time and is the preferred way of doing this. The if/else statement checks to make sure the files is open which should be done for input files. This i not as necessary for output files because if the file does not exist it is created, so there is less of a chance the an output will not be opened.

As noted in my code the use of using namespace std; is a bad idea and WILL get you in trouble some day. It is better to learn early what is in the "std" namespace and to qualify these names with "std::" like "std::cout", "std::cin" and "std::endl". A good IDE will help you with this.

Your program opens two output files in two different ways. Each way will work, but one of the file streams is never used.

For variable names associated "ifstream"s and "ofstream"s I like to use inFile" and "outFile". Some day when you have a program that requires more than one input or output file stream you could just add to inFile" and "outFile" something that is descriptive for the file it is using. This will make the variable name longer, but it is better than just adding a 2 or 3 to the end of the variable name.

One last note, when you write for (int i = 0; i<10; i++) { this is fine because the compiler does not care where the opening { is placed or about white space. But for reading the code this is much easier to read:

1
2
3
4
for (int i = 0; i<10; i++)
{
    // Some code here.
}

When the {}s line up in the same column it is not only easier to read, but easier to find when when a closing } is missing.

I need to give you a commendation on the first for loop as it is well written and presents the input to the user in a good way.

Hope that helps,

Andy
Thanks Handy Andy! :)
Hello Trixypu,

You are welcome.

If you have your answer be sure to put a green check on the post so everyone knows that you are finished.

Andy
Topic archived. No new replies allowed.