Question

I am very new to C++ i have this code which is supposed to print the text file in Task1 but it displays nothing and it gives me no output. and the text file contains "1 2 3 4" for an example

#include <iostream>
#include <fstream>
#include <string>
using namespace std;
void main()
{
    string line ;
    std::fstream myfile("D:\\Task1.txt", std::ios_base::in);
    getline( myfile, line );
    //cout<<line.length();
    while( getline( myfile, line ) )
    {
        for (int i=0; i < line.length(); i++)
        {
            cout<<line[i];
            //if (line[i] ...) // look at each character and process it accordingly
        }
    }

    getchar();
}

how can i fix this?

Was it helpful?

Solution

There is a simple issue with your code:

#include <iostream>
#include <fstream>
#include <string>
using namespace std;
void main()
{
    string line ;
    std::fstream myfile("D:\\Task1.txt", std::ios_base::in);
    getline( myfile, line ); // That's the problematic line!
    //cout<<line.length();
    while( getline( myfile, line ) ) // first line lost here
    ...

You read the first (and presumably only line) of the file and simply discard it, because you enter the while loop immediately afterwards.

Fix: Remove the line getline( myfile, line ); that precedes the while loop.

You should also check that the file actually exists (there may be a typo in the filename!):

std::fstream myfile("D:\\Task1.txt", std::ios_base::in);
if ( !myfile )
{
   std::cerr << "File does not exist!\n";
   return 1;
}

If you aren't planning to use the fstream for output as well, just replace

std::fstream myfile("D:\\Task1.txt", std::ios_base::in);

by

std::ifstream myfile("D:\\Task1.txt");

OTHER TIPS

You are ignoring first line by reading and do not outputting it.

string line ;
ifstream myfile("D:\\Task1.txt");

while(getline(myfile, line))
{
    cout << line << endl;
}

Extra:

  • You don't need to use std namespace if you are using "using namespace std".
  • You can use ifstream to read-only.

Better You use ifstream for reading. Check always an ifstream, whether the open was successful or not.

std::ifstream myfile("D:\\Task1.txt");
if( !myfile.is_open() ) {
    cerr << "error open file\n";
    // return or break
}

If You want to read numbers, so read numbers

for( int number; myfile >> number; ) {
    cout << number << endl;
}

you need to flush the cout if you want the output printed. Change this line:

cout<<line[i] << std::endl;

and you will have the chars printed in a column

How many lines you have in text file? You call getline twice before you start printing output. If you have only one line you'll get empty string ;)

Also I don't think your while condition is proper. Getline won't return anything useful anyway (it returns istream). If it approach end of file it'll simply rise error flag.

You should do it like that:

while (! myfile.eof() )
{
  // do some reading & printing
}

Also, please, remember to close your file via myfile.close().

Hope it helps.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top