Connecting Tech Pros Worldwide Forums | Help | Site Map

AnsiString input from file

Member
 
Join Date: Jan 2007
Location: London
Posts: 62
#1: Nov 4 '07
Hi guys,
I have a problem with inputting from a file. I have this function:

Expand|Select|Wrap|Line Numbers
  1.  
  2. void Product :: InputFromFile()
  3. {
  4.         fstream inputfile;
  5.         inputfile.open("input.txt");
  6.  
  7.         inputfile>>ProductID;
  8.         inputfile>>ProductName.c_str();
  9.         inputfile>>Weight;
  10.         inputfile>>PriceWithoutVAT;
  11.  
  12.         PriceWithVAT=CalculateVATPrice();
  13.  
  14.         inputfile>>ExpiryDate.c_str();
  15.         inputfile>>AmountInStock;
  16.         inputfile>>ReorderLimit;
  17.          inputfile>>ProductCategory.c_str();
  18.          inputfile>>InputDate.c_str();
  19.          inputfile>>DueDate.c_str();
  20.          inputfile>>Manufacturer.c_str();
  21.          inputfile>>ManufacturerAddress.c_str(); 
  22.          inputfile.close(); //
  23. }
  24.  
and a simple text file with data. The program reads only the data until the line inputfile>>PriceWithoutVAT; , and doesn't see all the rest data. It even ignores the CalculateVATPrice() function, which in other functions works fine.
I have tried this also as:

Expand|Select|Wrap|Line Numbers
  1.  
  2.        fstream inputfile;
  3.         inputfile.open("input.txt");
  4.  
  5.         inputfile>>ProductID;
  6.         inputfile.getline(ProductName.c_str(),10,'\n');
  7.         inputfile>>Weight;
  8.         inputfile>>PriceWithoutVAT;
  9.  
  10.         PriceWithVAT=CalculateVATPrice();
  11.         inputfile.getline(ExpiryDate.c_str(),10,'\n');
  12.  
  13.         inputfile>>AmountInStock;
  14.         inputfile>>ReorderLimit;
  15.  
  16.        inputfile.getline(ProductCategory.c_str(),10,'\n');
  17.        inputfile.getline(InputDate.c_str(),10,'\n');
  18.        inputfile.getline(DueDate.c_str(),10,'\n');
  19.        inputfile.getline(Manufacturer.c_str(),10,'\n');
  20.        inputfile.getline(ManufacturerAddress.c_str(),10,'\n');
  21.        inputfile.close();
  22.  
It has the same result. I'm really stuck, everything seems right, and it works for the first lines, which are done in the same way.
Please help if anybody has any ideas.
Thanks.

Moderator
 
Join Date: Mar 2007
Location: North Bend Washington USA
Posts: 5,369
#2: Nov 5 '07

re: AnsiString input from file


What's haappening is that one of your inputfile>> is failing.

The >> operator fetches data into the variable you specify. It that variable is the wrong type (like trying to fetch a 1 so a char or an A to an int), the >> operator sets a fail bit.

This fail bit is the first things check by the >> operator itself. SO on the next inputfile>> the fail bit is on so the >> does nothing and returns.

The effect is that after the failure, all of the >> operators in your program appear to not work.

You have to
1)test every >> operator to make sure the fail bit is off:
Expand|Select|Wrap|Line Numbers
  1. inputfile>>data;
  2. if (inputfile.fail() == true)
  3. {
  4.     //Big problem
  5. }
  6. //OK to continue
  7.  
If the fail bit is on, you can reset it by calling inputfile.clear(). However, the data that caused the error is still in the inout stream and will have to be dealt with.

Remember, the >> assumes you know ahead of time what data is being fetched AND that the data elements are separated from each other by whitespace. If this is not the case, then you can't use the >> operator.
Member
 
Join Date: Jan 2007
Location: London
Posts: 62
#3: Nov 5 '07

re: AnsiString input from file


Quote:

Originally Posted by weaknessforcats

What's haappening is that one of your inputfile>> is failing.

The >> operator fetches data into the variable you specify. It that variable is the wrong type (like trying to fetch a 1 so a char or an A to an int), the >> operator sets a fail bit.

This fail bit is the first things check by the >> operator itself. SO on the next inputfile>> the fail bit is on so the >> does nothing and returns.

The effect is that after the failure, all of the >> operators in your program appear to not work.

You have to
1)test every >> operator to make sure the fail bit is off:

Expand|Select|Wrap|Line Numbers
  1. inputfile>>data;
  2. if (inputfile.fail() == true)
  3. {
  4.     //Big problem
  5. }
  6. //OK to continue
  7.  
If the fail bit is on, you can reset it by calling inputfile.clear(). However, the data that caused the error is still in the inout stream and will have to be dealt with.

Remember, the >> assumes you know ahead of time what data is being fetched AND that the data elements are separated from each other by whitespace. If this is not the case, then you can't use the >> operator.

First of all, thank you very much for the reply.
You know, I'm starting to think that either Borland at all or just my version specifically has a very bad compiler.
Yesterday i left this function as it was, and today when I opened the program and recompiled it, while debugging using breakpoints I see that it is reading all the rest data that it wasn't reading yesterday, it's processing the function it ignored yesterday, but now instead it is giving me some error just in the end, something about access violation at xxxxx.
I'll try to resolve this somehow, if anyone has suggestions please post.
Reply