Print Struct to file

This is a discussion on Print Struct to file within the C Programming forums, part of the General Programming Boards category; I want to print the struct to a .txt file. When I compile my code my final printf returns the ...

  1. #1
    carpe diem
    Join Date
    Jan 2010
    Posts
    46

    Question Print Struct to file

    I want to print the struct to a .txt file. When I compile my code my final printf returns the values I input just fine but my fprintf saves some strange symbols into the text file. Can anyone please help me debug this?
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
        
    
    int main ()
    
    {
     struct 
     {
      char user_airportdestination[5];
      char user_airportorigin[5];
      char user_maker[7];
      char user_model[4];
      char user_flightnumber[8];
      char user_tailnumber[8];
      float user_altitude;
      float user_heading;
      float user_trackangle;
      float user_gspeed;
      float user_airtemp;
      float user_latitude;
      float user_longitude;
     } data;
     
      printf("Aircraft data acquisition\nThis program acquires user data and saves it into a file\nPress enter to continue");
      getchar();
      system("cls");
      printf ("Airport Destination:\n");
      scanf ("%s",&data.user_airportdestination);
    
      printf ("Airport Origin:\n");
      scanf ("%s",&data.user_airportorigin); 
      
      printf ("Aircraft Maker:\n");
      scanf ("%s",&data.user_maker); 
     
      printf ("Aircraft Model:\n");
      scanf ("%s",&data.user_model); 
      
      printf ("Flight Number:\n");
      scanf ("%s",&data.user_flightnumber);
      
      printf ("Aircraft's Tail Number:\n");
      scanf ("%s",&EFBdata.user_tailnumber); 
       
      printf ("Altitude:\n");
      scanf ("%f",&data.user_altitude);
          
      printf ("Heading:\n");
      scanf ("%f",&EFBdata.user_heading);
    
      printf ("Track Angle:\n");
      scanf ("%f",&data.user_trackangle);
       
      printf ("Groundspeed:\n");
      scanf ("%f",&EFBdata.user_gspeed);
      
      printf ("Static Air Temperature (degrees Celsius):\n");
      scanf ("%f",&data.user_latitude);
      
      printf ("Latitude:\n");
      scanf ("%f",&EFBdata.user_latitude);
      
      printf ("Longitude:\n");
      scanf ("%f",&data.user_longitude);
      
    
     printf ("Data acquired %s\n%f", data.user_airportdestination, data.user_latitude);
     getchar();
    
      FILE *file; 
      file = fopen("user_input.txt","w");
      fprintf(file,"%s %f", data.user_airportdestination, data.user_latitude); //writes// 
      printf ("Data saved in user_input.txt");
      getchar(); // pause and wait for key //
      
      
    
      return 0;
    }
    Thanks!
    Doia

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Code:
    printf ("Data acquired %s\n%f", data.user_airportdestination, data.user_latitude);
     getchar();
    
      FILE *file; 
      file = fopen("user_input.txt","w");
      fprintf(file,"%s %f", data.user_airportdestination, data.user_latitude); //writes//
    If that red part prints okay the file output should also be okay. Are you sure this is exactly the code you are using? Maybe put a /n in and fclose() the file to make sure it is flushed.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,855
    My first guess would be that you're typing in longer strings than your short arrays allow for.

    Post some examples of user input which display corrupt data.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    carpe diem
    Join Date
    Jan 2010
    Posts
    46
    Thanks Salem and MK27. The fclose() solved the problem.
    Is there a way I can print out the entire struct at once rather than having to call each variable separately?

  5. #5
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by doia View Post
    Thanks Salem and MK27. The fclose() solved the problem.
    Is there a way I can print out the entire struct at once rather than having to call each variable separately?
    Not if you want to read the file as text. If you just want to store the data, you can write straight out -- except you have a pointer in the struct, which there is no point in recording addresses.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Assignment HELP!!
    By cprogrammer22 in forum C Programming
    Replies: 35
    Last Post: 01-24-2009, 02:24 PM
  2. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 05:02 AM
  3. Double print
    By thestrap in forum C Programming
    Replies: 1
    Last Post: 12-05-2007, 07:17 PM
  4. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 01:13 AM
  5. Personal Program that is making me go wtf?
    By Submeg in forum C Programming
    Replies: 20
    Last Post: 06-27-2006, 01:13 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21