Sign in to follow this  

fopen() fwrite() ?

This topic is 4865 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi I'm having some trouble with fopen and fwrite: When I try to write a char* in binary it is saved as a string anyway and when I try to write it in text mode it is also saved as a readable string but the new lines are not written: FILE* pFile = fopen("testfile.txt", "w"); char* message = new char[9]; message[8] = 0; strcpy(message, "Hello!!\n"); fwrite((void*)message, sizeof(char), 9, pFile); fwrite((void*)message, sizeof(char), 9, pFile); int value = 10000; fwrite((void*)&value, sizeof(int), 1, pFile); //Neither the text nor integer is written as supposed to ? //The file: Hello!! Hello!! %# //I wanted: //The file: Hello!! Hello!! 10000 And I want to use the fopen/fwrite functions. (for the int I guess I could convert it to a string first...) And same goes for binary mode: When I try to write in binary mode: FILE* pFile = fopen("testfile.txt", "wb"); char* message = new char[9]; message[8] = 0; strcpy(message, "Hello!!\n"); fwrite((void*)message, sizeof(char), 9, pFile); fwrite((void*)message, sizeof(char), 9, pFile); int value = 10000; fwrite((void*)&value, sizeof(int), 1, pFile); //The text is written like in ascii mode? only the int is written in unreadable format... //The file: Hello!! Hello!! ' //I wanted something like: %&&&!% (I gues I could encrypt it but thats not the point...) Would appreciate any clarification, thank you!

Share this post


Link to post
Share on other sites
Never use strcpy, use strncpy if you really have to, and strcpy/strncpy both add the null terminator for you, you don't need to do it explicitly.

char will always be 1 byte from sizeof() so you can just use 1 istead of sizeof(char)

fwrite and fread are binary I/O, the 10000 is written and it can be read using fread. If you want it in ascii you should use fputs/fgets or something.. but why are you using the C file I/O in C++?

Share this post


Link to post
Share on other sites
You have a misconception. ASCII codes *ARE* binary.
If you want it to be "unreadable" you will have to change the values somehow(encrypt them), for example subtracting 32 from each letter might work.

The difference between binary mode and text mode is explained fully in the fopen documentation at MSDN, here (see t and b modes.) But basically, in text mode, 0 is treated as the end of file, and characters like '\n' are translated for the locale. In binary it doesn't translate anything for you.

@soundsystem
A lot of people prefer to use the standard C I/O in C++, including myself, for various reasons. What's the problem with it?

Share this post


Link to post
Share on other sites
Setting 'b' flag on the fopen() sets the file to "binary mode" which means that:

- you can write ASCII 27 (^Z) without it being interpreted as an end-of-file. (Similarly with ASCII 4, ^D, on Linux; I THINK; or does it make a difference?)
- newlines are not "converted", but instead you get just the plain ASCII 13. On most non-Windows systems, this doesn't make a difference.

Using 'fwrite()' to output data means that you are writing 'binary data' which means that:

- numeric data is encoded as a sequence of bytes, rather than a textual representation of the number.
- Text data still appears normally, because that is how it is represented in memory.
- However, if you ask to write the '\0' at the end of a string, it gets written.

If you get a real file/text editor, you will be able to see the non-printing characters that are included in the output file. Better yet, see if you can view the file as a hex dump.

Using the C-style I/O routines is bad in C++ for the same reason using varargs, printf, etc. is all bad: you're working with C-style casts and the C type system what with its "void *" and 0 vs. NULL and all that. Bad mojo(TM).

Share this post


Link to post
Share on other sites
So... you mean the ofstream istream should be preffered over
the fopen version?

But why exactly are they so much better?

Thank you guys for the posts!

Share this post


Link to post
Share on other sites
Quote:
Original post by bilsa
So... you mean the ofstream istream should be preffered over
the fopen version?

But why exactly are they so much better?


I/O stream library is type-safe.
I/O stream library can work with user-defined types directly
I/O stream library is extendable.

Share this post


Link to post
Share on other sites
#include <stdio.h>
#include <string.h>

int main()
{
FILE* pFile = fopen("testfile.txt", "w");
char* message = new char[9];
message[8] = 0;
strcpy(message, "Hello!!\n");
fwrite((void*)message, sizeof(char), 9, pFile);
fwrite((void*)message, sizeof(char), 9, pFile);
char value[] = "10000";
fwrite((void*)&value, sizeof(char), 5, pFile);
}

char value[] = "10000";....it should be a string so that can be output as text stream instead of binary stream

Share this post


Link to post
Share on other sites

This topic is 4865 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this