Home > Net >  C Weird File Line Read
C Weird File Line Read

Time:11-29

I'm new to C programming and trying to figure out a weird line read behavior when reading a line from a text file. For this specific program, I have to wait for the user to press enter before reading the next line.

If I hard code the file name, the file read starts at line 1 as expected:

#include <iostream>
#include <fstream>
using namespace std;

int main(void) {
   ifstream in_file;
   in_file.open("test.txt");

   // read line by line
   string line;

   while (getline(in_file, line)) {
      cout << line;
      cin.get();
   }

   in_file.close();
   return 0;
}

I compile with g -Wall -std=c 14 test1.cpp -o test1 and get:

$ ./test
This is line one.
**user presses enter**
This is line two.
**user presses enter**
This is line three.
etc. etc.

But when I add in the option to have the user type in a file name, the line read starts at line 2:

#include <iostream>
#include <fstream>
using namespace std;

int main(void) {
   string filename;
   cin >> filename;   

   ifstream in_file;
   in_file.open(filename);

   // read line by line
   string line;

   while (getline(in_file, line)) {
      cout << line;
      cin.get();
   }

   in_file.close();
   return 0;
}

The same compile command gives me:

$ ./test2
test.txt
This is line two.
**user presses enter**
This is line three.
**user presses enter**
This is line four.
etc. etc.

Am I missing something here? I have no idea why it starts reading at line 2 when I add in the code to specify a file name. Am I not finishing the cin statement properly or something?

Thanks!

CodePudding user response:

by default cin operator>> reads data up to the first whitespace characte and whitespace characte is not extracted reference. So if you read file name like this cin>>file; file variable will contains only first part of your string without whitespace. So that when reading you do not have such problems use getline

#include <iostream>
#include <fstream>
#include <sstream>

using namespace std;

int main(void) {
    string filename;

    getline(cin, filename, '\n');

    ifstream in_file;
    in_file.open(filename);

    // read line by line
    string line;

    while (getline(in_file, line)) {
        cout << line;
        cin.get();
    }

    in_file.close();
    return 0;
}

CodePudding user response:

This implementation of the code should work. You just needed to add cin.ignore() to ignore the remaining characters on the line until you hit either the end of the line(EOL) or the end of the file(EOF). The function also takes in 2 parameters, which are the maximum number of characters to ignore and the character to ignore. link to the use of cin.ignore(). Hope that this helps :)

#include <iostream>//basic
#include <fstream>//file

using namespace std;

int main(){
    //set file name
    string file="";
    cout<<"file name: ";
    cin>>file;

    //create/write to file
    ofstream out_file;
    out_file.open(file);
    out_file<<"test 1\ntest 2\ntest 3";
    out_file.close();

    //read file
    ifstream in_file;
    in_file.open(file);

    string line;

    cin.ignore();//clear buffer

    while(getline(in_file,line)){
        cout<<line;
        cin.get();
    }

    in_file.close();

    system("pause");

    return 0;
}
  •  Tags:  
  • c
  • Related