Why can't i get info from the text file correctly?Plz help me ASAP:(

This is a discussion on Why can't i get info from the text file correctly?Plz help me ASAP:( within the C Programming forums, part of the General Programming Boards category; I'm working on my assignment, and my deadline is tomorrow, i have tried again and again so many times but ...

  1. #1
    Registered User
    Join Date
    Dec 2013
    Posts
    2

    Why can't i get info from the text file correctly?Plz help me ASAP:(

    I'm working on my assignment, and my deadline is tomorrow, i have tried again and again so many times but the info gotten from the text file are all wrong, i can't find any problem with my code, please help me
    Code:
    #include<stdio.h>
    FILE *f;
    typedef struct prd
        {
            char name[30];
            char code[30];
            char idt[30];
            float price;
        }prd;
        
    void print(int i)
        {int ct=1;
        char line[500];
         f=fopen("product.txt","r+");
         prd product[i];
         printf("pr: %s\n",product[ct].name);
         
         {
         fscanf(f,"%s %s %s %f",product[ct].name,product[ct].code,product[ct].idt,&product[ct].price);}
                            
         {
            printf("%s\t",product[ct].name);
    
            printf("%s\t", product[ct].code);
            
            printf("%s\t", product[ct].idt);
    
            printf("%0.2f\n", product[ct].price);
            }
        getch();
        }
    int main()
    {
        int i,j;
        char sp;
        
        f=fopen("product.txt","w");
        printf("Input number of products: ");scanf("%d",&i);
        struct prd product[i];
    
        printf("Please input information of these products");
        for (j = 0;j<i;j++)
        {
    
            printf("\nProduct #%d:\n",j+1);
            
            printf("Name:");
            scanf("%s",&product[j].name);
            
            printf("Code:");
            scanf("%s",&product[j].code);
            printf("Id:");
            scanf("%s",&product[j].idt);
    
            printf("Price:");
            scanf("%f", &product[j].price);
            sp=getchar();
        }
        for (j = 0;j<i;j++)
        {
            fprintf(f,"%s\t",product[j].name);
    
            fprintf(f,"%s\t", product[j].code);
            
            fprintf(f,"%s\t", product[j].idt);
    
            fprintf(f,"%0.2f\n", product[j].price);
        }
        print(i);
    }

  2. #2
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    You shouldn't have the ampersands in front of the string members of product in your scanf's.


    You need to close the file in main before you call print (which opens the file and should also close it before it returns).


    f shouldn't be global. Define it separately in both functions.


    You shouldn't use r+ in fopen if you just want to read the file. Just use r.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You probably want to close product.txt before you try to open it again. Unlike writing to the screen (which generally happens at every newline), writing to a file generally doesn't happen until you have a lot (a lot) of characters queued up (at least a KB or ten), or you explicitly flush (or close) the file.

    Three of your four scanfs are incorrect (specifically the first three), as they have an extraneous & character.

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,311
    Mindreading is not a common skill among forum members. You might want to read this link before posting again.

    What data is being input? What is being output?

    You may see nothing wrong with your code. I see multiple problems. I'll give a hint about two significant categories of error your code exhibits (multiple times).

    Why isn't your code checking whether operations (opening files, reading data, writing data, etc) are succeeding? In your code, if any operation fails, most subsequent operations will also fail. Failing to open a file prevents any input or output operations on that file. Failure to read data will mean subsequent output of that data will not be as expected.

    Your code opens the file "product.txt" twice (once for writing, once for reading). It is never closed. A file has to be closed before it can be reopened (unless some means of file sharing is active, which your code hasn't activated.
    Right 98% of the time, and don't care about the other 3%.

  5. #5
    Registered User
    Join Date
    Dec 2013
    Posts
    2
    Quote Originally Posted by grumpy View Post
    Mindreading is not a common skill among forum members. You might want to read this link before posting again.

    What data is being input? What is being output?

    You may see nothing wrong with your code. I see multiple problems. I'll give a hint about two significant categories of error your code exhibits (multiple times).

    Why isn't your code checking whether operations (opening files, reading data, writing data, etc) are succeeding? In your code, if any operation fails, most subsequent operations will also fail. Failing to open a file prevents any input or output operations on that file. Failure to read data will mean subsequent output of that data will not be as expected.

    Your code opens the file "product.txt" twice (once for writing, once for reading). It is never closed. A file has to be closed before it can be reopened (unless some means of file sharing is active, which your code hasn't activated.
    Thanks, i'll keep that in mind. btw, this is just a prototype of my code so i haven't added the codes to check for succeeded operations. I have followed your hints and got the code to work, however, i had another problem. Could you take a look at this for me? Why can't I get the sort function to work
    Code:
    #include<stdio.h>
    typedef struct prd
        {
            char name[30];
            char code[30];
            char idt[30];
            float price;
        }prd,temp;
        void write();
        void sort();
        int i,j;
        void searchcode();
        void searchname();
        
    int main()
    {FILE *f;
        f=fopen("product.txt","w");
        printf("Input number of products: ");scanf("%d",&i);
        struct prd product[i];
    
        printf("Please input information of these products");
        for (j = 0;j<i;j++)
        {
    
            printf("\nProduct #%d:\n",j+1);
            
            printf("Name:");
            scanf("%s",product[j].name);
            
            printf("Code:");
            scanf("%s",product[j].code);
            printf("Id:");
            scanf("%s",product[j].idt);
    
            printf("Price:");
            scanf("%f", &product[j].price);
        }
       fprintf(f,"NAME\tCODE\tID\tPRICE\n");
        for (j = 0;j<i;j++)
        {
            fprintf(f,"%s\t",product[j].name);
    
            fprintf(f,"%s\t", product[j].code);
            
            fprintf(f,"%s\t", product[j].idt);
    
            fprintf(f,"%0.2f\n", product[j].price);
        }
        fclose(f);
        sort();
    }
    
        void sort()
        {
        FILE *f;
        int k;
        char line[500];
         f=fopen("product.txt","r+");
         prd product[i];
         fscanf(f,"%[^\n]\n",line);
        for(j=0;j<i;j++)
        {
         fscanf(f,"%s %s %s %f",product[j].name,product[j].code,product[j].idt,&product[j].price);}
         printf("name %s\n",product[j].name);
          for (j=0;j<i-1;j++)
          {
              for (k=j+1;k<i;k++)
              {
                  if (product[j].price<product[k].price)
                                       {
                                        struct prd temp=product[j];
                                        product[j]=product[k];
                                        product[k]=temp;
                                       }}}
               for (j=0;j<i;j++)
               {
                   fprintf(f,"%s\t",product[j].name);
    
                   fprintf(f,"%s\t", product[j].code);
            
                   fprintf(f,"%s\t", product[j].idt);
    
                   fprintf(f,"%0.2f\n", product[j].price);}
            fclose(f);
        getch();
        }

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Looking at the sort function.

    Code:
    void sort()
    {
        FILE *f;
        int k;
        char line[500];
         f=fopen("product.txt","r+");
             
         prd product[i];
         fscanf(f,"%[^\n]\n",line);
     
    
       for(j=0;j<i;j++)
        {
           fscanf(f,"%s %s %s %f", product[j].name,product[j].code, 
            product[j].idt, &product[j].price);
            printf("name %s\n",product[j].name);
        }
    
    
    //Now the sort - it's a Substitution sort.
          for (j=0;j<i-1;j++)
          {
              for (k=j+1;k<i;k++)
              {
                  if (product[j].price<product[k].price)
                  {
    /*older compilers may not allow variable declarations after the 
    first block of code. If so, declare this variable at the top of this 
    function, and then you can use it here.  Newer compilers will allow this. 
    */          
                ---> struct prd temp=product[j]; 
                       product[j]=product[k];
                       product[k]=temp;
                    }                   //}}}  //THIS SUCKS EGGS! :(
               }
          } 
          for (j=0;j<i;j++)
          {
               fprintf(f,"%s\t",product[j].name);
               fprintf(f,"%s\t", product[j].code);
             
               fprintf(f,"%s\t", product[j].idt);
               fprintf(f,"%0.2f\n", product[j].price); // NOT HERE! -->}
          }  // <------- Here!
          fclose(f);
          getch();
    }
    In main(), you get the size of the product array, with this line of code.
    Code:
    scanf("%d",&i);
    struct prd product[i];
    This is something that most compilers won't allow. Are you getting no warnings or errors from this?

  7. #7
    Registered User
    Join Date
    Jun 2005
    Posts
    6,311
    VLA's were introduced in the 1999 C standard, Adak.
    Right 98% of the time, and don't care about the other 3%.

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by grumpy View Post
    VLA's were introduced in the 1999 C standard, Adak.
    I knew it was in the standard, but didn't know if his compiler supported it. In fact, since I wasn't in the habit of using it, I didn't know that my compiler did support it. Sweet!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Not reading a file correctly?
    By tmac619619 in forum C Programming
    Replies: 1
    Last Post: 11-13-2012, 01:10 AM
  2. Text Calculator, first program, not working correctly
    By jacob199651 in forum C Programming
    Replies: 2
    Last Post: 04-19-2011, 04:41 PM
  3. file not opening correctly
    By scwizzo in forum C++ Programming
    Replies: 7
    Last Post: 04-04-2007, 01:23 PM
  4. File I/O not working correctly
    By gL_nEwB in forum C++ Programming
    Replies: 4
    Last Post: 05-27-2006, 10:29 PM
  5. Reading info from a text document.
    By twomers in forum C++ Programming
    Replies: 2
    Last Post: 12-13-2005, 10:53 AM

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