0

first time here. I am a student doing some c++ coding for end year project. The programme that I coded does not read the text file even though everything seems to be in order. Some helps would be fantastic!

void transactionRecords(double total, char answer, string nameT, int HpNo, string address)
{
    fstream myFile;
    string name;
    char idStatus;
    double amt, sumAll=0;
    myFile.open("transaction.txt",fstream::in);
    if (!myFile) cout<<"Unable to Open File under Input Mode";
    else
    {
        while (!myFile.eof())
        {
            myFile>>name>>idStatus>>address>>HpNo>>amt;
            if (myFile.fail()) break;
        }
        myFile.close();

        myFile.open("transaction.txt",fstream::app);
        if (!myFile) cout<<"Unable to Open File under App Mode";
        else
        {
            myFile<<nameT<<" "<<answer<<" "<<address<<" "<<HpNo<<" "<<total<<endl;
            if (myFile.fail()) cout<<"Error encountered while adding data!\n";
        }
    }
    myFile.close();
}

this is whats in the text file

Johns Y pasir_ris 81231211 4.14

user7545349
  • 3
  • 1
  • 3
  • 1
    What if instead of `if (myFile.fail()) break;` you report the error? What's in the file to begin with? – doctorlove Feb 10 '17 at 10:44
  • 1
    What exactly isn't working? You're reading into the arguments which have been passed by value, if you're expecting them to be populated by this function for use outside they need to be references. – Colin Feb 10 '17 at 10:45
  • Use debugger or show exception/error. – Amit Kumar Feb 10 '17 at 10:51
  • @Colin__s its just showing that the programme is unable to open file – user7545349 Feb 10 '17 at 11:03
  • Is `transaction.txt` in the same place as the program is being run from? Debuggers sometimes have run locations you're not expecting. – Colin Feb 10 '17 at 11:05
  • @Colin__s the text is under the same folder as the programme and under the source file folder in the solution explorer! – user7545349 Feb 10 '17 at 11:11

1 Answers1

0

First, I suggest you use the RAII principle when creating files that is:

void myfunction() {
   ifstream f{"file.txt"};
   // your logic here
   // NB -- no need to manually close file, prevents resource leaks
}

There is no need to manually close or open such a file, it is opened by the constructor and closed when the destructor is invoked upon exiting the current scope. This prevents leaks of file handles and is a pervasive technique in C++.

Second, use the standard stream read loop in C++:

while (myFile>>name>>idStatus>>address>>HpNo>>amt) {
  // your logic here, the read has succeded
  // TODO process myFile, name, idStatus etc.
}

With these changes, your example should look something like:

void transactionRecords(double total, char answer, string nameT, int HpNo, string address)
{
    ifstream myFile{"transaction.txt"};
    string name;
    char idStatus;
    double amt, sumAll=0;
    if (!myFile) cout<<"Unable to Open File under Input Mode";
       return;

    while (myFile>>name>>idStatus>>address>>HpNo>>amt) {
           // TODO do something here??
    }

    ofstream tFile{"transaction.txt"};    
    if (!myFile) cout<<"Unable to Open File under App Mode";
      return;

    if (!(tFile<<nameT<<" "<<answer<<" "<<address<<" "<<HpNo<<" "<<total<<endl)) {
              cout<<"Error encountered while adding data!\n";
        }
    }
}

You should probably do something in the TODO block, currently you are only storing the last values read? If you only mean to process one line from the file, swap the while loop with an if.

paul-g
  • 3,510
  • 1
  • 17
  • 32