Thread: FILE and strtok problems (C Linux)

  1. #1
    Registered User
    Join Date
    Apr 2010
    Posts
    23

    FILE and strtok problems (C Linux)

    Good day,

    I am having a compiling error with my code.

    Code:
    #include <stdio.h>
    #include <string.h>
    #define SIZE 81
    
    int main(void)
    {
       int maxLoop = 100;;
       char *status;
       FILE *infile, *outfile;
       
       infile = fopen("worker.txt", "r");
       outfile = fopen("outputData.txt", "w");
      
       struct
       {
    	char employeeNum[10];
    	char firstName[25];
    	char lastName[25];
    	char hoursWorked[5];
       } groupDetails[SIZE];
    
       status = fgets((char)*groupDetails, SIZE,infile);
       while (status != NULL)
       {
    	fputs((char*)groupDetails,outfile);
            status = fgets((char*)groupDetails,SIZE,infile);
       }
       fclose(infile);
       fclose(outfile);
    
       system("pause");
       return 0;	
    }
    The problem is, it's giving me an error on pointers in the status = fgets...
    can somebody help me on this?

    in addition, I need help on reading a line in the text file.
    am I doing it right? and how can I properly implement strtok here?

    thank you so much.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You should type "man fgets" into your system and read what it says. (EDIT: You may also want to type "man fread" as well. Just sayin'.)

    You also probably don't need to implement strtok, as that's the job of the people who made your system. If you want to use it, you should type "man strtok" into your system and read what it says.
    Last edited by tabstop; 04-10-2010 at 04:41 PM.

  3. #3
    Registered User
    Join Date
    Aug 2007
    Location
    MD, USA
    Posts
    71
    Yes, puts() is not meant to write nor is fgets() meant to read a block of data like a struct.
    fread() and fwrite() would be good for struct storage but the file would not be legible (plain.txt).
    You asked about writing/reading a line and using strtok() and it sounded like fun practice so:
    Code:
    #include <stdio.h>
    #include <string.h>
    #define SIZE 80
    
    int main(void)
    {
      FILE *fp;
      struct
      {
        char employeeNum[12];   /* note sizes in increments of 4 bytes */
        char firstName[32];
        char lastName[32];
        char hoursWorked[4];
      } groupDetails[SIZE];
    
      int  i, sz[] = { 0, 11, 31, 31, 3 };  /* sizes are -1 to leave room for \0 terminator */
      char buff[1024];
    
      i = 0;
      strncpy( groupDetails[i].employeeNum, "1234567890123", sz[1] );
      strncpy( groupDetails[i].firstName,   "Orville",       sz[2] );
      strncpy( groupDetails[i].lastName,    "Redenbacher",   sz[3] );
      strncpy( groupDetails[i].hoursWorked, "45",            sz[4] );
    
      fp = fopen("Data.txt", "w");
    
      /* assemble struct members into a comma delimited "line"  and write to file */
      fprintf(fp, "%s,%s,%s,%s\n", groupDetails[i].employeeNum,       
                                   groupDetails[i].firstName,
                                   groupDetails[i].lastName,
                                   groupDetails[i].hoursWorked  );
    
      fclose(fp);
     
      fp = fopen("Data.txt", "r");
    
      i++;
    
      /* read the "line" from file */
      fgets( buff, sizeof(buff), fp);
    
      /* disassemble the line into you struct members (man strtok) */
      strncpy( groupDetails[i].employeeNum, strtok( buff, ",\n"), sz[1] );
      strncpy( groupDetails[i].firstName,   strtok( NULL, ",\n"), sz[2] );
      strncpy( groupDetails[i].lastName,    strtok( NULL, ",\n"), sz[3] );
      strncpy( groupDetails[i].hoursWorked, strtok( NULL, ",\n"), sz[4] );
    
      printf("%s %s %s %s\n", groupDetails[i].employeeNum,
                              groupDetails[i].firstName,
                              groupDetails[i].lastName,
                              groupDetails[i].hoursWorked  );
      fclose(fp);
    
      getchar();  /* system("pause");  <-- DO NOT USE! IT IS A MARK OF THE BEAST */
    
      return 0;
    }
    Output: (note how strncpy() limited "employeeNum" )
    Code:
    ~>  gcc -Wall -W -pedantic 100411_filetostrtok-1.c -o 100411_filetostrtok-1
    
    ~>  ./100411_filetostrtok-1
    12345678901 Orville Redenbacher 45
                
    ~>  hexdump -C Data.txt
    00000000  31 32 33 34 35 36 37 38  39 30 31 2c 4f 72 76 69  |12345678901,Orvi|
    00000010  6c 6c 65 2c 52 65 64 65  6e 62 61 63 68 65 72 2c  |lle,Redenbacher,|
    00000020  34 35 0a                                          |45.|
    00000023
    Last edited by HowardL; 04-11-2010 at 09:08 AM.

  4. #4
    Registered User
    Join Date
    Apr 2010
    Posts
    23
    Quote Originally Posted by HowardL View Post
    Yes, puts() is not meant to write nor is fgets() meant to read a block of data like a struct.
    fread() and fwrite() would be good for struct storage but the file would not be legible (plain.txt).
    You asked about writing/reading a line and using strtok() and it sounded like fun practice so:
    Code:
    #include <stdio.h>
    #include <string.h>
    #define SIZE 80
    
    int main(void)
    {
      FILE *fp;
      struct
      {
        char employeeNum[12];   /* note sizes in increments of 4 bytes */
        char firstName[32];
        char lastName[32];
        char hoursWorked[4];
      } groupDetails[SIZE];
    
      int  i, sz[] = { 0, 11, 31, 31, 3 };  /* sizes are -1 to leave room for \0 terminator */
      char buff[1024];
    
      i = 0;
      strncpy( groupDetails[i].employeeNum, "1234567890123", sz[1] );
      strncpy( groupDetails[i].firstName,   "Orville",       sz[2] );
      strncpy( groupDetails[i].lastName,    "Redenbacher",   sz[3] );
      strncpy( groupDetails[i].hoursWorked, "45",            sz[4] );
    
      fp = fopen("Data.txt", "w");
    
      /* assemble struct members into a comma delimited "line"  and write to file */
      fprintf(fp, "%s,%s,%s,%s\n", groupDetails[i].employeeNum,       
                                   groupDetails[i].firstName,
                                   groupDetails[i].lastName,
                                   groupDetails[i].hoursWorked  );
    
      fclose(fp);
     
      fp = fopen("Data.txt", "r");
    
      i++;
    
      /* read the "line" from file */
      fgets( buff, sizeof(buff), fp);
    
      /* disassemble the line into you struct members (man strtok) */
      strncpy( groupDetails[i].employeeNum, strtok( buff, ",\n"), sz[1] );
      strncpy( groupDetails[i].firstName,   strtok( NULL, ",\n"), sz[2] );
      strncpy( groupDetails[i].lastName,    strtok( NULL, ",\n"), sz[3] );
      strncpy( groupDetails[i].hoursWorked, strtok( NULL, ",\n"), sz[4] );
    
      printf("%s %s %s %s\n", groupDetails[i].employeeNum,
                              groupDetails[i].firstName,
                              groupDetails[i].lastName,
                              groupDetails[i].hoursWorked  );
      fclose(fp);
    
      getchar();  /* system("pause");  <-- DO NOT USE! IT IS A MARK OF THE BEAST */
    
      return 0;
    }
    Output: (note how strncpy() limited "employeeNum" )
    Code:
    ~>  gcc -Wall -W -pedantic 100411_filetostrtok-1.c -o 100411_filetostrtok-1
    
    ~>  ./100411_filetostrtok-1
    12345678901 Orville Redenbacher 45
                
    ~>  hexdump -C Data.txt
    00000000  31 32 33 34 35 36 37 38  39 30 31 2c 4f 72 76 69  |12345678901,Orvi|
    00000010  6c 6c 65 2c 52 65 64 65  6e 62 61 63 68 65 72 2c  |lle,Redenbacher,|
    00000020  34 35 0a                                          |45.|
    00000023


    From your help, I managed to fix my code, but it's giving me another error.

    Quote Originally Posted by h4rdc0ded
    Examin the point where the error occurs closer- are you aware of what you typecast into what?

    Fixing that error alone won't make your program run; you also need to realize what SIZE is, instead of blindly using it wherever a function expects an int- parameter...
    I got the stuff working, but I am having a few more errors.
    Code:
    #include <stdio.h>
    #include <string.h>
    #define SIZE 81
    #define SIZEPAY 81
    
    int main(void)
    {
       FILE *fp, *fpPay;
       char *status, *statusPay;
    
       struct
       {
    	char employeeNum[10];
            char lastName[25];
            char firstName[25];
    	char jobCode[5];
    	char payCode[3];
       } group[SIZE];
    
       struct
       {
    	char payCode[3];
    	char payAmount[8];
       } payRate[SIZEPAY];
    
       int trial2 = 0;
       int i, sz[] = {0,9,24,24,4,2};
       int y, szPayRate[] = {0,2,7};
      
       char buff[1024];
       char buff3[1024];
    
       fp = fopen("Employee.txt","r");
       fpPay = fopen("payR.txt","r");
    
       status = fgets(buff, sizeof(buff), fp);
       statusPay = fgets(buff3, sizeof(buff3), fpPay);
     
       while (statusPay != NULL){
    	strncpy(payRate[y].payCode, strtok(buff3,","),szPayRate[1]);
    	strncpy(payRate[y].payAmount, strtok(NULL,","),szPayRate[2]);
    	statusPay = fgets(buff3,sizeof(buff3),fpPay);
    	y++;
       }
    
       while (status != NULL)
       {
    	strncpy(group[i].employeeNum, strtok(buff," "), sz[1]);
    	strncpy(group[i].lastName, strtok(NULL," "), sz[2]);
    	strncpy(group[i].firstName, strtok(NULL," "), sz[3]);
    	strncpy(group[i].jobCode, strtok(NULL," "), sz[4]);
    	strncpy(group[i].payCode, strtok(NULL," "), sz[5]);      
    
    	for(trial2 = 0; trial2<16;trial2++)
    	{
    	   if(strcmp(group[i].payCode, payRate[trial].payCode) == 0)
    		{
    		   fputs(group[i].payCode, stdout);
    		   fputs("\t",stdout);
    		   fputs(payRate[trial].payAmount, stdout);
    		}
    	}
    
    	
            status = fgets(buff,sizeof(buff),fp);
    	trial2 = 0;
    	i++;
       }
       fclose(fp);
       fclose(fpPay);
    
    return 0;
    getchar();
       
       
    }
    Unfortunately now, The output is not what I am asking.

    my payRate File looks as follows:

    A01, 3.00
    A02, 94.50
    A03, 43.00
    A04, 2.00
    A05, 123.50
    A06, 76.00
    A07, 35.00
    A08, 49.00

    the output in my terminal is giving me A0 3.00
    so many times. I've been trying to work on it for 2 hours, but
    I just can't find it.

  5. #5
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    3 store string of 3 chars you need array of at least 4 chars - to make space for nul-terminator
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. strtok
    By Smithy in forum C Programming
    Replies: 6
    Last Post: 04-09-2010, 07:01 PM
  2. Replies: 6
    Last Post: 10-07-2009, 01:00 PM
  3. problems with strtok()
    By Albinoswordfish in forum C Programming
    Replies: 3
    Last Post: 06-03-2009, 07:49 PM
  4. Help debugging my program
    By shoobsie in forum C Programming
    Replies: 4
    Last Post: 07-05-2005, 07:14 AM
  5. strtok - word counting from a file
    By |PiD| in forum C Programming
    Replies: 4
    Last Post: 11-15-2002, 04:16 AM

Tags for this Thread