"Invalid chars", when I try to create a text file

This is a discussion on "Invalid chars", when I try to create a text file within the C Programming forums, part of the General Programming Boards category; Hello guys, how are you? I'm trying to generate a text file using the commands fread and fwrite, but when ...

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    10

    "Invalid chars", when I try to create a text file

    Hello guys, how are you?

    I'm trying to generate a text file using the commands fread and fwrite, but when I open the text file that I'm generating it has a lot of weird letters, and a warning: "Your file has invalid chars"

    I have a struct, I read data into it, and then I put it data on a bin file. (I'm able to read the bin file, and show it to the user without any errors),

    Then I try to put these data into a text file, and then when i try to open the text file, it fails...

    I've already made a lot of others programs that works with FILE commands, so, in your experience, what that might be?

    Thanks in advance,
    Last edited by frank1; 10-02-2012 at 07:45 PM.

  2. #2
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    It's probably acting correctly.
    fwrite and fread write unformatted (so-called "binary") data to the file. So if n is an integer and you do this
    Code:
    fwrite(&n, sizeof n, 1, fp);
    Then the actual bytes of the integer are written to the file, not the ascii characters that make up the "human-readable" form.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  3. #3
    Registered User
    Join Date
    Oct 2012
    Posts
    10
    but.... what could I do then?

    What could I do to create a text readable form file if I have already read a bin file?

    I have a file.bin, and I can read it on the terminal, but if I try to put that data on a file.txt file, when I try to open this file, it shows a weird output

    Thanks for the help
    Last edited by frank1; 10-02-2012 at 08:05 PM.

  4. #4
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,739
    It's not necessary to make text files with fwrite and to read them with fread. If you want to convert binary data into a simple text file you have to represent the data with printable characters. This means that the data will have to be converted back into data types when you read them back in, completely unlike binary files.

  5. #5
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Quote Originally Posted by frank1 View Post
    What could I do to create a text readable form file if I have already read a bin file?
    I have a file.bin, and I can read it on the terminal, but if I try to put that data on a file.txt file, when I try to open this file, it shows a weird output
    Pretend that I don't have psychic powers for the moment and ask your question again.

    Where did file.bin come from?

    Do you know it's binary structure?

    What exactly are you trying to do?
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  6. #6
    Registered User
    Join Date
    Oct 2012
    Posts
    10
    But... if I'm reading like fread(&myStruct, sizeof(typeMyStruct), 1, fp) I'm already converting the data to a struct, and then I try to fwrite this data from the struct...

    Code:
    while(fread(&myStruct, sizeof(typeMyStruct), 1, fp)!=0){
         fwrite(&myStruct, sizeof(typeMyStruct), 1, fp2);
    }
    I cant see why it doesnt work, cause it seems to be what you are talking about

    Thanks again for the help guys, really appreciate it

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,931
    You are not writing the struct correctly. Rather, you are just writing it as if you wanted to write to a binary file, but you stated that you want it to be a human readable text file. Therefore, you need to come up with a suitable file format, then write it to file using that format, e.g.,
    Code:
    while (fread(&myStruct, sizeof(typeMyStruct), 1, fp) != 0) {
        fprintf(fp2, "%d %d\n", myStruct->x, myStruct->y);
    }
    (Of course, I'm making up the format and even your struct's members for my example.)
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    Oct 2012
    Posts
    10
    Yeah, it worked!

    I was dealing so much with binary files, that I forgot that to read/write on txt files it had a different syntax xD

    Thanks oogabooga, whiteflags and laserlight

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 03-29-2010, 09:07 PM
  2. Replies: 1
    Last Post: 08-06-2007, 10:09 PM
  3. "itoa"-"_itoa" , "inp"-"_inp", Why some functions have "
    By L.O.K. in forum Windows Programming
    Replies: 5
    Last Post: 12-08-2002, 07:25 AM
  4. "CWnd"-"HWnd","CBitmap"-"HBitmap"...., What is mean by "
    By L.O.K. in forum Windows Programming
    Replies: 2
    Last Post: 12-04-2002, 06:59 AM
  5. Replies: 7
    Last Post: 09-05-2002, 08:01 AM

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