Thread: How to read a txt file into an array

  1. #16
    Registered User
    Join Date
    Mar 2009
    Posts
    37
    Quote Originally Posted by MK27 View Post
    I hate to point this out but perhaps you have fallen a little behind in the work for this course? In your last post there is an unclosed curly brace, part of the struct, which will cause you a problem..
    As MK27 said the Struct is not properly defined, missing curly braket at the end

    Quote Originally Posted by MK27 View Post
    The link you posted is fine or appears to be at first glance. One thing you will probably want to do is define the struct *outside* of main() since you will be using it in your other functions as well.
    The main function is think is not now defined correctly can you confirm this MK27?

  2. #17
    Banned
    Join Date
    Aug 2009
    Posts
    30
    ok I have added the curly bracket. so what should I do next? I have tried to figure out how to read the ID as the main part of each line. wow I noticed I get so many errors with the latest code I have

  3. #18
    Banned
    Join Date
    Aug 2009
    Posts
    30
    strange, when I try to build my code, I get this

    Code:
    // cstdio standard header
    #pragma once
    #ifndef _CSTDIO_
    #define _CSTDIO_
    #include <yvals.h>
    
    #ifdef _STD_USING
     #undef _STD_USING
      #include <stdio.h>
     #define _STD_USING
    
    #else /* _STD_USING */
     #include <stdio.h>
    #endif /* _STD_USING */
    
     #define _HAS_CONVENTIONAL_CLIB	1
    
     #define _IOBASE	_base
     #define _IOPTR	_ptr
     #define _IOCNT	_cnt
    
    #ifndef RC_INVOKED
     #if _GLOBAL_USING
    _STD_BEGIN
    using ::size_t; using ::fpos_t; using ::FILE;
    using ::clearerr; using ::fclose; using ::feof;
    using ::ferror; using ::fflush; using ::fgetc;
    using ::fgetpos; using ::fgets; using ::fopen;
    using ::fprintf; using ::fputc; using ::fputs;
    using ::fread; using ::freopen; using ::fscanf;
    using ::fseek; using ::fsetpos; using ::ftell;
    using ::fwrite; using ::getc; using ::getchar;
    using ::gets; using ::perror;
    using ::putc; using ::putchar;
    using ::printf; using ::puts; using ::remove;
    using ::rename; using ::rewind; using ::scanf;
    using ::setbuf; using ::setvbuf; using ::sprintf;
    using ::sscanf; using ::tmpfile; using ::tmpnam;
    using ::ungetc; using ::vfprintf; using ::vprintf;
    using ::vsprintf;
    _STD_END
     #endif /* _GLOBAL_USING */
    #endif /* RC_INVOKED */
    
    #ifndef _Filet
     #define _Filet	FILE
    #endif /* _Filet */
    
    #ifndef _FPOSOFF
      #define _FPOSOFF(fp)  ((long)(fp))
    #endif /* _FPOSOFF */
    
    #endif /* _CSTDIO_ */
    
    /*
     * Copyright (c) 1992-2005 by P.J. Plauger.  ALL RIGHTS RESERVED.
     * Consult your license regarding permissions and restrictions.
     V4.05:0009 */
    seems I didn't have to include two function and that's what brought up this code. I so what do I do now to proceed? here is the latest code

    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];
    char *f1, *f2, *f3, *p;
    int max_cnt=15;
    int i, cnt=0;
    
    
    
    
    	  struct item record [50]
    	  
    	 
    		if ((fp=fopen("C://shoppinglist.txt","r");
        printf("Error: can't open file.\n");    
        exit(1);
    }
    
    		while (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++;
    			}
    for(i=0; i < cnt; i++)
    printf("%d %s %d\n", record[i].ID, record[i].name,record[i].price);
    
    fclose(fp);
    exit(0);
    }
    printf( "1 - view the complete list of goods in the shop, with their prices" );
    printf( "2 – list the cheapest item on offer" );
    printf( "3 – “discount” option" );
    printf( "4 – choose items for purchase" );
    printf( "5 – total price for items" );
    printf( "6 - Exit");
    
    
    
    	
    	
    
        switch ( option ) {
    		case 1:           
    
            case 2:          
                
                break;
            case 3:         
      
            case 4:        
                printf( "choose item for purchase\n" );
    			
                break;
            case 5:            
                printf( "total price for items\n" );
                break;
    		case 6:
    			printf( "Exit\n" );
    
    
    
        }
        return 0;
    Last edited by Hitsugaya_KK; 08-19-2009 at 03:39 AM.

  4. #19
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Again I see some missing braces, so this code will not compile properly. You should be trying to at least compile your code without errors every few lines even if it does not produce output or anything.
    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

  5. #20
    Banned
    Join Date
    Aug 2009
    Posts
    30
    this is the latest:
    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];
    char *f1, *f2, *f3, *p;
    int max_cnt=15;
    int i, cnt=0;
    
    
    
    
    	  struct item record [50];
    	  
    	 
    		if ((fp=fopen("C://shoppinglist.txt","r");
    	}
        printf("Error: can't open file.\n");   
    	{
        exit(1);
            }
    	
    		while (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++;
    }
    for(i=0; i < cnt; i++)
    printf("%d %s %d\n", record[i].ID, record[i].name,record[i].price);
    
    fclose(fp);
    exit(0);
    }
    printf( "1 - view the complete list of goods in the shop, with their prices" );
    printf( "2 – list the cheapest item on offer" );
    printf( "3 – “discount” option" );
    printf( "4 – choose items for purchase" );
    printf( "5 – total price for items" );
    printf( "6 - Exit");
    
    
    
    	
    	
    
        switch ( option ) {
    		case 1:           
    
            case 2:          
                
                break;
            case 3:         
      
            case 4:        
                printf( "choose item for purchase\n" );
    			
                break;
            case 5:            
                printf( "total price for items\n" );
                break;
    		case 6:
    			printf( "Exit\n" );
    
    
    
        }
        return 0;
    finding it hard to get read of the errors :/

  6. #21
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Hitsugaya_KK
    finding it hard to get read of the errors :/
    You need to indent your code properly. For example:
    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];
        char *f1, *f2, *f3, *p;
        int max_cnt=15;
        int i, cnt=0;
    
        struct item record [50];
    
        if ((fp=fopen("C://shoppinglist.txt","r");
    }
    
    
    printf("Error: can't open file.\n");
    
    {
        exit(1);
    }
    
    while (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++;
    }
    
    for(i=0; i < cnt; i++)
        printf("%d %s %d\n", record[i].ID, record[i].name,record[i].price);
    
    fclose(fp);
    exit(0);
    }
    
    printf( "1 - view the complete list of goods in the shop, with their prices" );
    printf( "2 – list the cheapest item on offer" );
    printf( "3 – “discount” option" );
    printf( "4 – choose items for purchase" );
    printf( "5 – total price for items" );
    printf( "6 - Exit");
    
    switch ( option ) {
    case 1:
    case 2:
        break;
    case 3:
    case 4:
        printf( "choose item for purchase\n" );
        break;
    case 5:
        printf( "total price for items\n" );
        break;
    case 6:
        printf( "Exit\n" );
    }
    
    return 0;
    It is now obvious that you have code that should be in a function, presumably the main function, hanging in the middle of nowhere.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #22
    Banned
    Join Date
    Aug 2009
    Posts
    30
    this is what I have done so far:
    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];
               char *f1, *f2, *f3, *p;
               int max_cnt=15;
               int i, cnt=0;
    
    
    
    
    	   struct item record [10];
    	  
    	 
    	   if (fp=fopen("C://shoppinglist.txt","r"));
    }
    
    
               printf("Error: can't open file.\n");
    
    
    {
               exit(1);
    }
    	
    while   (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++;
    }
    
    
    for(i=0; i < cnt; i++)
             printf("%d %s %d\n", record[i].ID, record[i].name,record[i].price);
    
    fclose(fp);
    exit(0);
    }
    
              printf( "1 - view the complete list of goods in the shop, with their prices" );
              printf( "2 – list the cheapest item on offer" );
              printf( "3 – “discount” option" );
              printf( "4 – choose items for purchase" );
              printf( "5 – total price for items" );
              printf( "6 - Exit");
    
    
    
    	
    	
    
              switch ( option )
    
                         {
    
                          case 1:         
      
    
                          case 2:          
                                      break;
    
                          case 3:
             
      
                          case 4: printf( "choose item for purchase\n" );			
                                      break;
    
                          case 5: printf( "total price for items\n" );
                                      break;
    
                          case 6: printf( "Exit\n" );
    
    
                         }
    
                          return 0;
    }
    please can you help me point out the code?
    Last edited by Hitsugaya_KK; 08-19-2009 at 04:46 AM.

  8. #23
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Hitsugaya_KK
    please can you help me point out the code?
    Yes, I can point out mistakes in your code, but I refuse to do so until you indent it properly. I have already given you an example. If you ignore my advice, why should I give you more advice?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #24
    Banned
    Join Date
    Aug 2009
    Posts
    30
    ok I edited the code. will it do? I am sorry . ok I made final adjustments. I hope someone can help me in identifying my errors
    Last edited by Hitsugaya_KK; 08-19-2009 at 04:43 AM.

  10. #25
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    You're messing up your bracketing, which messes up program flow and your indenting. When it comes to indenting you might not care but the program flow matters. You want to ensure proper, top-down execution by opening and closing all your statement blocks. So like people have been saying you need to fix that. It's a good idea for beginners to follow a simple rule - match your brackets, then fill in the statements.

    First you type:
    Code:
    if()
    {
    }
    Let's fill in that if condition.
    Code:
    if( fp != NULL ) 
    {
    }
    Add a while loop.
    Code:
    if( fp != NULL ) 
    {
       char retstr[BUFSIZ];
       while( )
       {
       }
       fclose(fp);
    }
    Code:
    if( fp != NULL ) 
    {
       char retstr[BUFSIZ];
       while( fgets(retstr, BUFSIZ, fp) != NULL )
       {
           /* strtok stuff.... */
       }
       fclose(fp);
    }
    Savvy? Get to work. Start over.
    Last edited by whiteflags; 08-19-2009 at 06:01 AM.

  11. #26
    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];
               char *f1, *f2, *f3, *p;
               int max_cnt=15;
               int i, cnt=0;
    
    
    
    
    	   struct item record [10];
    	  
    	 
    	   if (fp=fopen("C://shoppinglist.txt","r"));
    	   {
    
    
    printf("Error: can't open file.\n");
    
    
    	   }
          exit(1);
    }
    	
    
    if( fp != NULL ) 
    {
       char retstr, [BUFSIZ];
       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++;
       }
    
    
    for(i=0; i < cnt; i++)
    {
    
    
          printf("%d %s %d\n", record[i].ID, record[i].name,record[i].price);
      
    }
    
    fclose(fp);
    
     
    exit(0);
    }
    done what I can . I have 1 error in my program. And the error is "Error 10 error C2059: syntax error : 'if'". how do I go about it? it is referring to this "if( fp != NULL )".

  12. #27
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Hitsugaya_KK
    I have 1 error in my program. And the error is "Error 10 error C2059: syntax error : 'if'". how do I go about it? it is referring to this "if( fp != NULL )".
    This is your main function:
    Code:
    int main(int argc, char *argv[])
    {
        FILE *fp;
        char retstr[BUFSIZ];
        char *f1, *f2, *f3, *p;
        int max_cnt=15;
        int i, cnt=0;
    
        struct item record [10];
    
        if (fp=fopen("C://shoppinglist.txt","r"));
        {
            printf("Error: can't open file.\n");
        }
        exit(1);
    }
    The line that the compiler is complaining about is outside of the main function, where it should not be. Notice that I indented all the code within the main function. As such, it is easy for me to see when the main function ends, thus I can confidentally tell you what I just told you.

    By the way, you have an extra semi-colon on this line:
    Code:
    if (fp=fopen("C://shoppinglist.txt","r"));
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  13. #28
    Banned
    Join Date
    Aug 2009
    Posts
    30
    laserlight, is this what you said I should do?

    Code:
    int main(int argc, char *argv[])
    {
               FILE *fp;
               char retstr[BUFSIZ];
               char *f1, *f2, *f3, *p;
               int max_cnt=15;
               int i, cnt=0;
    
          	   struct item record [10];
    	  
    	 
    	    if (fp=fopen("C://shoppinglist.txt","r"));
    	    {
                printf("Error: can't open file.\n");
                }
               
                if( fp != NULL ) 
                {
                char retstr, [BUFSIZ];
                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++;
               }
    
    
    
    
    	       
              exit(1);
    }
    I don't see an extra simicolon, just an extra bracket
    Last edited by Hitsugaya_KK; 08-19-2009 at 07:45 AM.

  14. #29
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You are getting closer, but you should be more consistent, including within the function:
    Code:
    int main(int argc, char *argv[])
    {
        FILE *fp;
        char retstr[BUFSIZ];
        char *f1, *f2, *f3, *p;
        int max_cnt=15;
        int i, cnt=0;
    
        struct item record [10];
    
        if (fp = fopen("C://shoppinglist.txt","r"));
        {
            printf("Error: can't open file.\n");
        }
    
        if (fp != NULL)
        {
            char retstr, [BUFSIZ];
            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++;
            }
    
            exit(1);
        }
    So now, based on the fact that the last closing brace is not aligned with the first closing brace, you get a visual cue that the main function is not complete.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  15. #30
    Banned
    Join Date
    Aug 2009
    Posts
    30
    I am now getting an error based on "char retstr, [BUFSIZ];" . It is saying Error 2 error C2059: syntax error : '[' . No need error anymore, I realised I had to get rid of "char retstr, [BUFSIZ];" since it had already been initialized. I had to rebuild the program so now there are no errors.

    Code:
    #include <stdio.h> 
    #include <string.h>
    #include <sys/types.h>
    #include <stdio.h>
    
    
    struct item
    {
          int ID;
          char name[64];
          int price;
          int quantity;
    };
    int main(int argc, char *argv[])
    {
               FILE *fp;
               char retstr[BUFSIZ];
               int *f1;
    		   char *f2; 
    		   int *f3;
                       int *f4;
    		   char *p;
               int max_cnt=15;
               int i, cnt=0;
    
          	   struct item record [10];
    	  
    	 
    	       if (fp=fopen("C://shoppinglist.txt","r"));
    	       {
               printf("Error: can't open file.\n");
    		   }
    		   if( fp != NULL ) 
               {
               
               while( fgets(retstr, BUFSIZ, fp) != NULL )
               {
    
    	       if ((p = strchr(retstr, '\n')) != NULL)
    	       *p = '\0';
    	       if(cnt == max_cnt)
    	       break;
    
    		   f1=strtok(,NULL " ");
    	       f2=strtok(retstr, " ");
    	       f3=strtok(NULL, " ");
    	       strcpy(record[cnt].ID, f1);
    	       strcpy(record[cnt].name, f2);
    	       strcpy(record[cnt].price, f3);
                   strcpy(record[cnt].quantity, f4);              
    
    	       cnt++;
               }
    
    
    
    
    	       
               exit(1);
               }
    	
    
    
               for(i=0; i < cnt; i++)
               {
    
    
               printf("%d %s %d\n", record[i].ID, record[i].name,record[i].price);
      
               }
    
               fclose(fp);
    
     
               exit(0);
    }
    I made a some changes to the program. So now I need to do the Menu part :/.

    Code:
    {
    Int option;
    int a; /*it lists all the items*/
    int b; /* it looks for the item with the lowest price*/
    int c; /* it discounts 20% of the price from all items only once*/
    int d:/* it allows the user to select the items he wants*/
    
    printf( "1 - view the complete list of goods in the shop, with their prices" );
    printf( "2 – list the cheapest item on offer" );
    printf( "3 – “discount” option" );
    printf( "4 – choose items for purchase" );
    printf( "5 – total price for items" );
    printf( "6 - Exit");
    
    switch ( option ) {
    case 1:
    case 2:
        break;
    case 3:
    case 4:
        printf( "choose item for purchase\n" );
        break;
    case 5:
        printf( "total price for items\n" );
        break;
    case 6:
        printf( "Exit\n" );
    }
    
    return 0;
    Last edited by Hitsugaya_KK; 08-19-2009 at 08:30 AM.

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, 02: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