Data files

Write a program that counts how many values are in a file.

I have a text file called "data5c.txt" which is basically contains a unknown number of random numbers. I have no idea on how data files work so can somebody please help me on how I can display- how many numbers there are in the file.
The only thing I know about data files is that I have to include <fstream> and that I have to declare my ifstream with whatever I want, so for example
ifstream fin; (input). I'm also very new to C++ so help would be greatly appreciated.
Last edited on
Read this tutorial to get a better understanding of this topic: http://www.cplusplus.com/doc/tutorial/files/


Basically you have to:
(1) Declare the file using ifstream.
(2) Open the file. Ideally you should also make sure the file is open using the if_open function.
(2) Read the file using a while loop with the "End of File" input control method.
(3) Cout what you've read using another loop

You'll understand all of this if you read the tutorial. If after reading the tutorial you still have questions, we'd be happy to help you.

Last edited on
Ok I believe I did this the correct way assuming my data5c is saved in the right place. Is this the correct setup for me to output the total numbers in my file. Also assuming this is the right setup how can I now output the total numbers in the file?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main()
{
  int n=0;
  double temp;
  ifstream fin;
  while (!fin.eof())
  {
      fin>>temp;
      n++;
  }
  fin.close();
  fin.open("data5c.txt");


}
This is not correct.

Firstly, the variable n is useless. You are using a while loop, hence an incrementing variable is unnecessary. Get rid of it completely.

Secondly, you are reading from the file on line 14 and opening it on line 18. How are you reading before opening? Open the file first, THEN read. Also, two reads will be required: one before the while test (this will read the first number), and one within the while loop (this will read all subsequent numbers as long as it passes the test).

Thirdly, after opening the file, it is good programming practice to check that it is actually open. Hence, after the open statement, insert this into your code:

1
2
3
4
5
if(!myfile.is_open())
{
    cout << "FILE NOT OPENED!";
    return 0;
}


Its a simple statement that will output "FILE NOT OPENED" to the screen and end the program if your file did not open successfully. If it did open successfully, the entire if-statement will be ignored and it will carry on with the program.

Fourthly, cout temp within the while loop. Thats the easiest way to print out your file onto the screen

Fifthly, the test while(!fin.eof()) will actually NOT cout your entire file. It will cout everything except for the last piece of information in the file. Say your file consisted of a list of numbers 1-10. Your code using this while test would only print the numbers 1-9. Why is this? Simply trace the program and you'll understand.

If you want to cout your entire file, simply insert the name of your file (just the file name and nothing else) in your while test. So it would be while(fin).

Sixthly, props on remembering to close the file at the end. I always forget to do that :)

Last edited on
Ok I think my first problem right now is that I tried using the test you displayed above to test if my file opened correctly or not, it didn't seem to do so.

I don't understand why, I though I had saved the txt file in the right place. I'm using codeblocks but my professor told the class we were suppose to save the text file inside our project folder(He was referring to the Microsoft Visual users). There is no projects folder in codeblocks. I saved my file as the following:

documents>Test(name of my project)>bin>debug>(inside the debug folder I saved my text file)

This is the code I just ran to test whether the file opened or not. It failed.

1
2
3
4
5
6
7
8
9
10
11
12
int main()
{
  ifstream fin;
  fin.open("data5c.txt");
  if(!fin.is_open())
  {
      cout<<"File NOT OPENED!";
      return 0;
  }


}
Update: I just realized I had been saving the txt file in the wrong place now when I run the test above the compiler directs no message relaying that my file had opened correctly.

Wouldn't I need the n value to count up the total amount of numbers in my file?

I've fixed some of the corrections you've pointed out so now it looks like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main()
{
  ifstream fin;
  double temp;
  fin.open("data.txt");
  fin>>temp;
  while(fin) // Is this what you meant I should write so it would run all numbers in the file?
  {
      fin>>temp;
      cout<<temp<<endl;
  }
  fin.close();

}


Running this code now seems to be displaying all the numbers in my file. How would I now find how many numbers total there are in the file?
Last edited on
Quick update: I've re added the n value back into the program and ran as such:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
int main()
{
  ifstream fin;
  int n=0;
  double temp;
  fin.open("data.txt");
  fin>>temp;
  while(fin)
  {
      fin>>temp;
      n++;
      cout<<temp<<endl;
  }
  cout<<"Total:"<<n<<endl;
  fin.close();

}


The total I cout seems to be giving me a reasonable number so I believe this is the total amount of values in my file, is that right?
Sorry, I just thought you wanted to print the values in your file, not the number of values. If you want to also print the number of values, then yes, n value will be needed.

Just one tiny adjustment: Move the second read (the one within the while loop) to immediately before the closing brace of the while loop. The second read has to be at the end of the while loop, not in the beginning like you have it. What you are actually doing is skipping count of the first value in your file by reading the file twice in a row.

Fix that, and im sure you are good to go.
No problem, I probably didn't phrase my question right. I've fixed the adjustment you mentioned and put the second read right before the brace.

I've just got one more question. I have to dynamically create an array and put all those values of the file into the array and then sort it. I've just tried doing this now but not sure how to go about it.

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
int main()
{
  ifstream fin;
  int n=0;
  double temp;
  fin.open("data.txt");
  fin>>temp;
  while(fin)
  {
      n++;
      cout<<temp<<endl;
      fin>>temp;
  }
  cout<<"Total:"<<n<<endl;
  fin.close();
  fin.open("data.txt"); // I believe I have to open the file again before doing a new task?
  double *A;
  A=new double[n];
  for (int i=0;i<n;i++)
  {
      fin A[i]; // Getting an error here I'm not sure how to go about this? 
  }
  cout<<A[i]<<endl;
  fin.close();


}
Topic archived. No new replies allowed.