trouble reading a file

This is a discussion on trouble reading a file within the C Programming forums, part of the General Programming Boards category; hey, I am having a lot of trouble trying to get this program to work. It is supposed to read ...

  1. #1
    Registered User
    Join Date
    Apr 2004
    Posts
    4

    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;
    }

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >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.
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Apr 2004
    Posts
    4
    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.

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >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.

  5. #5
    Registered User
    Join Date
    Apr 2004
    Posts
    4
    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

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    You might want to post your lastest code, preferably with code tags, so it's readable.

  7. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >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.
    My best code is written with the delete key.

  8. #8
    Registered User
    Join Date
    Apr 2004
    Posts
    4
    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
    Attached Files Attached Files

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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.
    Hope is the first step on the road to disappointment.

  10. #10
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,156
    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
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  11. #11
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >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.
    Last edited by swoopy; 04-21-2004 at 07:22 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Formatting a text file...
    By dagorsul in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 03:53 AM
  2. having trouble reading into Struct from File
    By bcianfrocca in forum C++ Programming
    Replies: 9
    Last Post: 09-06-2005, 10:54 PM
  3. Simple File encryption
    By caroundw5h in forum C Programming
    Replies: 2
    Last Post: 10-13-2004, 10:51 PM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21