String of Arrays

N

Nimmy

Hi,
I have code like this to read strings and integers from an input file....

FILE *FileRead, *FileWrite;
char lcBIC[200], lcIBAN[200];
int lcAccNumber[200], lcAmount[200];
int i, total_records;

FileRead = fopen( "input.txt" , "r" );
FileWrite = fopen( "output.txt", "r+w" );

for (i = 1; i <=2; ++i)
{
fscanf(FileRead, "%s,%d\n",&lcBIC, &lcAccNumber);
}
*********
But problem with this code is that it is reading the whole line including
commas and putting them in lcBIC, how can I read individual variables and
transfer to the variables?

Thanks
 
R

Rich Grise

Nimmy said:
Hi,
I have code like this to read strings and integers from an input file....

FILE *FileRead, *FileWrite;
char lcBIC[200], lcIBAN[200];
int lcAccNumber[200], lcAmount[200];
int i, total_records;

FileRead = fopen( "input.txt" , "r" );
FileWrite = fopen( "output.txt", "r+w" );

for (i = 1; i <=2; ++i)
{
fscanf(FileRead, "%s,%d\n",&lcBIC, &lcAccNumber);
}
*********
But problem with this code is that it is reading the whole line including
commas and putting them in lcBIC, how can I read individual variables
and transfer to the variables?

Thanks


How much of the string does '%s' parse? How much does the spec say
%s is _supposed_ to parse?

Cheers!
Rich
 
G

Gregory Pietsch

Nimmy said:
Hi,
I have code like this to read strings and integers from an input file....

FILE *FileRead, *FileWrite;
char lcBIC[200], lcIBAN[200];
int lcAccNumber[200], lcAmount[200];
int i, total_records;

FileRead = fopen( "input.txt" , "r" );
FileWrite = fopen( "output.txt", "r+w" );

If you're writing for update, wouldn't that be "w+"?
for (i = 1; i <=2; ++i)
{
fscanf(FileRead, "%s,%d\n",&lcBIC, &lcAccNumber);
}
*********
But problem with this code is that it is reading the whole line including
commas and putting them in lcBIC, how can I read individual variables and
transfer to the variables?

Thanks


Use fgets() to read a whole line in, then parse your string manually.
The functions strtol(), strtoul(), and possibly strtok() (but be
careful with it) may be of use.

Gregory Pietsch
 
C

Chris Torek

If you're writing for update, wouldn't that be "w+"?

No; but "r+" would be simpler (the "w" is likely ignored, at least
in C99 -- in C89 it is a bad idea). "w+" means "discard all existing
data, and open for read/write" while "r+" means "retain existing
data, open for read/write". In addition, "w+" means "create the
file if needed" while "r+" means "never create the file at all".

Note that there is something missing here, namely, "create the
file if needed, but DO NOT discard the data if there is some".
The "a+" mode does this, but also force all new writes to append
to the end of the file.

There really should be a string that is halfway between "r+" and
"a+" (or halfway between "w+" and "a+", depending on how you look
at it): "open for reading and writing, retaining existing data if
any, but creating a new file if necessary". This mode is missing
in ANSI/ISO C. You can write:

fp = fopen(filename, "r+");
if (fp == NULL)
fp = fopen(filename, "w+");

to approximate it, but this has a race condition on multi-process
systems.
 

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

No members online now.

Forum statistics

Threads
474,145
Messages
2,570,826
Members
47,372
Latest member
LucretiaFo

Latest Threads

Top