B
bfowlkes
Hello,
I am trying to parse two pre-formatted text files and write them to a
different files formatted in a different way. The story about this is I
was hired along with about 20 other people and it seems we are trying
to learn the whole C language in two weeks! To top it all off, I was an
English Major, but I'm trying my best. Ok back to the program. So we
have two files product_catalog.txt and sales_month.txt
The info in product_catalog.txt looks like this:
1010:CD drive external 32x :1MagiCopy:15.5:100
1020:CD drive external 40x :20th Century Fox:16.74:130
1030:CD drive external 48x :3COM:13.48:160
1040:CD drive external 52x :4XEM:15.92:190
We need to write it to another file that is going to look like this
ID Number Description Provider Cost Stock Total
1010 CD Drive 32x 1MagiCopy 15.50 100 1550.00
Since the text file to be read from is preformatted I thought I could
use the fscanf() to to parse each line and assign it into structure
variables, but I am having problems.
Here is my code to read the file:
int readFile (char *filename, struct productData product[], size_t
arrLen)
/* Returns number of products read */
{
FILE *fp;
if ( ( fp = fopen( "product_catalog.txt", "rb+" ) ) == NULL ) {
printf( "File could not be opened.\n" );
} /* end if */
else
{
int i;
for (i=0; i<arrLen && !feof(fp); i++)
{
if (5 != fscanf(fp, "%d %s %s %f %d",
&product.idnumber,
product.description,
product.provider,
&product.cost,
&product.stock))
{
printf("Invalid file format\n");
fclose(fp);
return 0;
}
}
fclose(fp);
return i;
}
}
The problem seems to be that each field I want to parse seems to be
separated by a colon
) Is there anyway to tell fscanf() to parse up
until you reach a colon and then stop and start scanning again, or
should I give up this approach and try to tokenize the input stream?
Any help is much appreciated.
Brett
I am trying to parse two pre-formatted text files and write them to a
different files formatted in a different way. The story about this is I
was hired along with about 20 other people and it seems we are trying
to learn the whole C language in two weeks! To top it all off, I was an
English Major, but I'm trying my best. Ok back to the program. So we
have two files product_catalog.txt and sales_month.txt
The info in product_catalog.txt looks like this:
1010:CD drive external 32x :1MagiCopy:15.5:100
1020:CD drive external 40x :20th Century Fox:16.74:130
1030:CD drive external 48x :3COM:13.48:160
1040:CD drive external 52x :4XEM:15.92:190
We need to write it to another file that is going to look like this
ID Number Description Provider Cost Stock Total
1010 CD Drive 32x 1MagiCopy 15.50 100 1550.00
Since the text file to be read from is preformatted I thought I could
use the fscanf() to to parse each line and assign it into structure
variables, but I am having problems.
Here is my code to read the file:
int readFile (char *filename, struct productData product[], size_t
arrLen)
/* Returns number of products read */
{
FILE *fp;
if ( ( fp = fopen( "product_catalog.txt", "rb+" ) ) == NULL ) {
printf( "File could not be opened.\n" );
} /* end if */
else
{
int i;
for (i=0; i<arrLen && !feof(fp); i++)
{
if (5 != fscanf(fp, "%d %s %s %f %d",
&product.idnumber,
product.description,
product.provider,
&product.cost,
&product.stock))
{
printf("Invalid file format\n");
fclose(fp);
return 0;
}
}
fclose(fp);
return i;
}
}
The problem seems to be that each field I want to parse seems to be
separated by a colon
until you reach a colon and then stop and start scanning again, or
should I give up this approach and try to tokenize the input stream?
Any help is much appreciated.
Brett