Thread: Parsing a Text File

  1. #1
    Registered User
    Join Date
    Jun 2012
    Posts
    2

    Parsing a Text File

    Hello all,

    I am trying to parse a text file that has the following format:

    3
    1:3,60,3
    2:6,70,1
    3:3,50,3

    I am supposed to take the first line "3", and allocate memory for the 3 lines after it.

    Then I want to skip the first line, and start parsing and tokenizing the the next 3 lines into a struct array.

    I wrote the code below, but it is not working. Please note that I am a beginner in C, and I wrote this according to my very basic knowledge about C:

    Code:
    if((file=fopen(textFile, "r"))!=NULL)
        {
            fscanf(file,"%d",&numberOfFlows);
        }    
        printf("%d", numberOfFlows);
        
        newFlows = malloc(sizeof(Flow)*numberOfFlows);
        
        int i;
        for(i = 0; i < (1+numberOfFlows); i++)
        {
            char *flow = malloc(sizeof(char)*15);
            fgets(flow, 15, file);
    
    
            char flowNo = flow[0];
            strtok(flow,":");
            
            char *arrivalTime = strtok(NULL, ",");
            char *transmissionTime = strtok(NULL, ",");
            int priority = flow[3];
    
    
            
            free(flow);
        }
        fclose(file);


    Please guide me to how modify this code.

    Thanks in advance

  2. #2
    Registered User
    Join Date
    Sep 2007
    Posts
    1,012
    How is it not working?

  3. #3
    Registered User
    Join Date
    Jun 2012
    Posts
    2
    I tried using printf to see if the array is being populated by the code but no luck

  4. #4
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    You might also get better responses if you posted a simplified version of compilable code that shows the problem. Perhaps the problem is not in this immediate chunk of code (such as an incorrect variable declaration, etc). This might also help you spot the problem yourself while preparing it to be posted.

  5. #5
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Quote Originally Posted by C_prog View Post
    I tried using printf to see if the array is being populated by the code but no luck
    How can the array be "populated" if you never put anything into it? You malloc room for numberOfFlows Flow structs (I assume it's a struct since you've kept that part a secret), and called it newFlows. But where do you assign to the members of newFlows???

    Also, you're not really doing the NULL file check properly. Your braces are screwed up, and even if they were in the right place you're still doing it in a dumb way. It's better to test for the error condition instead of the success condition, like this:
    Code:
    if ((file = fopen(textFile, "r")) == NULL)
    {
        fprintf(stderr, "What the f*&%???\n");
        exit(1); // or whatever is appropriate
    }
    // Then the rest of your code goes here without indentation.
    Yet another anomaly is that you malloc an array at the top of a block and free it at the end, as if it was an automatic variable. Why not just use an automatic variable then???
    Code:
    char flow[15];
    ...
    Give it another go and post your new code.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. parsing through a text file
    By oopsyourhead in forum C++ Programming
    Replies: 14
    Last Post: 05-29-2012, 01:42 PM
  2. parsing specific text from xml file
    By rabers in forum C Programming
    Replies: 5
    Last Post: 03-07-2011, 02:14 AM
  3. Text file parsing
    By papagaio in forum C Programming
    Replies: 7
    Last Post: 10-01-2009, 04:47 PM
  4. Help parsing text file
    By dudeomanodude in forum C++ Programming
    Replies: 7
    Last Post: 07-16-2008, 10:21 AM
  5. Text file parsing
    By Unregistered in forum C++ Programming
    Replies: 8
    Last Post: 07-25-2002, 01:17 AM