#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2013
    Posts
    11
    Rep Power
    0

    Question Reading fields in tab delimited line in variables


    I need to read lines from a tab-delimited file. Since my file is huge, I just want to read only those chars which I need to. For simplicity, I used static example here.
    I am only interested in first two fields of the line. For me rest of the line is noise. Also both the fields are variable in size.

    I don't want to use 'strtok' function here.

    Code:
    #include <stdio.h>
    int main ( )
    {
        //Read the patient file and count the total number of lines in it.                                        
      char line[100] = "1234\tsdhgfhgshgfhsfhjgsjhfgsjhfghsgfhsgfhsgfhgshgfhsgfhgsf\t11\t22\t55\n";
      int i=0, j=0;
      char *f1, *f2;
      printf("%s\n", line);
      //read first field                                                                                          
      while(line[i] != '\t')
        *(f1+(j++)) = line[i++];
      *(f1+j) = '\0';
      //read second field                                                                                         
      j=0;
      while(line[i] != '\t')
        *(f2+(j++)) = line[i++];
      *(f2+j) = '\0';
      printf("%s\t%s\n", f1, f2);
      //skip rest of the lines                                                                                    
      free(line);
      exit(0);
    }
    Edit: Input from dwise1_aol
    Code:
    #include <stdio.h>
    int main ( )
    {
        //Read the patient file and count the total number of lines in it.                                        
      char line[100] = "1234\tsdhgfhgshgfhsfhjgsjhfgsjhfghsgfhsgfhsgfhgshgfhsgfhgsf\t11\t22\t55\n";
      int i=0, j=0;
      char c;
      char f1[100], f2[200];
      printf("%s\n", line);
      //read first field                                                                                          
      while(line[i] != '\t')
        {c = line[i++];
          f1[j++]=c;}
      f1[j] = '\0';
      //read second field                                                                                         
      j=0;
      i++;
      while(line[i] != '\t')
        {c = line[i++];
          f2[j++] =c;
        }   
      f2[j] = '\0';
      printf("%s\t%s\n", f1, f2);
      //skip rest of the lines                                                                                    
    }
  2. #2
  3. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,091
    Rep Power
    2222
    What's your question?

    Have you tried to compile your program yet? Why haven't you corrected the warning that it's throwing? Compiled with MinGW gcc on WinXP:
    C:TEST>gcc -Wall kindall1.c
    kindall1.c: In function `main':
    kindall1.c:20: warning: implicit declaration of function `free'

    C:TEST>
    Even when you have #include'd the correct header file to clear up that warning, your program should crash on you. free can only free up memory that has been allocated dynamically with malloc or a related allocation function. line is not dynamically allocated memory, but rather an array that you had declared. You cannot free line.

    Also, you are using two uninitialized pointers, f1 and f2. Since they are uninitialized, they contain garbage addresses and hence could be pointing anywhere. Using uninitialized pointers is almost guaranteed to cause disaster. I've used the analogy of a crowded room with a narrow corridor of empty space that gives you a clear line of fire to legitimate targets, but you close your eyes, spin yourself around a few times, and blindly shoot in whatever direction you just happen to end up facing. What do you think you're most likely to hit, a legitimate target or a person or two? That's the variant of Russian Roulette that you play when you use uninitialized pointers.

    Either declare f1 and f2 as arrays or malloc/calloc some memory to them. In either case, ensure that they are large enough to accommodate the longest string that you could be giving them plus one more for the null-terminator.

    And please remember to never ignore warnings.

    Comments on this post

    • marykindall agrees
    Last edited by dwise1_aol; December 3rd, 2013 at 12:27 PM.

IMN logo majestic logo threadwatch logo seochat tools logo