Hello!!!I am trying to find a way to save my structs in a file!I have found a code that does this..and it is the following....
Code:
void WriteFile(struct car* q )
{
printf("Attempting to write...");
FILE* fp = 0;
char* buffer = 0;
int i=0;
/* allocate */
buffer = malloc ( 150 );
bzero( buffer, 150 );
/* copy the data to a string */
snprintf( buffer,150,"%s\t%s\t%d\t%s\t%.2f\t%.2f\t%d/%d/%d\t%d/%d/%\t%d/%d/%d",q->name,q->numberplate,q->km,q->phonenumber,q->overall_cost,q->paid_cost,q->dateIn->day,q->dateIn->month,q->dateIn->year,q->dateServiced->day,q->dateServiced->month,q->dateServiced->year,q->dateOut->day,q->dateOut->month,q->dateOut->year);
printf("\n");
fp = fopen("arxeio3.txt", "a" );
if (fp==NULL)
printf("Error in Opening the file!!");
else {
fputs( buffer, fp );
}
free( buffer );
fclose( fp );
}
However the problem is that i want to save my structs are all being saved in one line of the file!
What i want is to save my structs in my file with the following way:
q[0].name q[0].numberplate q[0].km .............................................
q[1].name q[1].numberplate q[1].km .............................................
q[2].name q[2].numberblate q[2].km .............................................
.................................................. .................................................. .
I tried putting "/n" at the end of snprintf () but nothing happens!!! Can anyone help me modify this function to save my files in the way I want????
I will appreciate your help!!