finding the end of a line of a file.

This is a discussion on finding the end of a line of a file. within the C Programming forums, part of the General Programming Boards category; I'm reading in a file line by line. Each line has a name, and a random number of classes. for ...

  1. #1
    Registered User
    Join Date
    Feb 2012
    Posts
    117

    finding the end of a line of a file.

    I'm reading in a file line by line. Each line has a name, and a random number of classes.

    for example

    Jerry,CSE1310,CSE1320,CSE1325
    Bob,CSE1105,CSE1310
    Barry,CSE1104,CSE1105,CSE1310,CSE1320

    These are the names and the classes they have already taken.
    I'm trying to tread this file and store in a 2d array of strings. The first column will be the names and the following columns are the classes taken. My question is, since the number of classes is not definite, how do I create a loop to do this until the newline?

    Here's what I have so far, but it's not working. I think my second while loop is where I'm messing up. I think it's a never ending loop because I'm not changing buffer. Just wondering how to basically say while it's not the end of the line, keep using strtok on the commas and put in the next column.


    Code:
         FILE *fp;     
         char buffer[100];
         int line_count = 0;
         int class_count = 0;
         char  *temp;
         char **names_and_classes_taken = malloc (sizeof(char*));
         
         if ( (fp = fopen("hw11-data.csv", "r" )) == NULL )
         {
         printf("Couldn't open file\n");
         exit(1); 
         }
           
         while(fgets(buffer, sizeof(buffer), fp))
         {
             while ( strcmp(buffer, " \n") != 0)                
              {
                   /* if class count is 0, use the buffer, else if the count is 
                      not 0, use the same buffer */
                   if(class_count == 0)
                     temp = strtok(buffer, ",");
                   else
                     temp = strtok(NULL, ",");
                     
                  names_and_classes_taken[class_count] = malloc(strlen(temp));
                  strcpy(names_and_classes_taken[class_count], temp);
              }       
              class_count = 0;        
              line_count++;
              names_and_classes_taken = realloc(names_and_classes_taken, (line_count+1)* sizeof(char*));    
         }
    fclose(fp);

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,751
    Your loop should be
    Code:
    for ( temp = strtok(buffer, ",") ; temp != NULL ; temp = strtok(NULL, ",") )
    > names_and_classes_taken[class_count] = malloc(strlen(temp));
    You need strlen(temp)+1

    And your realloc needs to be inside the loop as well.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Feb 2012
    Posts
    117
    ahh, i never thought of the loop that way. Much easier than what I was trying to do. So I no longer need my if-else statements either.
    Quick question though...Why does the realloc need to be inside that loop? Wouldn't the realloc of names_and_classes_taken just be in the while loop? Because isn't that realloc just to allocate memory for the next row?

    for example something like this?


    Code:
              while(fgets(buffer, sizeof(buffer), fp))  
       {
             for(temp = strtok(buffer, ","); temp != NULL; temp = strtok(NULL,","))              
              {          
                  names_and_classes_taken[class_count] = malloc(strlen(temp)+ 1);
                  strcpy(names_and_classes_taken[class_count], temp);     
                  class_count++;
              }       
              class_count = 0;        
              line_count++;
              names_and_classes_taken = realloc(names_and_classes_taken, (line_count+1)* sizeof(char*));    
         }
    fclose(fp);
    Last edited by mgracecar; 04-25-2012 at 03:02 PM. Reason: forgot my class_count++

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,751
    At the moment, class_count is a constant inside the loop, and you keep trashing the same entry with
    names_and_classes_taken[class_count] = malloc(strlen(temp)+ 1);
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User
    Join Date
    Feb 2012
    Posts
    117
    alright I'm going to fool around with it some more. Thanks for the help
    Last edited by mgracecar; 04-25-2012 at 03:29 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 21
    Last Post: 08-07-2011, 10:55 PM
  2. Replies: 7
    Last Post: 12-13-2010, 02:13 PM
  3. finding command line operatives
    By Tom_Arch in forum C Programming
    Replies: 2
    Last Post: 04-28-2009, 01:25 AM
  4. Finding carriage returns (\c) in a line
    By JizJizJiz in forum C++ Programming
    Replies: 37
    Last Post: 07-19-2006, 06:44 PM
  5. Finding the line and file in which a function was called
    By roktsyntst in forum C++ Programming
    Replies: 2
    Last Post: 04-20-2003, 11:30 AM

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