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:

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

I would like to store each line in a struct array so I created:

typedef struct Data{
    int Number1;
    int Number2;
    int Number3;
    int Number4;
}Data;

Please help!

The structure as given makes no sense; I suspect what you want is more on the order of this:

typedef struct dynamic_array 
{
    size_t array_size;
    int* array;
} DYNAMIC_ARRAY;

The idea here is that you have a size_t variable to hold the number of elements in the array, and then a pointer which can hold the array itself.

I am going to over-step the usual rules about how one is supposed to help people here, but only because I think you need a good example for how to work with structures and strtok(). While the meanings of the different aspects of this are purely speculation on my part, I think tha this is close to what you want:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define BUFFER_SIZE 15

typedef struct Packet
{
    unsigned int id;
    unsigned int arrivalTime;
    unsigned int transmissionTime;
    unsigned int priority;
} PACKET;

struct
{
    size_t message_size;
    PACKET* packets;
} Message;

int main()
{
    FILE* file;
    size_t i;
    char packet_description[BUFFER_SIZE];
    char *token;
    char* textFile = "data_filetest.txt";

    if((file=fopen(textFile, "r"))!=NULL)
    {
        fgets(packet_description, BUFFER_SIZE, file);
        sscanf(packet_description, "%u", &Message.message_size);
    }

    Message.packets = calloc(sizeof(PACKET), Message.message_size);



    for(i = 0; i < Message.message_size; i++)
    {
        fgets(packet_description, BUFFER_SIZE, file);
        token = strtok(packet_description, ":");
        Message.packets[i].id = atoi(token);
        token = strtok(NULL, ",");
        Message.packets[i].transmissionTime = atoi(token);
        token = strtok(NULL, ",");
        Message.packets[i].arrivalTime = atoi(token);
        token = strtok(NULL, "\n");
        Message.packets[i].priority = atoi(token);
    }
    fclose(file);

    for (i = 0; i < Message.message_size; i++)
    {
        printf("Packet #%u, Priority %u: ", Message.packets[i].id, Message.packets[i].priority);
        printf("transmitted at %u, arrived at %u\n", Message.packets[i].transmissionTime, Message.packets[i].arrivalTime);
    }
    printf("total message size: %u\n", Message.message_size);

    return 0;
}

You'll need to make the appropriate changes to match the actual interpretations of the different fields.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.