Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Reading Lines with Fgets(?) and a bit of C++ {Novice Programmer}

Reply
Thread Tools

Reading Lines with Fgets(?) and a bit of C++ {Novice Programmer}

 
 
AMT2K5
Guest
Posts: n/a
 
      07-06-2005
I just want to keep this as simple as possible. For parsing and working
with the fields is it suggessted that I use strtok?

AMT2K5 wrote:
> Hello.
> I have a file (for a school assignment) with the following format and
> delimiter format. Each record in the file has the following format:
> 123423454567987,29873,James,Ha*rry,St. Louis,416-555-5555;
> "accountNumber,balance,lastNam*e,city,phoneNumber; " Each record is
> guranteed to be no longer than 350 characters. "balance" is no longer
> than 20 characters, "accountNumber" is exactly 15 characters and the
> total number of characters in "lastName,firstName,city,phone*Number;"
> is
> no longer than 315 characters. Copy the fields in this record to the
> appropriate data member of the first empty Account in the array
> savings, using all the rules for copying (initializing).
>
>
> Contents of a3.dat
>
>
> 123423454567987,29873,James,Ha*rry,St.
> Louis,416-555-5555;22342345456*7987,198745,Jones,Beth,Toronto*,4 16-555-5556;323423454567987,*2349,Ng,Wei,Montreal,416-555-5*557;423423454567987,9234617,Wo*o,Charles,Winnipe g,416-555-555*8;523423454567987,2534,DeJesus*,Pancho,Edmonto n,416-555-5559;*623423454567987,543876,Smith,B*ob,Charlottet own,416-555-5544;*723423454567987,1234,Kasim,Vla*dislov,Halifa x,416-555-5566;82*3423454567987,98765,Yamaha,Dav*id,Vancouve r,416-555-5577;9234*23454567987,26486,Lee,Jim,Calg*ary,416-555-5588;1134234545679*87,83456,Baker,Susan,St.
>
> Louis-de-ha-ha,416-555-5599;13*3423454567987,29873,James,Harr*y,St.
> Louis,416-555-5555;
>
>
> Since each "record" is not a line and is shown as is (above) in the
> file, how might I go and read each "record", extract the information
> and send it to the correct data member of the first empty object in the
> array?
>
> So far I have my function like this
>
> void Bank::workFP(FILE* fp)
> {
> if(fp != NULL){
>
> }
> else printf("Unable to open file.\n");
> }
>
>
> Thanks in advance. Appreciate any help whatsoever.


 
Reply With Quote
 
 
 
 
red floyd
Guest
Posts: n/a
 
      07-06-2005
jason wrote:
[redacted]

> if (fp != NULL)
> {
> char *chunk = new char[256];
> std::string sBuffer;
> while (!feof(fp))
> {
> fread(chunk, sizeof(char), 256, fp); // you might even be able to
> read right into the string struct, but i'm not sure
> string = string + sBuffer;
> }
> delete[] chunk;
> chunk = null;
> DoParsingAndOtherStuff(string);
> }


in addition to Howard's comment on allocating "chunk", you also have the
usage of feof incorrect. feof doesn't kick in until you try to read and
actually have alreay hit EOF. It's not predictive like Pascal.

while (fread(chunk, sizeof(char), 256, fp) > 0)
{
// ...
}

Also, sizeof(char) is redundant since by definition it's 1, but it's
probably better to use it since you may in the future want to go to wchar_t.

Perhaps

while (fread(chunk,sizeof(chunk[0]), 256, fp) > 0)
{
// ...
}

which then becomes independent of the size of your character.
 
Reply With Quote
 
 
 
 
AMT2K5
Guest
Posts: n/a
 
      07-06-2005
Thanks for the help guys. I asked my prof for extra help today and he
suggested that I use fscanf.

Now I am using it but I am having trouble with my format string. I can
display the first fields fine, but the (%315) field I can not. I get a
single garbage character on my screen if I display it.

char accountTemp[16];
int balanceTemp = 0;
char stringTemp[316];

fscanf(fp, "%15[^,],%20d[^,],%315[^;]", accountTemp, &balanceTemp,
stringTemp);

red floyd wrote:
> jason wrote:
> [redacted]
>
> > if (fp != NULL)
> > {
> > char *chunk = new char[256];
> > std::string sBuffer;
> > while (!feof(fp))
> > {
> > fread(chunk, sizeof(char), 256, fp); // you might even be able to
> > read right into the string struct, but i'm not sure
> > string = string + sBuffer;
> > }
> > delete[] chunk;
> > chunk = null;
> > DoParsingAndOtherStuff(string);
> > }

>
> in addition to Howard's comment on allocating "chunk", you also have the
> usage of feof incorrect. feof doesn't kick in until you try to read and
> actually have alreay hit EOF. It's not predictive like Pascal.
>
> while (fread(chunk, sizeof(char), 256, fp) > 0)
> {
> // ...
> }
>
> Also, sizeof(char) is redundant since by definition it's 1, but it's
> probably better to use it since you may in the future want to go to wchar_t.
>
> Perhaps
>
> while (fread(chunk,sizeof(chunk[0]), 256, fp) > 0)
> {
> // ...
> }
>
> which then becomes independent of the size of your character.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
What is the point of having 16 bit colour if a computer monitor can only display 8 bit colour? How do you edit 16 bit colour when you can only see 8 bit? Scotius Digital Photography 6 07-13-2010 03:33 AM
Re: enumerating control pannel items from 32 bit executable on XP 64 bit and Vista 64 bit Homer J. Simpson Windows 64bit 5 01-19-2009 06:22 PM
Reading Lines with Fgets and a bit of C++ {Novice Programmer} AMT2K5 C Programming 6 07-06-2005 02:34 AM
64 bit - Windows Liberty 64bit, Windows Limited Edition 64 Bit, Microsoft SQL Server 2000 Developer Edition 64 Bit, IBM DB2 64 bit - new ! vvcd Computer Support 0 09-17-2004 08:15 PM
64 bit - Windows Liberty 64bit, Windows Limited Edition 64 Bit,Microsoft SQL Server 2000 Developer Edition 64 Bit, IBM DB2 64 bit - new! Ionizer Computer Support 1 01-01-2004 07:27 PM



Advertisments