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

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

    "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
    12
    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
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    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
    12
    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
    28,413
    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.)
    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

  8. #8
    Registered User
    Join Date
    Oct 2012
    Posts
    12
    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, 08: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, 07:59 AM
  5. Replies: 7
    Last Post: 09-05-2002, 08:01 AM