mercredi 4 novembre 2015

I Can't print out a file that I wrote on

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, MAX_SIZE);
    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("talent.txt", ios::app);
  file << name << ";" << age << ";" << "\n";
  file.close();
  file.clear();
}

Aucun commentaire:

Enregistrer un commentaire