Thread: Reading a Structure from a file

  1. #1
    Registered User
    Join Date
    Dec 2012
    Posts
    4

    Reading a Structure from a file

    I am getting a segmentation fault and i know case 3 is wrong i just need help getting started on it i need to keep track what the use orders then once they are done print it to the console thx for the help.
    Code:
    #include<stdio.h>#include<stdlib.h>
    #define SIZE 25
    
    typedef struct
    {
    char name[SIZE];
    int id;
    int quan;
    float price;
    }BEER;
    
    BEER var[10];
    
    int main ()
    {
    FILE *f;
    f=fopen("beer.dat", "r");
    
    int numBeer,tempQuan,tempId,searchId,choice;
    int i,j,n,p,q,h;
    int searchCount=0;
    float tempPrice;
    char tempName[SIZE];
    
    
    
    fscanf(f,"%d",&numBeer);
    
    BEER *beer;
    beer=(BEER*)calloc(numBeer,sizeof(BEER));
    
      for(i=0;i<numBeer;i++)
        {
        fscanf(f,"%s%d%d%f",beer[i].name,&beer[i].id,&beer[i].quan,&beer[i].price);
        }
    
    
    printf("Would you like to:\n1.Search a beer by id number\n2.View the entire inventory\n3.Place an Order\n");
    
    scanf("%d",&choice);
    
     switch(choice)
        {
    
        case 1:
                printf("Please enter a beer id # you wish to search: ");
                scanf("%d",&searchId);
    
         for(j=0;j<numBeer;j++)
                {
            if(beer[j].id==searchId)
                {
             printf("\n%s\nName: %d\nID: $%d\nQuantity: %.2fPrice\n\n",beer[j].name,beer[j].id,beer[j].quan,beer[j].price);
                    searchCount++;
                    }
        }
    
             if(searchCount==0)
                     {
                    printf("Invalid Beer id\n");
                     }
                      break;
        case 2:
           for(n=0;n<numBeer;n++)
                {
                    for(p=0;p<numBeer;p++)
            {
                if(beer[n].price>beer[p].price)
                {
    
            strcpy(tempName,beer[n].name);
                    strcpy(beer[n].name,beer[p].name);
                    strcpy(beer[p].name,tempName);
    
            tempId=beer[n].id;
                    beer[n].id=beer[p].id;
                    beer[p].id=tempId;
    
            tempQuan=beer[n].quan;
            beer[n].quan=beer[p].quan;
            beer[p].quan=tempQuan;
                  
                    tempPrice=beer[n].price;
                    beer[n].price=beer[p].price;
                    beer[p].price=tempPrice;
              
            
                  
                }
            }
        }
    for(q=0;q<numBeer;q++)
    {
    printf("\n%s\nName: %d\nId: $%d\nQuantity: %.2fPrice\n\n",beer[q].name,beer[q].id,beer[q].quan,beer[q].price);
    }
    
                break;
    
        case 3:
         
          for(i=0;i<10;i++){
                 printf("Please enter a beer id # you wish to buy, if you are finished enter the # 1: ");
                scanf("%d",&searchId);
    
         if(searchId==1)break;
    
         for(j=0;j<numBeer;j++)
                {
            if(beer[j].id==searchId)
                {
            //var[10].name[25] = beer[j].name;
            var[10].id = beer[j].id;
            var[10].quan = beer[j].quan;
            var[10].price = beer[j].price;
            
                    searchCount++;
                    }
        }
    }        for(h=0;h<10;h++){
             printf("\n%s\nName: %d\nID: $%d\nQuantity: %.2fPrice\n\n",var[10].name,var[10].id,var[10].quan,var[10].price);
            }
             if(searchCount==0)
                     {
                    printf("Invalid Beer id\n");
                     }
                      break;
    
    
    
            default:
                printf("Please Enter Valid Input (1,2,3)\n");
        }
    
    
    fclose(f);
    
    
    return 0;
    }
    
    

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Welcome to the forum, ferro9ii!

    First, glad to see you used code tags around your code. But code tags can't work well, unless you paste your code in as "plain text". Otherwise, the benefit of code tags is diminished (no coloring, no line numbers, wrong font, etc.)

    Second, you appear to be using a mixture of spaces and tabs for your indentation on code, and that tears up the code like a dogs breakfast. Use one or the other (I have found 3 spaces best for all), and please be consistent.

    As you move forward in programming in C, you get a trained eye for standard idioms that work VERY well, IF the code is nicely indented.

    Like this - no, it's like wading through hip deep snow. Posts like this, get ignored, before long. Help us, help you.

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #define SIZE 25
    
    typedef struct
    {
       char name[SIZE];
       int id;
       int quan;
       float price;
    }BEER;
    
    BEER var[10];
    
    int main ()
    {
       FILE *f;
       f=fopen("beer.dat", "r");
    
       int numBeer,tempQuan,tempId,searchId,choice;
       int i,j,n,p,q,h;
       int searchCount=0;
       float tempPrice;
       char tempName[SIZE];
    
       fscanf(f,"%d",&numBeer);
       BEER *beer;
       beer=(BEER*)calloc(numBeer,sizeof(BEER));
    
       for(i=0;i<numBeer;i++)
       {
          fscanf(f,"%s%d%d%f",beer[i].name,&beer[i].id,&beer[i].quan,&beer[i].price);
       }
       printf("Would you like to:\n1.Search a beer by id number\n2.View the entire inventory\n3.Place an Order\n");
    
       scanf("%d",&choice);
    
       switch(choice)
       {
          case 1:
             printf("Please enter a beer id # you wish to search: ");
             scanf("%d",&searchId);
    
             for(j=0;j<numBeer;j++)
             {
                if(beer[j].id==searchId)
                {
                   printf("\n%s\nName: %d\nID: $%d\nQuantity: %.2fPrice\n\n", beer[j].name,beer[j].id,beer[j].quan,beer[j].price);
                   searchCount++;
                }
             }
    
             if(searchCount==0)
             {
                printf("Invalid Beer id\n");
             }
             break;
          case 2:
             for(n=0;n<numBeer;n++)
             {
                for(p=0;p<numBeer;p++)
                {
                   if(beer[n].price>beer[p].price)
                   {
                      strcpy(tempName,beer[n].name);
                      strcpy(beer[n].name,beer[p].name);
                      strcpy(beer[p].name,tempName);
    
                      tempId=beer[n].id;
                      beer[n].id=beer[p].id;
                      beer[p].id=tempId;
    
                      tempQuan=beer[n].quan;
                      beer[n].quan=beer[p].quan;
                      beer[p].quan=tempQuan;
                  
                      tempPrice=beer[n].price;
                      beer[n].price=beer[p].price;
                      beer[p].price=tempPrice;
                   }
                }
             }
             for(q=0;q<numBeer;q++)
             {
                printf("\n%s\nName: %d\nId: $%d\nQuantity: %.2fPrice\n\n",  beer[q].name,beer[q].id,beer[q].quan,beer[q].price);
             }
             break;
    
          case 3:
             for(i=0;i<10;i++){
                printf("Please enter a beer id # you wish to buy, if you are finished enter the # 1: ");
                scanf("%d",&searchId);
    
                if(searchId==1)break;
    
                for(j=0;j<numBeer;j++)
                {
                   if(beer[j].id==searchId)
                   {
                      //var[10].name[25] = beer[j].name;
                      var[10].id = beer[j].id;
                      var[10].quan = beer[j].quan;
                      var[10].price = beer[j].price;
            
                      searchCount++;
                   }
                }
             }        
             for(h=0;h<10;h++){
                printf("\n%s\nName: %d\nID: $%d\nQuantity: %.2fPrice\n\n",var[10].name,var[10].id,var[10].quan,var[10].price);
             }
             if(searchCount==0)
             {
                printf("Invalid Beer id\n");
             }
             break;
    
          default:
             printf("Please Enter Valid Input (1,2,3)\n");
       }
       fclose(f);
    
       return 0;
    }
    This is a one-time reformat. After this, it's up to you.

  3. #3
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    Yes there is a problem in case 3:
    Code:
    var[10].id = beer[j].id;
    var[10].quan = beer[j].quan;
    var[10].price = beer[j].price;
    You declared var to be an array of 10 elements. You can only access using index 0 to 9. 10 would be out of range.

  4. #4
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Also, in the future, if your program is accessing files (like "beer.dat"), you should provide [at least some of] the contents of that file.

    Quote Originally Posted by Adak View Post
    ...like a dogs breakfast.
    About a year ago, I started referring to this as "barkfast."

  5. #5
    Registered User
    Join Date
    Dec 2012
    Posts
    4
    thx for the feedback

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    So line 99 should be:
    Code:
    strcpy(var[i].name, beer[j].name);
    And all the assignments following it should also go to var[i].Something, instead of var[10].Something.

    And in order to use strcpy(), you need to add the header file

    #include <string.h>

    to your program.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading text from file using structure.
    By shawney in forum C Programming
    Replies: 2
    Last Post: 02-07-2012, 02:58 AM
  2. Structure and reading from a file
    By acmarshall in forum C++ Programming
    Replies: 6
    Last Post: 12-12-2011, 03:40 PM
  3. Reading text file into a structure
    By jb1989 in forum C Programming
    Replies: 1
    Last Post: 12-13-2010, 12:17 PM
  4. Reading structure from file
    By nime in forum C++ Programming
    Replies: 7
    Last Post: 03-21-2010, 04:17 PM
  5. reading structure from file..??
    By CyC|OpS in forum C Programming
    Replies: 5
    Last Post: 04-07-2009, 05:28 AM

Tags for this Thread