0

I have created a function to write some data on a text file, and it works fine. I created another function to read in all the content of the file, and print it out for me! But, it is not working for some reason. Could any one help please?

This is my function:

void myClass::displayFile() {
  char line[LINE]; //to hold the current line

  file.open("data.txt", ios::app);

  //keep reading information from the file while the file is open and has data
  while (!file.fail() && !file.eof()) {
    int lineSize; //to loope through each line

    file.getline(line, LINE);
    lineSize = strlen(line);

    //loop through the line to print it without delimiters
    for (int i = 0; i < lineSize; ++i) {
      if (line[i] == ';') {
        cout << " || ";
      } else {
        cout << line[i];
      }
    }
  }
  file.close();
  file.clear();

  if (file.fail()) {
    cerr << "Something went wrong with the file!";
  }
}

Note: The function compiles and the loop is accessible, but the line string is empty.

This is the writing function:

void myClass::fileWriter() {
  file.open("data.txt", ios::app);
  file << name << ";" << age << ";" << "\n";
  file.close();
  file.clear();
}
Wilis1944
  • 3
  • 3
  • 1
    I tried your code on one of my files and i can see it to print the content..Can u check if your file was actually written correctly – Megha Nov 05 '15 at 03:47
  • 1
    Why are you opening in append mode, you're not writing to the file? Why are you using different sizes for the declaration of `line` and the `getline` call? What's wrong with [`std::string`](http://en.cppreference.com/w/cpp/string/basic_string) and [`std::getline`](http://en.cppreference.com/w/cpp/string/basic_string/getline) (which are safer and not prone to buffer overflows)? That `file.fail()` check after the loop, that won't work as you explicitly clear the flags before the check. – Some programmer dude Nov 05 '15 at 03:47
  • Oh, and while ["Why is “while ( !feof (file) )” always wrong?"](http://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong) is tagged for the C programming language, the same problem exists for C++ and e.g. `while (!file.eof())`. – Some programmer dude Nov 05 '15 at 03:48
  • 1
    Finally, you might want to learn how to use a debugger and how to step through code line by line. That way you can see what happens. – Some programmer dude Nov 05 '15 at 03:49
  • Can you provide details on the failure condition? – Greg Nov 05 '15 at 04:28

1 Answers1

0

Silly me, the cause of your problem was staring me right in the face from the beginning, and it's the app open-mode that's the problem. It is to open the file in write mode, which means you can't read from it.

And even if you could read from the file, the cursor is placed ad the end of the file the eofbit flag would have been set inside the first iteration anyway.

If you want to read from a file, then either use std::ifstream which automatically sets the in mode if you don't specify a mode, or you have to explicitly set the in mode when opening.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • TRUE! I tried to remove it after you told me that it is for writing on a file, then my code worked just fine and I was about to declare it here!!! But, you got it, thanks – Wilis1944 Nov 05 '15 at 03:57