Jump to content
  • Advertisement
Sign in to follow this  
mckoo

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

This topic is 5173 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
Advertisement
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
thx in advance

is that content "65" the ascii code that is used with fwrite() will be converted into characters and printed to a file?

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!