Thread: Write to file using struct

  1. #1
    Registered User
    Join Date
    Sep 2022
    Posts
    5

    Write to file using struct

    While programming with the struct data type, I have a .txt that I wish to write to and I am unable to do so. Below I showcase my code, the problem is the contents of my file become overridden whereas I wish to simply add onto it.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    
    // a struct to read and write
    struct person
    {
        int id;
        char fname[20];
        char lname[20];
    };
    
    
    int main ()
    {
        FILE *outfile;
    
    
        // open file for writing
        outfile = fopen ("person.txt", "w");
        if (outfile == NULL)
        {
            fprintf(stderr, "\nError opened file\n");
            exit (1);
        }
    
    
        struct person input1 = {1, "rohan", "sharma"};
        struct person input2 = {2, "mahendra", "dhoni"};
    
    
        // write struct to file
        //fwrite (&input1, sizeof(struct person), 1, outfile);
        //fwrite (&input2, sizeof(struct person), 1, outfile);
    
    
        //Why does the above erase the contents already in my file
        //I want to keep them and to add the above lines to the file
    
    
        if(&fwrite != 0)
            printf("contents to file written successfully !\n");
        else
            printf("error writing file !\n");
    
    
        // close file
        fclose (outfile);
    
    
        return 0;
    }

  2. #2
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,111
    //Why does the above erase the contents already in my file
    //I want to keep them and to add the above lines to the file
    First of all, you should open the file in "a" Append mode. If the file does not exist, it will create the file. If the file exists then the file will open and new lines will be written to the end of the file, appending the new data to the existing file. Please see man fopen online or in any Linux command line.

    Secondly, I would write out the members of the struct individually, formatting the output on one line or each member on separate lines.

  3. #3
    Registered User
    Join Date
    Sep 2020
    Posts
    425
    Also...

    Code:
       if(&fwrite!= 0)
    Is saying "if the fwrite function has an address that isn't zero".

    Maybe use
    Code:
        if(fprintf(f,"%s, %s\n", input1.fname, input1.lname) == 2) {
           // wrote 2 things to the file (fname and lname)
        } else {
            // unable to write - so error
        }

  4. #4
    Registered User
    Join Date
    Sep 2022
    Posts
    5
    These suggestions I found helpful, only hamster_nz I do not know what you meant by the parameter 'f' as it gives me an error. Now I am also wondering like you said rstanley how to hardcode struct members individually. Currently they are being added one after the other on the last line of the file, do I have choices other than using '\n' to define their output?

  5. #5
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,111
    ufl:

    You don't say how you will use the data file, once you have written to a file. Will you read the data back into the same program or into a second program?

    Yes you could write out the struct as binary data to a file, using fwrite(), or if transferring data between two programs, you might want to write out the three elements of the struct as text lines using fprintf().

    From hamster_nz:
    fprintf(f,"%s, %s\n", input1.fname, input1.lname)
    The first argument to fprintf(), "f" represents the file pointer. in your code, your file pointer is "outfile".

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Write File to struct
    By Olchi in forum C Programming
    Replies: 3
    Last Post: 04-15-2020, 03:24 AM
  2. Write and read struct to txt file
    By Heisenberg800 in forum C Programming
    Replies: 1
    Last Post: 11-28-2017, 12:49 PM
  3. Write to struct
    By john7 in forum C Programming
    Replies: 2
    Last Post: 07-14-2014, 06:41 AM
  4. Using binary write to save a struct
    By skan in forum C++ Programming
    Replies: 4
    Last Post: 08-14-2013, 06:57 AM
  5. multiway read\write struct
    By quantt in forum C Programming
    Replies: 2
    Last Post: 02-02-2009, 10:13 AM

Tags for this Thread