Sign in to follow this  

question arose from the topic "fopen()? fwrite()"

This topic is 4863 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

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 why "int" value cannot work out with fwrite???

Share this post


Link to post
Share on other sites
The fwrite is writing 10000, it's just that you're writing it to a text file so it's printing the equivalent characters.

Also, you're writing 9 characters in the first 2 writes. This means you're printing the zero that terminates the string.

#include <cstdio>
#include <cstring>

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), strlen(message), pFile);
fwrite((void*)message, sizeof(char), strlen(message), pFile);
int value = 10000;
sprintf(message,"%d",value);
fwrite((void*)message, sizeof(char), strlen(message), pFile);
return 0;
}




The contents of testfile.txt

Hello!!
Hello!!
10000

Share this post


Link to post
Share on other sites
i understand you're asking about fwrite(), but if you want to write integers easier, you could use fprintf().

example: fprintf(pFile, "%s\n%s\n%i", message, message, value);

Share this post


Link to post
Share on other sites
Quote:
Original post by jorgander
i understand you're asking about fwrite(), but if you want to write integers easier, you could use fprintf().

example: fprintf(pFile, "%s\n%s\n%i", message, message, value);

Agreed. iostreams are also something you should look into if you haven't already.

Newport, eh? Not too many Rhode Islanders about (I wonder why that is :p). I'm from Westerly.

Share this post


Link to post
Share on other sites
I would just like to note that:

integer 0 != null character '\0'

but it works because strcpy is overwriting that the last character with a null character so this:


char* message = new char[9];
message[8] = 0;
strcpy(message, "Hello!!\n");


is wrong & redundant so this is all you need:


char* message = new char[9];
strcpy(message, "Hello!!\n");



also explicit casting to type pointer to void is redundant so this:


fwrite((void*)&value, sizeof(int), 1, pFile);


can be this:


fwrite(&value, sizeof(int), 1, pFile);


[Edited by - snk_kid on August 22, 2004 10:21:47 AM]

Share this post


Link to post
Share on other sites
Quote:
Original post by snk_kid
I would just like to note that:

integer 0 != null character '\0'

Are you sure?

When I add this:
message[8] = 0;
message[7] = '\0';


and break it, my watch says that message[0]-message[6] are garbage and message[7] and message[8] are 0.

A quick search yields:

Quote:
7.1.2 Character Strings as Arrays
The NULL character is written using the escape sequence '\0'. The ASCII value of NULL is 0

Share this post


Link to post
Share on other sites
Quote:
Original post by Woodsman
The fwrite is writing 10000, it's just that you're writing it to a text file so it's printing the equivalent characters.

Also, you're writing 9 characters in the first 2 writes. This means you're printing the zero that terminates the string.
*** Source Snippet Removed ***

The contents of testfile.txt
*** Source Snippet Removed ***


so it means 10000 can only be printed as a string?
i'm really confused....

Share this post


Link to post
Share on other sites
Quote:
Original post by mckoo
Quote:
Original post by Woodsman
The fwrite is writing 10000, it's just that you're writing it to a text file so it's printing the equivalent characters.

Also, you're writing 9 characters in the first 2 writes. This means you're printing the zero that terminates the string.
*** Source Snippet Removed ***

The contents of testfile.txt
*** Source Snippet Removed ***


so it means 10000 can only be printed as a string?
i'm really confused....

No. When you get those weird characters printed out, that is 10000. The point is that it is printing out 10000 in ascii. You're reading a text file, remember? When you fwrite an int of 65 it'll show up as the character 'A'. Get it? When you fwrite the character 'A', it's actually writing 65 to the file.

If you open it in a hex editor, you'll see 65 in hex.

When you open it in notepad or whatever, it converts that 65 to 'A'.


Look here.
You have to realize that with:
char a = 65;
char b = 'A';

a and b have the same value.

Share this post


Link to post
Share on other sites
Do you mean to say will 65 be converted to a character? Well, no. What really happens when you do this:
message[0] = 'h';
message[1] = 'e';
message[2] = 'l';
message[3] = 'l';
message[4] = 'o';
message[5] = '!';

What it is actually storing is this:
message[0] = 104;
message[1] = 101;
message[2] = 108;
message[3] = 108;
message[4] = 111;
message[5] = 33;

So what you're writing to the file is:
104 101 108 108 111 33 (only they're each 1 byte and in binary form)

When notepad reads
104 101 108 108 111 33
it converts it into ASCII, displaying:
hello!

A character is really just a byte (well the standard defines byte as char but thats irrelevant :p). So it has to store everything as a number. ASCII is a standard way of mapping numbers to letters.

There's nothing magic going on. If you use a hex editor or something that doesn't convert the file to ascii, you'll see the binary/hex version of the numbers:
104 101 108 108 111 33


In fact, open up notepad and type in hello!. Now save it. Use XVI32 to open it. That's what is 'really' there.

Share this post


Link to post
Share on other sites

This topic is 4863 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