473,473 Members | 1,504 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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 1761

"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\"";...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.