0

I have a struct pointer pcbptr that points to a struct pcb. To simplify it a bit I'll say pcb has 3 parameters all of type int so I have

pcbptr mypcb = malloc(sizeof(pcb))
mypcb->first = 0;
mypcb->second = 0;
mypcb->third = 0;

Now I have a file I call input.txt, and basically it just looks like so:

3, 5, 2
5, 2, 1

What I want to do is create 2 different pcbptrs that store the following values so my first mypcb will look like this:

mypcb->first = 3, mypcb->second = 5, mypcb->third = 2,

and the 2nd mypcb will look like this:

mypcb->first = 5, mypcb->second = 2, mypcb->third = 1

The issue I am having is trying to keep track of where I have read up to. So I might call my read from file function on the first pcb, and then stop writing once I reach the end of the line. Then for my second pcb, I want to start reading from the start of the 2nd line, where I left off last.

Basically I have a while loop, and in each one I first initialize my pcbptr, then call the function that reads these files, but I am having trouble how to specify where to start reading.

Can anyone explain how I might be able to do this?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Steven Hsu
  • 183
  • 1
  • 3
  • 15
  • See [Is it a good idea to typedef pointers?](http://stackoverflow.com/questions/750178/is-it-a-good-idea-to-typedef-pointers) – Jonathan Leffler Apr 09 '15 at 16:04
  • If my answer isn't helping you, you will need to show more of your code so we have some idea of what the problems you've got are. Please read about how to create an MCVE ([How to create a Minimal, Complete, and Verifiable Example?](http://stackoverflow.com/help/mcve)). Your question about keeping track of positions is puzzling me; the file stream keeps track of where you are. Your mention of 'then stop writing' puzzles me too; writing hasn't been mentioned before and it isn't clear why writing on an output file would affect how you read data from an input file. _[…continued…]_ – Jonathan Leffler Apr 10 '15 at 02:28
  • _[…continuation…]_ It is also unclear whether you need the value from two (or more) rows available in separate structures at the same time, or whether it is sufficient to have one row at a time in memory. How you handle the allocated `struct pcb`'s will depend on the answer to that. Remember, for every `malloc()`, there should be a corresponding `free()`. – Jonathan Leffler Apr 10 '15 at 02:30

1 Answers1

2

There must be other questions asking basically the same thing, but it's probably easier to write an answer than to find the duplicate.

You probably want to read lines with fgets() and convert them with sscanf():

char buffer[4096];

while (fgets(buffer, sizeof(buffer), stdin) != 0)
{
    pcb *new_pcb = malloc(sizeof(*new_pcb));
    if (new_pcb == 0)
        …report out of memory error; do not pass go; do not collect $200…
    if (sscanf(buffer, "%d, %d, %d", &new_pcb->first, &new_pcb->second, &new_pcb->third) != 3)
        …report data format error; do not leak memory…
    …process or save data pointed at by new_pcb somewhere…
}

Obviously, you can specify a different input file stream from stdin if you wish (and you may well prefer to do so).

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278