Computing.Net > Forums > Programming > C++ HELP! File I/O. I'm losing it!

Computing.Net: Over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to sign up now, it's free!

C++ HELP! File I/O. I'm losing it!

Reply to Message Icon

Original Message
Name: NandiniSeshadri
Date: May 5, 2005 at 18:19:13 Pacific
Subject: C++ HELP! File I/O. I'm losing it!
OS: Windows XP
CPU/Ram: Lots. More than enough.
Comment:

It's driving me nuts.

I need to read from a file of data in the following format:

(int showing total number of objects in file)
(string)Name1
(int)ID1
(double)Price1
(int)Stock1
(string)Name2
(int)ID2
(double)Price2
(int)Stock2
... and so on.

I'm reading each set as an object called Item. Here's my reading-in code.

int main() {
int count = 0;
Item itemArray[50];
ifstream fp("myFile.txt", ios::in);
if(!fp.is_open()) {
cerr << "Error while opening file." << endl;
return 0;
}

while (!fp.eof() || count < 10) {
//THE PROBLEM IS HERE, OUTPUT STATEMENT HERE PRINTS OUT, BUT NOTHING AFTER THIS.
for (int i = 0; i < 10; i++) {
fp >> itemArray[i];
count++;
}
}

fp.close();
}

I've also overloaded the >> operator to read the data into the object array directly:

class Item {
friend istream& operator >> (istream&, Item&);
...
};

istream& operator >>(istream& fp, Item& i) {
fp >> i.name;
fp >> i.id;
fp >> i.price;
fp >> i.stock;

return fp;
}

The while thing gets a 'fatal error' message (Windowspeak for 'coredump' I guess?) Oh, and another thing. The coredump DOES NOT HAPPEN when my file contains nothing.

But as soon as I put something into my file, DUMP!

Please help!
-Nandini


Report Offensive Message For Removal


Response Number 1
Name: cdac1000
Date: May 6, 2005 at 07:24:02 Pacific
Reply: (edit)

the for loop in which you try to get the items from the file.

for (int i = 0; i < 10; i++) {
fp >> itemArray[i];
count++;
}

I think fp >> itemArray[i]; this statement is fishy. fp is the file pointer, with this ur dumping the file pointer to array..

I think what you need is one of the file read functions eg. fgetchar or fgetc or fgets

fgets(fp) >> itemArray[i]

Give it a try.


Report Offensive Follow Up For Removal

Response Number 2
Name: egkenny
Date: May 6, 2005 at 21:28:58 Pacific
Reply: (edit)

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

class Item;
istream& operator >>(istream& fp, Item& i);

class Item
{
friend istream& operator >> (istream&, Item&);
public:
Item();
string name;
int id;
double price;
int stock;
};

Item::Item() : id(0), price(0.0), stock(0)
{
}

istream& operator >>(istream& fp, Item& i)
{
fp >> i.name;
fp >> i.id;
fp >> i.price;
fp >> i.stock;

return fp;
}


int main()
{
int count = 0;
Item itemArray[50];
ifstream fp("myFile.txt", ios::in);
if(!fp.is_open())
{
cerr << "Error while opening file." << endl;
return 1;
}

fp >> count;
int i=0;
fp >> itemArray[i];
while (!fp.eof() && i<count)
{
i++;
fp >> itemArray[i];
}

fp.close();

return 0;
}


Report Offensive Follow Up For Removal

Response Number 3
Name: egkenny
Date: May 6, 2005 at 21:36:11 Pacific
Reply: (edit)

Here's the data file "myFile.txt":
2
thename1
11
12.34
5
thename2
22
56.78
9


Report Offensive Follow Up For Removal







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home








Do you own an iPhone?

Yes
No, but soon
No


View Results

Poll Finishes In 7 Days.
Discuss in The Lounge
Poll History




Data Recovery Software