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. -
void __fastcall TForm1::FormCreate(TObject *Sender)
-
{
-
myProductList.ReadData(i,size);
-
-
myProductList.getProduct(i)->display(ProductIDLabel,
-
ProductNameLabel,
-
ProductWeightLabel,
-
PriceWithoutVATLabel,
-
PriceWithVATLabel,
-
ExpiryDateLabel,
-
AmountInStockLabel,
-
ReorderLimitLabel,
-
ProductCategoryLabel,
-
InputDateLabel,
-
DueDateLabel,
-
ManufacturerLabel,
-
ManufacturerAddressLabel,
-
ReorderStatus);
-
}
-
This is the ReadData() function in the myProductList class: -
void ProductList :: ReadData(int &i, int &size)
-
{
-
AnsiString ProductName;
-
int ProductID;
-
float Weight;
-
float PriceWithoutVAT;
-
float PriceWithVAT;
-
AnsiString ExpiryDate;
-
int AmountInStock;
-
int ReorderLimit;
-
AnsiString ProductCategory;
-
AnsiString InputDate;
-
AnsiString DueDate;
-
AnsiString Manufacturer;
-
AnsiString ManufacturerAddress;
-
-
ifstream datafile("data.txt", ios::in);
-
if (!datafile)
-
{
-
exit(1);
-
}
-
else
-
{
-
while(!datafile.eof())
-
{
-
i++;
-
size++;
-
datafile>>ProductID;
-
datafile>>ProductName.c_str();
-
datafile>>Weight;
-
datafile>>ExpiryDate.c_str();
-
datafile>>PriceWithoutVAT;
-
datafile>>PriceWithVAT;
-
datafile>>AmountInStock;
-
datafile>>ReorderLimit;
-
datafile>>ProductCategory.c_str();
-
datafile>>InputDate.c_str();
-
datafile>>DueDate.c_str();
-
datafile>>Manufacturer.c_str();
-
datafile>>ManufacturerAddress.c_str();
-
-
myProductList.push_back(Product(ProductName,
-
ProductID,
-
Weight,
-
PriceWithoutVAT,
-
PriceWithVAT,
-
ExpiryDate,
-
AmountInStock,
-
ReorderLimit,
-
ProductCategory,
-
InputDate,
-
DueDate,
-
Manufacturer,
-
ManufacturerAddress));
-
-
};
-
-
datafile.close();
-
-
index=i;
-
listsize=size;
-
}
-
}
-
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?
3 1233
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.
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..
I've been using c_str() because without it the compiler says it doesn't have operator >> for Strings.
You need to #include <string>.
Sign in to post your reply or Sign up for a free account.
Similar topics |
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...
|
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
|
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...
|
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...
|
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();
| |
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...
|
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...
|
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....
|
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...
|
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++.
|
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...
| |
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,...
|
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...
|
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,...
|
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...
|
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...
|
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...
| |
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...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |