Thread: save struct in a txt file problem!

  1. #1
    Registered User
    Join Date
    May 2011
    Posts
    3

    save struct in a txt file problem!

    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!!

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Why not try \n instead?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Bitmap, save to file problem.
    By Steve A. in forum Windows Programming
    Replies: 5
    Last Post: 04-08-2011, 12:16 PM
  2. Replies: 2
    Last Post: 05-09-2008, 07:27 AM
  3. Replies: 12
    Last Post: 04-01-2008, 08:58 AM
  4. Save data from two struct arrays in one .dat file
    By IndioDoido in forum C Programming
    Replies: 5
    Last Post: 03-27-2008, 03:50 PM
  5. Newbie File Read and Save problem
    By simham_uk in forum Windows Programming
    Replies: 2
    Last Post: 06-06-2002, 05:17 AM

Tags for this Thread