Adding a line that will output some information to a text file in a C program

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • CBnasa
    New Member
    • Aug 2013
    • 2

    Adding a line that will output some information to a text file in a C program

    I have a program written in C, that was written by a co-worker. I want to add a line of code that will output certain structures that are within a If loop inside the program, to a text file. How do i do that?
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    Before the loop call fopen to open or create your file.

    Inside the loop call fwrite to write data to your file.

    After the loop call fclose() close the file.

    There are plenty of tutorials on the Internet on how to use these functions.

    Comment

    • CBnasa
      New Member
      • Aug 2013
      • 2

      #3
      Originally posted by weaknessforcats
      Before the loop call fopen to open or create your file.

      Inside the loop call fwrite to write data to your file.

      After the loop call fclose() close the file.

      There are plenty of tutorials on the Internet on how to use these functions.






      So i want to write these structures to a file inside the following loop

      Code:
       if(fread(&data1,sizeof(struct applanix_data_group1),1,fp)==1)
           {
      
              sbet.gpstime = time_sod;
              sbet.latitude = data1.latitude * DEGREES2RADIANS;
              sbet.longitude = data1.longitude * DEGREES2RADIANS;
              sbet.altitude = data1.altitude;
      
      (there are other structures in this loops but these are the ones that i need)
           }
      So how would i write the fwrite command to only output these structures to a text file?
      Last edited by Rabbit; Aug 20 '13, 08:31 PM. Reason: Please use code tags when posting code.

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        You would probably use an fwrite function call for each data item followed by one fwrite call to write a \n.

        I really recommend you write a small program to open a file, write data to it, close the file so you can verify you know how to do this. Then apply that knowledge to this program.

        Comment

        Working...