473,289 Members | 2,087 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,289 software developers and data experts.

trouble reading from file

bas
hey,

I am having a lot of trouble trying to get this program to work
properly. It is supposed to read integers from a file and place them
into the categories for inventory. The problem seems to be that the
program will only read the first line of integers from the file, and
just print them into all the categories. Any help or suggestions
woulud be great, I swear i probably tried everything

#include <stdio.h>

int main(void)
{

int PartNo[5];
float Price[5];
int QOH[5];
int ReorderPt[5];
int MinOrder[5];
int x[4];
int i;
int y;
FILE *Fp1;

Fp1 = fopen("data.txt", "r");
if ( Fp1 == NULL )
{
perror ( NULL );
}

for(y=0;y<5;y++)
{
for(i=0;i<4;i++)
fscanf(Fp1,"%d",&x[i]);
PartNo[y] = ((x[0]*1000)+(x[1]*100)+(x[2]*10)+x[3]);
}

for(y=0;y<5;y++)
{
for(i=0;i<4;i++)
fscanf(Fp1, "%f", &x[i]);
Price[y] = ((x[0]*1000)+(x[1]*100)+(x[2]*10)+x[3])/100;
}

for(y=0;y<5;y++)
{
for(i=0;i<4;i++)
fscanf(Fp1, "%d", &x[i]);
QOH[y] = ((x[0]*1000)+(x[1]*100)+(x[2]*10)+x[3]);
}

for(y=0;y<5;y++)
{
for(i=0;i<4;i++)
fscanf(Fp1, "%f", &x[i]);
ReorderPt[y] = ((x[0]*1000)+(x[1]*100)+(x[2]*10)+x[3]);
}

for(y=0;y<5;y++)
{
for(i=0;i<4;i++)
fscanf(Fp1, "%f", &x[i]);
MinOrder[y] = ((x[0]*1000)+(x[1]*100)+(x[2]*10)+x[3]);
}

for(y=0;y<5;y++)
{
if(QOH[y]<ReorderPt[y])
QOH[y] = QOH[y]+MinOrder[y];
}

printf("\n\b |-----------------------------------------------------------|");
printf("\n\b |Part Number --- Price --- Quantity on Hand --- Reorder
Point --- Minimum Order |");
printf("\n\b |-----------------------------------------------------------|");
printf("\n | %4d --- %2.2f --- %2d --- %2d |", PartNo[0], Price[0],
QOH[0], ReorderPt[0], MinOrder[0]);
printf("\n | %4d --- %2.2f --- %2d --- %2d |", PartNo[1], Price[1],
QOH[1], ReorderPt[1], MinOrder[1]);
printf("\n | %4d --- %2.2f --- %2d --- %2d |", PartNo[2], Price[2],
QOH[2], ReorderPt[2], MinOrder[2]);
printf("\n | %4d --- %2.2f --- %2d --- %2d |", PartNo[3], Price[3],
QOH[3], ReorderPt[3], MinOrder[3]);
printf("\n | %4d --- %2.2f --- %2d --- %2d |", PartNo[4], Price[4],
QOH[4], ReorderPt[4], MinOrder[4]);
printf("\n\b |-----------------------------------------------------------|");

return 0;
}
Nov 14 '05 #1
6 1751

"bas" <ba*****@cup.edu> a écrit dans le message de
news:d3**************************@posting.google.c om...
hey,
Hi,

I am having a lot of trouble trying to get this program to work
properly. It is supposed to read integers from a file and place them
into the categories for inventory. The problem seems to be that the
program will only read the first line of integers from the file, and
just print them into all the categories. Any help or suggestions
woulud be great, I swear i probably tried everything

#include <stdio.h>

int main(void)
{

int PartNo[5];
float Price[5];
int QOH[5];
int ReorderPt[5];
int MinOrder[5];
int x[4];
int i;
int y;
FILE *Fp1;

Fp1 = fopen("data.txt", "r");
if ( Fp1 == NULL )
{
perror ( NULL );
}
Be careful, perror() lets you to continue the program. fscanf might fail if
Fp1 is NULL.

[snipped]
for(y=0;y<5;y++)
{
for(i=0;i<4;i++)
fscanf(Fp1, "%f", &x[i]);
Be careful, the format specifier %f (float) is different from the type of an
x[i] (integer). It produces an undefined behavior.
Price[y] = ((x[0]*1000)+(x[1]*100)+(x[2]*10)+x[3])/100;


Price elements are floats, and it's a division between integers.

Price[y] = (float)((x[0]*1000)+(x[1]*100)+(x[2]*10)+x[3])/100.0;
[snipped]

Why not :

#include <stdio.h>
#incldue <stdlib.h>

int main(void)
{
int i, y, x[4];
int PartNo[5], QOH[5], ReorderPt[5], MinOrder[5];
float Price[5];

FILE *Fp1;

Fp1 = fopen("data.txt", "r");

if (Fp1==NULL)
{
perror()
exit(EXIT_FAILURE);
}

do
{
/* Get a line with 4 integers */
fscanf(Fp1,"%d%d%d%d",&x[0],&x[1],&x[2],&x[3]);
for(y=0; y<5; y++)
{
PartNo[y] = ((x[0]*1000)+(x[1]*100)+(x[2]*10)+x[3]);
/* Price elements are floats*/
Price[y] = (float)((x[0]*1000)+(x[1]*100)+(x[2]*10)+x[3])/100.0;
QOH[y] = ((x[0]*1000)+(x[1]*100)+(x[2]*10)+x[3]);
ReorderPt[y] = ((x[0]*1000)+(x[1]*100)+(x[2]*10)+x[3]);
MinOrder[y] = ((x[0]*1000)+(x[1]*100)+(x[2]*10)+x[3]);

if(QOH[y]<ReorderPt[y])
{
QOH[y] = QOH[y]+MinOrder[y];
}
}

/* Print the different values*/

} while(!feof(Fp1) && !ferror(Fp1));

fclose(Fp1);
return EXIT_SUCCESS;
}

Regis
Nov 14 '05 #2
> #incldue <stdlib.h>
#include <stdlib.h>
int main(void)
{
int i, y, x[4];
int PartNo[5], QOH[5], ReorderPt[5], MinOrder[5];
float Price[5];

FILE *Fp1;

Fp1 = fopen("data.txt", "r");

if (Fp1==NULL)
{
perror()
perror("data.txt");

exit(EXIT_FAILURE);
}

do
{
/* Get a line with 4 integers */
fscanf(Fp1,"%d%d%d%d",&x[0],&x[1],&x[2],&x[3]);
for(y=0; y<5; y++)
{
PartNo[y] = ((x[0]*1000)+(x[1]*100)+(x[2]*10)+x[3]);
/* Price elements are floats*/
Price[y] = (float)((x[0]*1000)+(x[1]*100)+(x[2]*10)+x[3])/100.0;
QOH[y] = ((x[0]*1000)+(x[1]*100)+(x[2]*10)+x[3]);
ReorderPt[y] = ((x[0]*1000)+(x[1]*100)+(x[2]*10)+x[3]);
MinOrder[y] = ((x[0]*1000)+(x[1]*100)+(x[2]*10)+x[3]);

if(QOH[y]<ReorderPt[y])
{
QOH[y] = QOH[y]+MinOrder[y];
}
}

/* Print the different values*/

} while(!feof(Fp1) && !ferror(Fp1));

fclose(Fp1);
return EXIT_SUCCESS;
}

Regis

Nov 14 '05 #3
In article <news:c6**********@news-reader3.wanadoo.fr>
Régis Troadec <re**@wanadoo.fr> writes:
while(!feof(Fp1) ...
This is virtually *guaranteed* to be wrong (about a 99.997% chance
in my experience, and the remaining .003% are just redundant :-) ).
Now, let us find out whether it is:
do {
/* Get a line with 4 integers */
fscanf(Fp1,"%d%d%d%d",&x[0],&x[1],&x[2],&x[3]);
[snippage of code that uses the values in x[]]
} while(!feof(Fp1) && !ferror(Fp1));


The comment, "a line with 4 integers", suggests that the input
stream that Fp1 will refer to consists (or should consist) of
lines like:

1 2 3 4
5 6 7 8
9 10 11 12

Assuming this is the case (and that there are only the three lines
of input), the first fscanf() will apply the four "%d" directives
in turn, successfully reading and converting "1", then " 2", then
" 3", then " 4". The newline after the 4 will remain in the input
stream. The snipped code will (one hopes; I did not look) do
something useful with the four integers. The tests for feof(Fp1)
and ferror(Fp1) will produce 0, as there was no error and there is
a newline in the stream ready to read.

The next fscanf() will again apply four "%d" directives, this time
reading and converting "\n5", " 6", " 7", and " 8" respectively,
and leave a newline in the stream. The feof() and ferror() tests
will behave as before.

The third fscanf() will likewise read and convert "\n9", " 10",
" 11", and "12", and leave a newline in the stream. The feof()
and ferror() tests will once again say "no EOF encountered, nor
any error".

The fourth fscanf() call will try to apply the first %d, and will
read the newline but then encounter EOF. Having failed to perform
any conversions, fscanf() will return EOF -- if it had done at least
one successful conversion it would return a positive number -- but
its return value will be ignored. The four values left over in
the x[] array from the previous fscanf() will still be there, and
the snipped code will do something with them. This time, however,
the feof() test will return a nonzero value, so the loop will stop.

In other words, the above code will successfully read all four
lines of a three-line file.

The moral of this story, as it were, is never to use feof() to
control a loop. The fscanf() call has a return value, and you
really have to inspect it in order to write reliable code.
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
Nov 14 '05 #4

"Chris Torek" <no****@torek.net> a écrit dans le message de
news:c6********@news3.newsguy.com...

Hi,
In article <news:c6**********@news-reader3.wanadoo.fr>
Régis Troadec <re**@wanadoo.fr> writes:
while(!feof(Fp1) ...
This is virtually *guaranteed* to be wrong (about a 99.997% chance
in my experience, and the remaining .003% are just redundant :-) ).
Now, let us find out whether it is:
do {
/* Get a line with 4 integers */
fscanf(Fp1,"%d%d%d%d",&x[0],&x[1],&x[2],&x[3]);


[snippage of code that uses the values in x[]]
} while(!feof(Fp1) && !ferror(Fp1));


Yes, I don't mastering formatted I/O in C, I quickly picked out this nasty
do-while loop using feof and ferror from my C99 draft (example 3,
§7.19.6.2).

[snipped the excellent informations about fscanf]
The moral of this story, as it were, is never to use feof() to
control a loop. The fscanf() call has a return value, and you
really have to inspect it in order to write reliable code.


You're perfectly right. Here is a better version to read 4 integers per line
:

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[])
{
FILE * fp;

int arr[4], cnt;

if(argc != 2)
{
fprintf(stderr, "Usage : "
"%s <fic>\n"
"Reads four ints per line from <fic>\n",
argv[0]);
exit(EXIT_FAILURE);
}

fp = fopen(argv[1],"r");

if(fp)
{
cnt = fscanf(fp, "%d%d%d%d\n",&arr[0], &arr[1],&arr[2],&arr[3]);
while(cnt==4)
{
/* Display the picked out values*/
printf("%d %d %d %d\n",arr[0], arr[1], arr[2], arr[3]);

/* Some processings here */

/* Reads the next line */
cnt = fscanf(fp, "%d%d%d%d\n",&arr[0], &arr[1],&arr[2],&arr[3]);
}

fclose(fp);
}

return EXIT_SUCCESS;
}

Regis
Nov 14 '05 #5
in comp.lang.c i read:
cnt = fscanf(fp, "%d%d%d%d\n",&arr[0], &arr[1],&arr[2],&arr[3]);
while(cnt==4)
{
/* Display the picked out values*/
printf("%d %d %d %d\n",arr[0], arr[1], arr[2], arr[3]);

/* Some processings here */

/* Reads the next line */
cnt = fscanf(fp, "%d%d%d%d\n",&arr[0], &arr[1],&arr[2],&arr[3]);
}


writing the fscanf twice is just asking for trouble, e.g., any tiny editing
mistake and they will work differently and you will probably only `see' the
first while you gnash your teeth debugging.

while (4 == (cnt = fscanf(...))
{
printf(...);
/* processing */
}

--
a signature
Nov 14 '05 #6
On Thu, 22 Apr 2004 14:08:26 +0200, "Régis Troadec" <re**@wanadoo.fr>
wrote:

"Chris Torek" <no****@torek.net> a écrit dans le message de
news:c6********@news3.newsguy.com... <snip>
Yes, I don't mastering formatted I/O in C, I quickly picked out this nasty
do-while loop using feof and ferror from my C99 draft (example 3,
§7.19.6.2).
(where the last operation on the subject file in the loop is
fscanf(stdin, "%*[^\n]"), whose return value cannot usefully be
checked unlike most *scanf calls)
[snipped the excellent informations about fscanf]
The moral of this story, as it were, is never to use feof() to
control a loop. The fscanf() call has a return value, and you
really have to inspect it in order to write reliable code.
You're perfectly right. Here is a better version to read 4 integers per line
:

<snip> cnt = fscanf(fp, "%d%d%d%d\n",&arr[0], &arr[1],&arr[2],&arr[3]);
while(cnt==4)
{ <snip> cnt = fscanf(fp, "%d%d%d%d\n",&arr[0], &arr[1],&arr[2],&arr[3]);
}


Note that \n in a *scanf format string doesn't match just a newline;
like any whitespace it matches *any and all* whitespace. For input
from a file like this case it can be OK, but for interactive prompted
input it makes the program appear to "hang"; FAQ 12.17.

- David.Thompson1 at worldnet.att.net
Nov 14 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: somaboy mx | last post by:
hi, I'm on winXPpro / apache 1.2 / php 4.4.x I'm experimenting with writing and reading from textfiles via php. I can create a file with fopen, write to it, but I'm having trouble reading...
3
by: Robert | last post by:
I am trying to read from a input data file and I am having trouble and ending up with another read at the eof that is garbage. I know that I shouldn't use while(!eof) but I am pre-loading my input...
7
by: laclac01 | last post by:
So I am converting some matlab code to C++. I am stuck at one part of the code. The matlab code uses fread() to read in to a vector a file. It's a binary file. The vector is made up of floats,...
1
by: bas | last post by:
hey, I am having a lot of trouble trying to get this program to work. It is supposed to read integers from a file and place them into the categories for inventory. The problem seems to be that...
1
by: Westbrook, Christopher L (WESTBCL04) | last post by:
I am having some trouble, and I can't figure out why. I'm trying to use the simple keylogger project from source forge, and then create a web interface to open the resulting files, so people from...
1
by: khaleel.alyasini | last post by:
anyone could point me out where did i do wrong? it seems that i can't get back the original Lena image after the IDCT(inverse discrete cosine transform) process. the output raw image is nothing...
2
by: Peter | last post by:
Hello, I am having alot of trouble reading in a date values from a text file which is exported from another application. My program is pretty basic, I read in values from a plain text file...
31
by: tophandasa | last post by:
Hi all, I'm having a trouble reading a binary file as float values.I have to read the data in binary mode, then read every four bytes into a float variable. I have done my search, but i found out...
0
by: Anish G | last post by:
Hi, I have an issue with reading CSV files. I am to reading CSV file and putting it in a Datatable in C#. I am using a regular expression to read the values. Below is the code. Now, it reads...
1
by: sachinkale123 | last post by:
Hi, I am reading excel file and reading values from that I am using provider As : "Provider=Microsoft.Jet.OLEDB.4.0;Data Source = " + Filename + ";Extended Properties=\"Excel 8.0;Hdr=No;IMEX=1\"";...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.