473,378 Members | 1,555 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

AnsiString input from file

62
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.
Nov 4 '07 #1
2 2355
weaknessforcats
9,208 Expert Mod 8TB
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.
Nov 5 '07 #2
jewel87
62
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.
Nov 5 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Mendon?a | last post by:
I cant seem to find on borland help files anything about AnsiString to char conversion. Just the opposite. Can u help me?
5
by: JNY | last post by:
Hi, I'm using a GUI Edit box to allow the user to enter a number. This data is stored as an AnsiString. I need it in Integer format, but cannot convert between the two. I found...
9
by: Franz Seidl | last post by:
Hello, how can i convert a AnsiString to double? I'm using C++ Builder 5 from Borland... Greets, Franz --
2
by: crash | last post by:
I have a program that i changed over from using char *'s to using ansistrings. Previously it had been: char string1; char string2; strcpy(string1,string2); now i use:
6
by: afraze | last post by:
Hi! My problem is that: When i declare an AnsiString function in my Form.h("Form.h" is an ex. name) i am getting this error: :Unresolved external ' __fastcall System::TObject::ToString()...
3
by: larcrik | last post by:
i have to convert a string into an AnsiString. Is there a way to do that?
5
by: Michael Bell | last post by:
I have just done (but done badly in) the Open University course "MT262: Putting computers to work". Borland Builder 5 is supplied as part of the course. It uses the data-type AnsiString for strings...
1
codexparse
by: codexparse | last post by:
I am writing a browser program in C++ Builder 6 that loads a web page using the following code: void __fastcall TForm1::ToolButton1Click(TObject *Sender) { wchar_t buff; ...
4
by: jpenguin | last post by:
I'm writing an MCF prog, and I need to convert an ansistring to a cstring, but it seems to lose the last letter-- char* S = input->Text.c_str(); Label1->Caption = S;
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.