Computing.Net > Forums > Programming > File I/O in c++

File I/O in c++

Reply to Message Icon

Original Message
Name: gimmpy224
Date: August 9, 2004 at 22:23:21 Pacific
Subject: File I/O in c++
OS: windows XP pro
CPU/Ram: athlon XP 512 mb
Comment:

Hey guys, Havnt been on in a while cause ive been gaming a lot and finishing up memorizing and taking notes on my book :).
well my problem(s) right now are 1, my program has some weird numbers in it when i print the list saved in the file.

the code is :

#include <fstream.h>
#include <ctype.h>


struct TestStruct
{
char name[30];
char age[30];
};

void Add(fstream& Info);
void List(fstream& Info);

void main()
{
char ans;

fstream Info("c:\\Info.txt", ios::in | ios::end);

if (!Info)
{
cout << "****ERROR - FILE DID NOT OPEN****"<<endl;
return;
}

while(1)
{

cout << "(A)dd, (L)ist, (Q)uit? ";

cin >> ans;

switch (toupper(ans))
{
case 'A':
Add(Info);

break;
case 'L':
List(Info);

break;
case 'Q':
return;
break;
}
}

return;
}

//****************************************************************

void Add(fstream& Info)
{
TestStruct entry = {0};

cout << "Whats the persons name?? ";

cin.getline(entry.name,30);

cout <<endl;

cout << "Whats the persons age?? ";

cin.getline(entry.age,30);

Info.seekg(0L, ios::end);

Info.write((char*)&entry, sizeof(TestStruct));
}
//****************************************************************

void List(fstream& Info)
{
TestStruct entry;

int count = 0;

Info.seekg(0L,ios::beg);

while (!Info.eof())
{
Info.read((char*)&entry,sizeof(TestStruct));

if (Info.eof())
{
break;
}

cout << ++count << ": " << entry.name << '/t' << entry.age <<endl;
}
Info.clear();
}

and it gives me the numbers 12148 when i ask it to print the list, and i cant for the life of me figure out how they are being made lol.
can anyone figure this problem out??
Also, any advice on how to improve my code and stuff... mainly talking to IR or DON since you two have been helping me since i started :).

also, this code is using what my book taught me in the very last chapter, so where should i go from here, what should i look for in a c++ book to further inprove upon my knowledge :)

THANKS LOADS!!! :)

GIMPS


Report Offensive Message For Removal

Response Number 1
Name: BlueRaja
Date: August 10, 2004 at 01:15:52 Pacific
Subject: File I/O in c++
Reply: (edit)

Why just IR or Don? Some of the rest of us know how to program as well...although I haven't done anything in command prompt since I started programming C++ in the 9th grade (about two years ago..) doesn't mean I'm HORRIBLY rusty...

First of all, I'll start off by saying I'm not going to do anyone's homework for them. I don't know if this is homework or not, but in either case, it's more enjoyable to "figure" it out on your own ;P

Now, onto the problem...

1. To get rid of those extra numbers, you must remember that there's a difference between "/" and "\" ;)

2. I noticed that you're not flushing your input buffer before calling cin.getline(). This will cause your next cin.getline() (the one for entry.name) to misfunction.
To clear the input buffer, use the function
fflush(stdin);
However, notice that this uses the STANDARD stream class (simply because that's what I was taught from my cheap little books and semi-useless online game creation forums). This means you'll have to use the standard input library id est change the includes from

#include <fstream.h>
#include <ctype.h>

to

#include <fstream>
#include <iostream>
#include <ctype.h>
using namespace std;

Hope that helps ;P


Report Offensive Follow Up For Removal

Response Number 2
Name: gimmpy224
Date: August 10, 2004 at 16:41:27 Pacific
Subject: File I/O in c++
Reply: (edit)

illg ive it a try, and yea im just now in 10th grade, and i was asking IR and DON because they have been helping me since i first started c++ 4 or 5 months ago :)

GIMPS


Report Offensive Follow Up For Removal

Response Number 3
Name: gimmpy224
Date: August 10, 2004 at 16:49:06 Pacific
Subject: File I/O in c++
Reply: (edit)

well i finaly got rid of those pesky numbers, but i didnt understand what you meant by "flushing" my input buffers.

how would i use that fflush(stdin) function?

GIMPS


Report Offensive Follow Up For Removal

Response Number 4
Name: gimmpy224
Date: August 10, 2004 at 16:51:02 Pacific
Subject: File I/O in c++
Reply: (edit)

lol nvm. i figured it out, stupid mistake on my part :)

yea im reading an older book that uses visual c++ 1.0 and im programming in 6.0
so i think thats why i keep running into problems :)

thanks BLue :)

GIMPS


Report Offensive Follow Up For Removal

Response Number 5
Name: BlueRaja
Date: August 10, 2004 at 23:20:36 Pacific
Subject: File I/O in c++
Reply: (edit)

lol, anytime


Report Offensive Follow Up For Removal







Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: File I/O in c++

Comments:

 


  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 
Data Recovery Software