-
trouble reading a file
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. I think the problem is that the program is not actually reading from the file, but I have no idea how to fix it. 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");
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;
}
-
>I am having a lot of trouble trying to get this program to work.
What is it doing wrong? "It doesn't work" isn't likely to get you helpful answers.
>Fp1 = fopen("data.txt", "r");
When working with files, the first problem people encounter is a failure of fopen. Be sure to check for this and print out a useful diagnostic:
Code:
Fp1 = fopen("data.txt", "r");
if ( Fp1 == NULL ) {
perror ( NULL );
}
>fscanf(Fp1,"%d",&x[i]");
This might be a typo, but that last double quote shouldn't be there.
-
the program runs, but for some reason it just prints the first number in the text file i am reading from in every field. it seems to me like it is not reading the whole file and just reading the first line of it.
-
>fscanf(Fp1,"%d",&x[i]");
As Prelude said, this won't compile.
>fscanf(Fp1, "%f", &x[i]);
Here you're reading a float value, but x is an int.
-
thanks for the help guys.
>fscanf(Fp1,"%d",&x[i]");
that was just a typo
as far as everthing else goes. It is still printing the same value for all the categories :(
-
You might want to post your lastest code, preferably with code tags, so it's readable.
-
>It is still printing the same value for all the categories
If you post the file in question then I might be able to develop enough interest to look harder at your code.
-
sorry about the code tags.
and for the data.txt file that it reads these are the numbers in the file:
0123123232020
0234234345025
34563456565010
456745677105
5678678757525
i also attatched the txt file.
Code:
#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, "%d", &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, "%d", &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;
}
once again thanks for the help
-
I'll assume you're trying to read one single digit at a time into your variables, correct? As such, you shouldn't be using "%d", because it's going to try to read the entire number in one shot. (Because in a nutshell, that's what it does. It reads until it runs into something that isn't a number.)
Try using fgetc or perhaps just %c and then converting that character into a decimal digit by subtracting '0' from it.
Quzah.
-
The idea behind code tags is to preserve your formatting (indenting specifically). Since your source code isn't indented, code tags won't help. Indent your code using a standarf format and try again.
Don't forget to use the PREVIEW button to see if your post looks correct
-
>0123123232020
>0234234345025
>34563456565010
>456745677105
>5678678757525
Is each field a certain width? Or is there any way to tell where the PartNo ends, and the Price begins? If you know how wide each field is, then you can do what Quzah suggested. But just looking at it, you can't tell. It look like you're using 4 for a width of each field, but there's not enough character per line for four fields.