Read and display a text file into c++

Apr 13, 2013 at 11:07pm
Hey I'm a beginner at C++ and I'm trying to figure out how to read and display the content of a text file in my console application using C++. The file contains 25 lines and 3 columns. The first column is "Rank" (numerical values from 1 to 25) the second column is "Name" (25 teams name) and the third column "value" has the amount of point each team is worth. If someone could show me the code for reading and displaying all this information I would greatly appreciate it
Apr 14, 2013 at 1:15am
flemongo wrote:
If someone could show me the code for reading and displaying all this information I would greatly appreciate it
We won't write code for you, but we will help you write the code yourself.

How much have you written so far?
Apr 14, 2013 at 8:27am
Check out the tutorials for file handling on various sites.

One of the best sites I know is: http://www.learncpp.com

Also try Googling for various alternative methods.

Hope you find this useful.
Apr 14, 2013 at 1:56pm
The following block of code will read each line of a text file and write it to the console. You will need to figure out how to tokenize each line within the while loop.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
fstream fIn;
fIn.open( "c:\\SomeFile.txt", ios::in );

if( fIn.is_open() )
{
  string s;
  while( getline( fIn, s ) )
  {
    cout << s << endl;
    // Tokenize s here into columns (probably on spaces)
  }
  fIn.close();
}
else
  cout << "Error opening file " << errno << endl;
Apr 14, 2013 at 4:32pm
I have tried many different things I saw on google. Like using the getline with an ifstream or even displaying the entire file without reading each line, but it doesn't work. Apparently there's a problem because two of the columns are integer while the other is a string.when I try to build it, the output box says something like conversion impossible.
Topic archived. No new replies allowed.