473,830 Members | 2,326 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Reading from file problem

62 New Member
Hello everyone,

I'm having a problem with reading data from file. In my form i have a function which should read the data from a file into a vector myProductList.

Expand|Select|Wrap|Line Numbers
  1. void __fastcall TForm1::FormCreate(TObject *Sender)
  2. {
  3.        myProductList.ReadData(i,size);
  4.  
  5.        myProductList.getProduct(i)->display(ProductIDLabel,
  6.                                                      ProductNameLabel,
  7.                                                      ProductWeightLabel,
  8.                                                      PriceWithoutVATLabel,
  9.                                                      PriceWithVATLabel,
  10.                                                      ExpiryDateLabel,
  11.                                                      AmountInStockLabel,
  12.                                                      ReorderLimitLabel,
  13.                                                      ProductCategoryLabel,
  14.                                                      InputDateLabel,
  15.                                                      DueDateLabel,
  16.                                                      ManufacturerLabel,
  17.                                                      ManufacturerAddressLabel,
  18.                                                      ReorderStatus);
  19. }
  20.  
This is the ReadData() function in the myProductList class:

Expand|Select|Wrap|Line Numbers
  1. void ProductList :: ReadData(int &i, int &size)
  2. {
  3.     AnsiString ProductName;
  4.     int ProductID;
  5.     float Weight;
  6.     float PriceWithoutVAT;
  7.     float PriceWithVAT;
  8.     AnsiString ExpiryDate;
  9.     int AmountInStock;
  10.     int ReorderLimit;
  11.     AnsiString ProductCategory;
  12.     AnsiString InputDate;
  13.     AnsiString DueDate;
  14.     AnsiString Manufacturer;
  15.     AnsiString ManufacturerAddress;
  16.  
  17.         ifstream datafile("data.txt", ios::in);
  18.         if (!datafile)
  19.                 {
  20.                         exit(1);
  21.                 }
  22.         else
  23.         {
  24.                while(!datafile.eof())
  25.               {
  26.                 i++;
  27.                 size++;
  28.                      datafile>>ProductID;
  29.                      datafile>>ProductName.c_str();
  30.                      datafile>>Weight;
  31.                      datafile>>ExpiryDate.c_str();
  32.                      datafile>>PriceWithoutVAT;
  33.                      datafile>>PriceWithVAT;
  34.                      datafile>>AmountInStock;
  35.                      datafile>>ReorderLimit;
  36.                      datafile>>ProductCategory.c_str();
  37.                      datafile>>InputDate.c_str();
  38.                      datafile>>DueDate.c_str();
  39.                      datafile>>Manufacturer.c_str();
  40.                      datafile>>ManufacturerAddress.c_str();
  41.  
  42.                      myProductList.push_back(Product(ProductName,
  43.                                                     ProductID,
  44.                                                     Weight,
  45.                                                     PriceWithoutVAT,
  46.                                                     PriceWithVAT,
  47.                                                     ExpiryDate,
  48.                                                     AmountInStock,
  49.                                                     ReorderLimit,
  50.                                                     ProductCategory,
  51.                                                     InputDate,
  52.                                                     DueDate,
  53.                                                     Manufacturer,
  54.                                                     ManufacturerAddress));
  55.  
  56.               };
  57.  
  58.                 datafile.close();
  59.  
  60.                 index=i;
  61.                 listsize=size;
  62.         }
  63. }
  64.  
The other function - display() is working fine, but the problem is, when the file is empty, the program still reads some rubbish data from somewhere, i.e rubbish number values, and all the Strings are NULL. But if the file has some data, the program stucks and doesn't even open the form.
Could anyone help me, please?
Nov 19 '07 #1
3 1233
weaknessforcats
9,208 Recognized Expert Moderator Expert
datafile>>Produ ctName.c_str();
datafile>>Weigh t;
datafile>>Expir yDate.c_str();
I didn't look at all of the code but this is defintely a problem.

You need to datafile>>Produ ctName rather than
datafile>>Produ ctName.c_str().

The c_str() method makes a copy of the string. The string object was never used.
Nov 20 '07 #2
jewel87
62 New Member
I didn't look at all of the code but this is defintely a problem.

