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

A

AMT2K5

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,Harry,St. Louis,416-555-5555;
"accountNumber,balance,lastName,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,phoneNumber;" 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).

Would fgets work in this situation (I have never used fgets)? How would
you guys do it?

Thanks in advance.
 
E

Eric Sosman

AMT2K5 said:
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,Harry,St. Louis,416-555-5555;
"accountNumber,balance,lastName,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,phoneNumber;" 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).

Would fgets work in this situation (I have never used fgets)? How would
you guys do it?

fgets() makes sense if each "record" is a complete
line, but is less appropriate if the semicolon is the
only delimiter:

...,St. Louis,416-555-555;987654321654321,...

If each record is in fact a line, you could use
fgets() this way:

#include <stdio.h>
#define MAXLEN 350
...
char buff[MAXLEN+1+1]; /* extra for '\n' and '\0' */
FILE *input = fopen(...); /* the input stream */
if (input == NULL)
die(); /* fopen() failed */
while (fgets(buff, sizeof buff, input) != NULL) {
/* process the line in `buff' */
}
if (ferror(input))
die(); /* input error */
fclose (input);
 
A

AMT2K5

Contents of a3.dat

123423454567987,29873,James,Harry,St.
Louis,416-555-5555;223423454567987,198745,Jones,Beth,Toronto,416-555-5556;323423454567987,2349,Ng,Wei,Montreal,416-555-5557;423423454567987,9234617,Woo,Charles,Winnipeg,416-555-5558;523423454567987,2534,DeJesus,Pancho,Edmonton,416-555-5559;623423454567987,543876,Smith,Bob,Charlottetown,416-555-5544;723423454567987,1234,Kasim,Vladislov,Halifax,416-555-5566;823423454567987,98765,Yamaha,David,Vancouver,416-555-5577;923423454567987,26486,Lee,Jim,Calgary,416-555-5588;113423454567987,83456,Baker,Susan,St.
Louis-de-ha-ha,416-555-5599;133423454567987,29873,James,Harry,St.
Louis,416-555-5555;
 
R

Richard Bos

AMT2K5 said:
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,Harry,St. Louis,416-555-5555;
"accountNumber,balance,lastName,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,phoneNumber;" 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).

Would fgets work in this situation (I have never used fgets)?

Yes. Declare an array of 352 chars (350 plus line-ending '\n' plus
terminating '\0'), and use fgets() on it. Use the return value of
fgets() to see whether you have reached the end of the file or have read
a valid line.
Then you can use whatever you like to split the fields from the line:
sscanf() could work, provided the fields can't contain spaces; so could
strtok(), provided all fields are guaranteed to be present and not
empty. If you can have both empty fields and spaces within fields (e.g.,
if you have customers called "Jon Bob" _and_ corporate customers with no
first name), you could use strchr().

Solid code, of course, would either not assume that all lines are less
then 350 characters, or, if this were an innate requirement of the file
(e.g., because of database field sizes), reject longer lines with a
warning. fgets() can help with that, as well, since if you've read at
most

Richard
 
D

Default User

AMT2K5 said:
Hello.
I have a file (for a school assignment) with the following format and
delimiter format.


Where does the "bit of C++" mentioned in the subject come into this?
Are you working in C++? If so, why aren't you posting to comp.lang.c++?





Brian
 
A

AMT2K5

I figure I have to use Fgets which is a stdio standard C function.
Where C++ comes into play is where I am sending the recieved
information from the dat file and sending it off to data members of a
instantiated class of a class of arrays.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top