0

I have a text file that I need to read a populate a linked list. The file structure is like this.

Ant,Adam   10 5
Mander,Sally 4 3
King,May  6 6
King,Joe 9 6
Graph,Otto 2 5
Carr,Redd 1 3

The name is szName. The second int is iDepartTmUnits, and the last int is iTime;

I'm trying to read the input from stdin It should insert EVT_ARRIVE and EVT_DEPART events into the simulation's eventList. Assuming you are using fgets and sscanf, please make certain you check the count returned from your sscanf.

// Event Constants
#define EVT_ARRIVE          1      // when a person arrives
#define EVT_DEPART          2      // when a person departs the simulation 

We have these structures

typedef struct
{
char szName[16];       // Name
int iDepartTmUnits;    // time units representing how long he/she stays around
} Person;

// Event typedef (aka Element)
typedef struct
{
int iEventType;        // The type of event as an integer:
                       //    EVT_ARRIVE - arrival event
                       //    EVT_DEPART - departure event
int iTime;             // The time the event will occur 
Person person;         // The person invokved in the event.
} Event;

// NodeLL typedef - these are the nodes in the linked list
typedef struct NodeLL
{
Event event;
struct NodeLL *pNext;  // points to next node in the list
} NodeLL;

// typedefs for the ordered link list 
typedef struct
{
NodeLL *pHead;         // Points to the first node in the ordered list
} LinkedListImp;

typedef LinkedListImp *LinkedList;

// typedefs for the Simulation
typedef struct
{
int iClock;            // clock time
LinkedList eventList;  // A linked list of timed events
} SimulationImp;
typedef SimulationImp *Simulation;

Now where I am struggling is how to populate a linked list with this information. Actually I'm struggling with a lot to grasp my head around this, so I'm sorry at the question being overly complex or overly simple.

First Thing I am struggling with I'm declaring it as

void generateArival(Event eventM[])

I believe that is incorrect, because in my main, I wouldn't pass it the event, I believe I would pass it a Simulation implementation.

Second Thing I Am struggling with Here is the code I have so far where I am too copy from the file into a linked list.

while(!feof(pFilePerson))
{
    iLineCount++;
    i++;

    fgets(szInputBuffer, MAX_LINE_SIZE, pFilePerson);
    iScanfCnt = sscanf(szInputBuffer,"%s %d %d\n",
                      event.person.szName,
                      event.iTime,
                      event.person.iDepartTmUnits,
                      );
}

Lastly

I am to input the EVT_ARRIVE and EVT_DEPART into the eventList. I believe that to be something like this, they are int 1 and 2 respectfully, so I would need something like iEvent = event.iEventType; and input that into the sim->eventList

Any help is appreciated, I need a lot more time with this concept of linked lists, but this is breaking my head.

EDIT

I can print out the name but not the numbers

    while(fgets(szInputBuffer, sizeof szInputBuffer, pFilePerson) != NULL)
{

    // print the input buffer as is (it also has a linefeed)
    //printf("Person # %d: %s\n", iLineCount, szInputBuffer);
        sscanf(szInputBuffer,"%s",
                    event.person.szName);
        sscanf(szInputBuffer, "%I64d", 
                    &event.person.iDepartTmUnits);

                     //linkList.event.iTime);

    printf("%-7s\n", event.person.szName);
    printf("%d\n", event.person.iDepartTmUnits);
} 
  • 3
    Note that [`while (!feof(file))` is always wrong](https://stackoverflow.com/questions/5431941/while-feof-file-is-always-wrong) — your code is not an exception to the rule. Check the return value from `fgets()` to know whether you've reached EOF or not. – Jonathan Leffler Mar 01 '19 at 23:24
  • @JonathanLeffler yeah my bad, it should be while (fgets(szInputBuffer, MAX_LINE_SIZE, pfileCustomer) != NULL) – user33plus1 Mar 01 '19 at 23:29
  • Since there is no context for `void generateArival(Event eventM[])`, it isn't clear how to help you. (Two r's in Arrival would be a cosmetic improvement.) I agree that it is not obvious that passing an array to the function is correct — but how to fix it depends on what the function is supposed to do. If you need to read the data from inside the function, then maybe you need to pass the array size too. – Jonathan Leffler Mar 01 '19 at 23:29
  • generateArrival is supposed to this I'm trying to read the input from stdin It should insert EVT_ARRIVE and EVT_DEPART events into the simulation's eventList. Assuming you are using fgets and sscanf, please make certain you check the count returned from your sscanf. @JonathanLeffler – user33plus1 Mar 01 '19 at 23:35
  • @JonathanLeffler i have edited the text. I can output the name, but the numbers aren't printing out correctly – user33plus1 Mar 02 '19 at 02:43
  • for ease of US reading/understanding your code, do NOT hide a pointer via typedef – user3629249 Mar 02 '19 at 04:15
  • what are you expecting: `I64d` to accomplish? – user3629249 Mar 02 '19 at 04:16
  • regarding: `sscanf(szInputBuffer,"%s", event.person.szName);` when using the input format specifiers `%s` and/or `%[...]` always include a MAX CHARACTERS modifier that is 1 less than the length of the input buffer. This is because those input specifiers always append a NUL byte to the input and avoids any possibility of a buffer overflow and the resulting undefined behavior – user3629249 Mar 02 '19 at 04:18
  • when calling any of the `scanf()` family of functions, always check the returned value (not the parameter values) to assure the operation was sucessful – user3629249 Mar 02 '19 at 04:19
  • @user3629249 I am just copying the code that was provided by my professor verbatim. And to answer the second part, I saw that in another question on here that it might solve the problem. It did not. In this post https://stackoverflow.com/questions/39682024/reading-a-multiline-text-file-using-sscanf-and-fgets#comment66664193_39682024 they do something similar, except i try to emulate it and my output doesn't print out the output I am looking for. – user33plus1 Mar 02 '19 at 04:20
  • @user3629249 I added a counter to check, and it should go through the file a total of 3 times correct? it only goes through the line of text once. Which is why I'm guessing I'm getting the name output correctly, but when it comes to the numbers in the input file, it generates a random number in memory or a 0; – user33plus1 Mar 02 '19 at 04:21
  • Suggest: read each line via `fgets()` into a buffer, the use `strtok()` in a loop for each field, when the `name` field is being extracted, use `strcpy()` to copy the name to a separate field. When each numeric field is being extracted, use `strtol()` to convert the numeric string to a `long int` value – user3629249 Mar 02 '19 at 04:28
  • You are not going through the file 3 times. Rather, you are reading each line one time (it is just circumstantial that there are 3 lines in the file) – user3629249 Mar 03 '19 at 01:56

0 Answers0