-3

I want to create program for delaying subtitles and when I run this code below is not reading from first line. It strat like of middle of file.

#include "stdafx.h"
#include "iostream"
#include "cstdlib"
#include "fstream"
#include "string"

using namespace std;

int main(int argc, char *argv[]){
     ifstream input; //input

     char input_file[32]; //names of input and output


      cout << "Enter name of input fille: "; ///user gives names of input
      cin >> input_file;

input.open(input_file);
if (!input.good()){
    cout << "File " << input_file << " dosen't exist." << endl;
    return 1;
}


string row;
while (!input.eof()){    
    getline(input, row);

    cout << row << endl;
}

system("pause");
return 0;
}
user3127680
  • 343
  • 2
  • 4
  • 12

2 Answers2

0

First, eof() should not be placed in a while(), see here for details. So change

while (!input.eof()){    
    getline(input, row);
    cout << row << endl;
}

to

while (getline(input, row)){       
    cout << row << endl;
}

and see how it goes. It's really weird reading from the middle of the file. Maybe you can further share the content of the file to us to better fix the problem.

Community
  • 1
  • 1
herohuyongtao
  • 45,575
  • 23
  • 118
  • 159
0

You still can set the position to the beginning manually with input.seekg(0, input.beg) before reading anything.

Lucas8
  • 11
  • 1