How to read a txt file into an array

This is a discussion on How to read a txt file into an array within the C Programming forums, part of the General Programming Boards category; Code: #include <stdio.h> #include <string.h> #include <sys/types.h> #include <stdio.h> struct item { int ID; char name[64]; int price; }; int ...

  1. #46
    Banned
    Join Date
    Aug 2009
    Posts
    30
    Code:
    #include <stdio.h> 
    #include <string.h>
    #include <sys/types.h>
    #include <stdio.h>
    
    
    struct item
    {
               int ID;
               char name[64];
               int price;
    };
    int main(int argc, char *argv[])
    {
               FILE *fp;
               char retstr[BUFSIZ];
               int *f1;
    	   char *f2; 
    	   int *f3;
    	   char *p;
               int max_cnt=15;
               int i, cnt=0;
               int menu_choice;
    	   int cheapest_item;
    	   int discount;
    	   int x;
    	   int q;
    	   int d;
    
          	   struct item record [10];
    	  
    	 
    	    fp=fopen("C://shopping.txt","r");
    		   
    	    if(fp==NULL)
    	   {
               printf("Error: can't open file.\n");
    		   
    	   exit(1);
    	    }
    
    
    		   
               
    	   while( fgets(retstr, BUFSIZ, fp) != NULL )
               {
    
    	    if ((p = strchr(retstr, '\n')) != NULL)
    	    *p = '\0';
    	    if(cnt == max_cnt)
    	    break;
    
                f1=strtok(retstr, " ");
    	    f2=strtok(NULL, " ");
    	    f3=strtok(NULL, " ");
    	    strcpy(record[cnt].ID, f1);
    	    strcpy(record[cnt].name, f2);
    	    strcpy(record[cnt].price, f3);
    
    	    cnt++;
                }
    
             
    
    
              scanf("%d",&menu_choice);
              i=0; 
    
    	  switch (menu_choice){ 
              
              case 1: 
    	  for(i=0; i < cnt; i++) 	
    	  { 
    
              printf("%d %s %d\n", record[i].ID, record[i].name,record[i].price); 
    	
              }
              break;
    
              case 2:     
              cheapest_item = record[i].price;
    	  for(i=0; i < cnt; i++)
    	  {
    		
              if (record[i].price < cheapest_item)
    	  {
           	  printf("%s %d\n",record[i].name,record[i].price);
    	  }
    				
              break;
    
              case 3:
    	  discount = (record[i].price) - ((float)0.2 *(record[i].price)); 
    
    	  for(i=0; i<cnt; i++)
    	 {
    	  printf("%s %d\n",record[i].name,discount);
    	  }
    	  break;
    
    			
    
              case 4: 
    			      
              printf( "choose item for purchase\n" );
    				  
    	  for (i=0;i<10;i++)
    	  {
    				  
    	              
    	  printf("Enter a number: ");
    				  
    	  scanf("%d",&record[i].name);
    	      
              printf("choice %d - %s\n",i+1,record[i].name);
    
    				  
    	  printf("Enter quantity for item\n");
    	  scanf("%d",&q);
    				  
    	  printf("You entered %d %d %\n",x,q);	
    
    				  
    	  printf("Do you want to choose more items? Yes[1] No[2]");
    	  scanf("%d",&d);	 
    
              if((d==1));
    	  {
    	  printf("Enter a number: ");
    				  
    	  scanf("%d",&record[i].name);
    
    	  printf("choice %d - %s\n",i+1,record[i].name);
    	  }
    		  
    		  
    	      
    	if((d==0));
    	break;
    		  
                      
    	case 5: printf("total price for items\n" );
    	break;
                      
    	case 6:  printf( "Exit\n" );
                      
    	break;
    
            fclose(fp);
            return 0;
    }
    ok I made changes to my code. and I have some warnings about types, functions etc.

    I am mostly concernced about writing the code fore case 5, I am have no clue to go about it. And if there are still errors plz point them out
    Last edited by Hitsugaya_KK; 08-21-2009 at 10:32 AM.

  2. #47
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Hitsugaya_KK View Post
    I am mostly concernced about writing the code fore case 5, I am have no clue to go about it. And if there are still errors plz point them out
    I am mostly concerned about your lack of indentation, which makes the code more awkward to read. You have already had this explained several times in this thread. If you want other people to look over your code for you, why don't you make an effort to make it properly readable?
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #48
    Banned
    Join Date
    Aug 2009
    Posts
    30
    I have only one problem now regarding case 4, I don't have any problem with the reset of my code

    Code:
    case 4: //allows user to choose items
                       printf("do you want to add choose item Y[1] N[0]");
    		   scanf("%d",&input);
    		   if(input==1)
    		   {
    
    		  
    		  for (u=0;u<11;u++)
    		  {
    		  printf( "choose item for purchase\n" );
    		  scanf("%d",&u);
    		  printf("do you want to add choose item Y[1] N[0]");
    		  scanf("%d",&input);
    
    		  printf("%d %s %f\n", record[u].ID, record[u].name,record[u].price);
    		  }
    		  continue;
    		  }
    		  
    
    
    		  else if(input==0){
    		  break;
    		  }
    It keeps on asking me to add items and never stops, I want to know how to make it stop if I want it to . the item choosing works fine now, but I haven't found a way to stop the infinite process.
    Last edited by Hitsugaya_KK; 08-22-2009 at 12:25 PM.

  4. #49
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    HK, you have a few problems you haven't addressed, still.

    BEFORE you go on and code up anything else, you need to fix the problems that your compiler is telling you.

    You should be checking your code for warnings or errors from your compiler, after every few lines of code. Yes, it takes longer, but it's much shorter overall, because you aren't left with a program that has 40, 80 or 100 lines, and the damn thing will not compile.

    If you don't want to follow the advice about this, and about indenting your code like a sane person - well, that's your choice.

    Life is short, and we're all kinda busy.

  5. #50
    Banned
    Join Date
    Aug 2009
    Posts
    30
    -_-. I have already fixed my problem. My whole code works fine now. I have been wasting time not working on my code much thinking someone could help me, but I had to be at everone's mercy by trying to indent my code :/. Meanwhile, I am a newbie, so I don't have a good indentation style to begin with.

Page 4 of 4 FirstFirst 1234
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie homework help
    By fossage in forum C Programming
    Replies: 3
    Last Post: 04-30-2009, 04:27 PM
  2. opening empty file causes access violation
    By trevordunstan in forum C Programming
    Replies: 10
    Last Post: 10-21-2008, 11:19 PM
  3. Replies: 3
    Last Post: 03-04-2005, 01:46 PM
  4. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM

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