-2

I have a txt file which is :

http://www.siz.co.il/my.php?i=znzwlnkn2fmq.png

and I have my structs :

typedef struct _car_s
{
    char destination[50];
    int priority;
    struct _car_s *next;
    int number_id;

}car_s, *p_car_s;

typedef struct _station_s
{
    struct _station_s *waiting;
    char nameofstation[50];
    struct _station_s *detached;

}station_s, *p_station_s;

I would like to use your help to figure out how I can initialize my stations and cars from the file, a recommended function. Thank in advance and have a nice day.

edit :

I made a file reading function :

int read_files(char* filename, p_car_s cars, p_station_s station)
{

    FILE* myFile;
    char buff[1024];
    myFile = fopen(filename, "r");
    if(NULL == myFile)
    {
        printf("\n fopen() Error!!!\n");
        return 0;
    }
    while (fgets(buff, 1024, myFile) != NULL)
    {
        handle_line(buff, cars, station);
    }


    if(fclose(myFile) == 0 )
    {
        buff[0]='\0';

    }
}

But I cant seem to think of what to do with the function Handle_line and how to initialize the structs.. any ideas ?

Natezone
  • 1
  • 5
  • You will need to write code that reads data from the file and then initializes a struct with the values you have read – M.M Apr 24 '16 at 06:30
  • Ive added a read file function I made.. – Natezone Apr 24 '16 at 06:44
  • Good that you're using `fgets()` correctly. Now, what have you tried for `handle_line`? Please read [Is it a good idea to typedef pointers](http://stackoverflow.com/questions/750178/is-it-a-good-idea-to-typedef-pointers) where you will learn that the short answer is "No". – Jonathan Leffler Apr 24 '16 at 14:14
  • In general, it is best to include the text of a sample data file in the question rather than making us go to some off-site resource to look at it. There are important details, such as `#` comment lines and blank lines being ignored, as well as the use of comma-separated values in the input lines, and the separation marker between two types of data in the file that should be visible in the question. How would you spot a comment line? How would you spot an empty, or all blank, line? How would you spot the separation marker? Which code keeps track of whether you're reading stations or cars? – Jonathan Leffler Apr 24 '16 at 14:19
  • Note that your code in `read_files()` sometimes return a value and sometimes doesn't; that's bad. The name is also a misnomer; it reads a single file, not a multitude of files. At some point, you're going to face the perpetual problem of lists and trees; how do you get the data back from the called function. Unless you create an empty list that the file reading code can append to, your interface won't work. With care, you can use `sscanf()` to parse the input lines, though the comma-space field separators demand care. – Jonathan Leffler Apr 24 '16 at 14:22

2 Answers2

0

In pure C there is no function that reads arbitrary data into a data structure.

You need to read each and every byte from the file into each and every byte in your data structure. You might read 4 bytes at a time if its a 32 bit integer or 8 bytes if it's an 8 byte integer. Don't read a struct from file into a struct in memory unless you know what you're doing.

There are libraries in C that allow you to read things like JSON etc but these libraries hide an inordinate amount of complexity from us users.

Harry
  • 11,298
  • 1
  • 29
  • 43
0

I think the better way is to use the fread() to read the contents one by one and then initialize.

Vivek
  • 192
  • 1
  • 3
  • 7
  • Using `fread()` is not the answer. The input file is in variable length text records with comment lines that need to be ignored. `fread()` is inappropriate unless your proposal was to read the entire file into a block of memory in one slurp (that's the term used for reading a file in one chunk), and that isn't what you suggest. – Jonathan Leffler Apr 24 '16 at 14:12