Struct Help

This is a discussion on Struct Help within the C Programming forums, part of the General Programming Boards category; Alright I don't understand how I would go about reading in from a file to store those values in multiple ...

Struct Help

Alright I don't understand how I would go about reading in from a file to store those values in multiple structs. I've been trying to read tutorials on this for awhile to no avail. Could someone shed some light on this for me in lame mans terms would be helpful haha. This is what I'm trying to do

Code:

#include <stdio.h>
typedef struct {
int number;
int rating;
char compName;
char compType;
} review;
int main()
{
int i;
char buffer[1000];
FILE *fp; //setting file pointer to fp and reading the file
fp=fopen("review.txt", "r");
while (fgets(buffer,1000,fp)!= NULL){ //storing the file in buffer
puts(buffer);
}
//Now I want to store the multiple values that are in the text value into the multiple elements of the structure, theres going to be 4 values for number etc, they are formatted line by line in the file
}

Not sure how to go about it at all, reading material, examples, anything would be appreciated.

My problem is I don't want to assume anything about the length of data in the file because I want to be able to give the user option to input the data, I really appreciate what you have shown me so far, as it helps certainly. The first part is for a homework assignment, which you certainly gave me an understanding as I was going to do it a lot harder way lol, however what I'm asking now is just so I can figure out how I can use this to write a n amount of structures to represent information I want to add to, for my own knowledge

Any suggestions on reading materials? Everything I have found on structures was rather basic, even the book I have does go in depth about them, and I understand the basic premise, it's the intermediary stuff like this that I would like to understand better.

I don't know where to start explaining it, and I'm really too lazy to go over it line by line, but if you have any specific spots you want me to explain (why the code does what it does, and how a specific part works), I'd be happy to try to.

I did just whip it up from scratch, so it may contain bugs. If you or anyone else spots one, please do say so, preferably including the fixed code in your post.

The main useful points in my mind, are

Dynamic memory allocation.
In all cases there is the pointer, the currently used size, and the allocated size. (I often use size for the allocated size, and used for the length or count actually used.)

realloc(NULL, size) is the same as malloc(size)

free(NULL) is safe, and does nothing

Usefulness of POSIX.1-2008 strndup() and getline() functions
I included C89-compatible implementations in case your C library does not provide them. In most current environments, if you #define _POSIX_C_SOURCE 200809L before any #includes, your C library should provide those for you.

How simple it would be to support any newline convention
My implementation of getline() accepts \n, \r, \r\n, and \n\r as newlines.
Unfortunately, the standard getline() does not.

Parsing input using complicated sscanf() patterns
The main point, really, is that instead of copying substrings into separate arrays, you can use %n to save the position in the string (it takes a pointer to an integer). Unfortunately, the standards disagree whether %n is considered a conversion or not.
As a solution, I initialize the integer matching the last %n to -1, which is a value scanf() will never assign to it. If it is nonnegative after the scanf() call, it was converted. Otherwise not.

As to the C functions used, I warmly recommend looking at the Linux man-pages project. The section 3 contains the C interfaces. Although it is a Linux project, it is not Linux-specific; each man page has a Conforming to section, which tells you which standards or conventions it is described in.