Thread: Strange characters and spaces in file when I write a structure in it.

  1. #1
    Registered User
    Join Date
    Dec 2019
    Posts
    10

    Strange characters and spaces in file when I write a structure in it.

    I am writing a structure to a file. Program compiled and executes successfully. In file, I get strange spaces and some character(not even on keyboard), including the structure.. Meanwhile when I read the file on console, I get correct output(the structures I tried to write in file).
    Code:
    // C program for writing  
    // struct to file 
    #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) 
        { 
            printf(stderr, "\nError opening 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); 
    
        if(fwrite != 0)  
            printf("contents to file written successfully !\n"); 
    
        else 
            printf("error writing file !\n"); 
    
        // close file 
    
        fclose (outfile); 
    
         //To read from file
    FILE * infile = fopen("person.txt");
    struct person input;
    
    if(infile==NULL)
    {
          printf("Error opening file!\n");
          exit(1);
    }
    
    //read file contents till end of file
    while (fread(&input,sizeof(struct person),1,infile)
            printf("\nid = %d. name = %s %s\n",input.id,input.fname,input.lname);
    
    fclose(infile);
    
        return 0; 
    }
    When I open file, I get these;
    " rohan shama sc mahen dhoni"
    (Without the quotes, where sc = strange character not even on my keyboard)
    Please help, I'm a noob in this site.. thnks

    P.S. this code was gotten from a website geeksforgeeks.com
    Last edited by SergeP; 12-06-2019 at 12:13 AM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,509
    Quote Originally Posted by SergeP
    Meanwhile when I read the file on console, I get correct output(the structures I tried to write in file).
    Then there's no problem. You're using fwrite to write a binary representation of the struct person object to file, so when you open the file as if it were a text file, you might find some unusual output, but that's just the binary representation being misinterpreted as text. So, nothing to worry about. If you do want to open the file in an editor, open it in a hex editor.

    That said, you're not checking for successful fwrite correctly: check the return value of the fwrite calls, not the fwrite function converted to a function pointer.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Dec 2019
    Posts
    10
    Thanks ma'am. And is there a way to actually write the text (not the binary representation) i.e an alternative to fwrite?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,509
    Quote Originally Posted by SergeP
    And is there a way to actually write the text (not the binary representation) i.e an alternative to fwrite?
    Of course. You just need to decide on what you want the output to look like, and if you intend to read programmatically from file, you need to decide on an output format that can be read like that.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Dec 2019
    Posts
    10
    Wow, thanks so much.
    I've found a function fprintf(), it actually writes to a file and formats too.

    Thanks too for pointing the stuff about binary... I understood this now. ������

    Is it possible to recommend an answer here? (Like maybe to mark that your answer was the best...)

  6. #6
    Registered User
    Join Date
    Aug 2019
    Location
    Inside a Singularity
    Posts
    239
    If she's the one answering, it's probably the best there is in the thread, just saying. Also, no, there is no way, not that I know of, to mark someone's response the best (in case you were thinking you could vote on others' answers like on StackOverflow).
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook, The Wizardry Compiled

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Write into file with spaces and diacritic.
    By maestorm in forum C Programming
    Replies: 14
    Last Post: 12-30-2012, 02:00 PM
  2. Write dynamic structure to a text file
    By deathseeker25 in forum C Programming
    Replies: 3
    Last Post: 05-22-2012, 12:33 PM
  3. write structure to a file
    By tubby123 in forum C Programming
    Replies: 11
    Last Post: 07-02-2011, 11:20 AM
  4. Replies: 5
    Last Post: 03-18-2006, 11:25 AM
  5. Structure Dynamic with File Write
    By k4z1nh0 in forum C Programming
    Replies: 1
    Last Post: 06-25-2005, 11:46 AM

Tags for this Thread