I searched for it the whole day, but stil wasnt able to find any useful or clear information- some were too complex, some didnt work and thats why im here hoping for some1 to help me out..
Im giving some parts of my program if its any help
typedef struct {
char name [20];
char surname [20];
int min;
int vid;
} stud_db;
stud_db *student, *tmp;
...
...
<-- i think somewhere there ive to make a code which reads records from file
and writes them into student
if (records < 1)
student = (stud_db*) malloc (sizeof(stud_db));
else
student = (stud_db*) realloc (student, (ieraksti + 1)*sizeof(stud_db));
...
...
<-- there i should write all the records into file i guess
} //end of program
i tried fwrite,fread,fprintf and something else, cant remember anymore so i would be grateful if any1 could explain how to make this work
p.s. i just hope i didnt post this in wrong section...
I'm not quite sure on the syntax of C, but I'll give it a shot:
1 2 3 4 5 6 7 8 9 10 11
int printStudent(FILE* output, typdef struct stud_db *student)
{
// One line makes the return easier:
return fprintf(output, "%s\n%s\n%d\n%d\n\n\n",
student->name, student->surname, student->min, student->vid);
}
// Use
if (printStudent(myOutPutFile, myStudent) < 0)
printf("Something went wrong\n");
You could do a "write", and then give it the entire struct to write at once, but I only know this in c++
There are many problems with this, the first being that it will be in binary format. Not really a big deal, but I would imagine you want to be able to edit the file without a program.
Second problem is that sizeof() will actually consider the entire char arrays (not just until null terminator), so you'll get a fair amount of nonsense in your file.