You need to datafile>>Produ ctName rather than
datafile>>Produ ctName.c_str().

The c_str() method makes a copy of the string. The string object was never used.
Thanks, I've tried to use char array instead, it removed part of the problems. I've been using c_str() because without it the compiler says it doesn't have operator >> for Strings.

Now, if the file it is reading is empty, it still tries to access it and reads some rubbish values from somewhere. Why my .eof condition is not working??? File is empty for sure..
Nov 20 '07 #3
weaknessforcats
9,208 Recognized Expert Moderator Expert
I've been using c_str() because without it the compiler says it doesn't have operator >> for Strings.
You need to #include <string>.
Nov 21 '07 #4

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

Similar topics

1
7060
by: fabrice | last post by:
Hello, I've got trouble reading a text file (event viewer dump) by using the getline() function... After 200 - 300 lines that are read correctly, it suddenly stops reading the rest of the file... Thank you to all of you who can help me with this one...
3
2214
by: SB | last post by:
Hello. I have an input file which is laid out in the following manner... Name Day 1 am time 1 am time 2 appointment pm time 1 pm time 2 appointment Day 2
8
18259
by: Phil Slater | last post by:
I'm trying to process a collection of text files, reading word by word. The program run hangs whenever it encounters a word with an accented letter (like rôle or passé) - ie something that's not a "char" with an ASCII code in 0..127 I've searched the ANSI C++ standard, the internet and various text books, but can't see how to workaround this one. I've tried wchar_t and wstring without success. But rather than spending lots of time on...
8
5232
by: dbuser | last post by:
Hi, I need help on a problem, as described below. I am reading a file "input.txt"which has data like this: abc def gh izk lmnopq rst uvwxyz I am using fstream object to read the file and writing into a dynamic array. My problem is that the array shows extra z and probably because of this further processing gives run time error in borland compiler. Can you please tell me, if the problem is related to handling end-of line , how do i do...
2
2501
by: Sabin Finateanu | last post by:
Hi I'm having problem reading a file from my program and I think it's from a procedure I'm using but I don't see where I'm going wrong. Here is the code: public bool AllowUsage() { OperatingSystem os = Environment.OSVersion; AppDomain ad = Thread.GetDomain();
1
2023
by: Need Helps | last post by:
Hello. I'm writing an application that writes to a file a month, day, year, number of comments, then some strings for the comments. So the format for each record would look like: mdyn"comment~""comment~"\n Now, I wrote some code to read these records, and it works perfectly for every date I've tried it on, except when the day is 26. I tried saving a record for 6/26/2004 and 7/26/2004 and it read it in as the day, year, and number of...
7
6072
by: John Dann | last post by:
I'm trying to read some binary data from a file created by another program. I know the binary file format but can't change or control the format. The binary data is organised such that it should populate a series of structures of specified variable composition. I have the structures created OK, but actually reading the files is giving me an error. Can I ask a simple question to start with: I'm trying to read the file using the...
2
1750
by: GeoUK | last post by:
Hi All, New member here with a bit of problem. I have read the FAQ's and searched text books but still cannot solve the problem that I have. As part of a course I am doing at University I had to write a program in C++ that would allow the user to enter student information (matriculation number, name, status and mark), which was then stored in an array. They could then search it or list it. Everything was woking (and still is) up to here....
5
14999
blazedaces
by: blazedaces | last post by:
Ok, so you know my problem, java is running out of memory reading with SAX, the event-based xml parser intended more-so than DOM for extremely large files. I'll try to explain what I've been doing and why I have to do it. Hopefully someone has a suggestion... Alright, so I'm using a gps-simulation program that outputs gps data, like longitude, lattitude, altitude, etc. (hundreds of terms, these are just the well known ones). In the newer...
6
3536
by: efrenba | last post by:
Hi, I came from delphi world and now I'm doing my first steps in C++. I'm using C++builder because its ide is like delphi although I'm trying to avoid the vcl. I need to insert new features to an old program that I wrote in delphi and it's a good opportunity to start with c++.
0
9790
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9642
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10770
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10524
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10199
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9312
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7741
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5616
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3956
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.