0

What i want to do is infile a line of chars then perform operations on it save it to an out file location then read the next line and do the same thing.

//seperate function called within int main.
//expression is passed by reference to this function.

ifstream infile;
int i = 0;

infile.open("test.data");

if(infile.fail()){
  cout << "ERROR: File failed to open." << endl;
  exit(1);
}

while(!infile.eof()){
  infile >> expression[i];
  while(expression[i +1] != '\o'){
    infile >> expression[i + 1];
    i++;
  }
  //perform other operations within a class

  //print out results
  //read next line from file.

Thank you. I just need help with reading the next line in the file. May not be entirely logically correct just a rough example of what is going on.

Jdismu1
  • 13
  • 4
  • Have you tried 'getLine'? Please check http://www.cplusplus.com/reference/iostream/istream/getline/ – ATaylor Oct 18 '12 at 16:56
  • [while (!f.eof()) is wrong.](http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong) – interjay Oct 18 '12 at 16:58
  • What is '\o'? Did you mean '\0'? What is declaration of expression? Why would expression[i+1] become '\0' or '\o' ever? Who is going to put a '\0' in the file? – user93353 Oct 18 '12 at 16:59

1 Answers1

0

It will be very expensive if you want to do inplace. Open a file, read it line by line, output each line regardless it is changed or not to a temporary file created in the same place where the original file is. At the end close both files, and if there was no error, delete the original and rename the temporary one to the original one.

Grzegorz
  • 2,928
  • 3
  • 15
  • 37