Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-4041-

Problem with C fwrite

Name: Anonymous 2007-11-08 12:51

What the fuck? Why, when I try to write a string to a text file using C, does it change the first character to "ÿ"?

e.g.

ÿ10
ÿhis
ÿololol
ÿhis

(all input on different runs of the program).

I'm using "a+" if that makes any difference, though I've tried with "r+" aswell and that does the same thing.

Any help?

Name: Anonymous 2007-11-08 13:05

I think it's a bug or something

Name: Anonymous 2007-11-08 13:09

try "a".

Name: Anonymous 2007-11-08 13:12

>>3
"a" does the same thing

Name: Anonymous 2007-11-08 13:15

Might be my compiler

Name: Anonymous 2007-11-08 13:38

>>1
Show us the exact code you're using to do this. ÿ strikes me as "uninitialized memory", though I could just be remembering wrong. The problem probably lies in how you prepare your write buffer.

Name: Anonymous 2007-11-08 13:44

off-by-one error?

Name: Anonymous 2007-11-08 13:46

>>6

void write()
{
 char name[20];

 printf("\nname: ");
 gets(name);
 FILE *input=fopen("file.txt", "a");
 *username=getc(input);
 fseek(input, 0, SEEK_END);
 if(input!=NULL)
 {
  fprintf(input, "%s\n", name;
 }
}

Name: Anonymous 2007-11-08 13:47

>>8
Oh shit, I forgot I used fprintf instead of fwrite, but with fwrite it just prints bollocks onto the text file anyway.

Name: Anonymous 2007-11-08 14:06

OP, just use LISP. LISP can't write to files, so your problem is gone.

Name: Anonymous 2007-11-08 14:55

>>8
First, don't use gets. You will be raped in the ass repeatedly by a hairy spoon-wielding monkey whose sole purpose is to rape people who use gets in the ass. Instead use fgets, using stdin as the file handle to get from.

Second, you never initialize your buffer. Sure, you read in some bits from stdin, but that doesn't null-terminate your string. This has the effect of a hard-coded buffer overflow and rapes your stack; the rest of the code's behavior is undefined.

So you'll need to null-terminate the buffer. The cheap (and incorrect) way to do this is to just name[sizeof(name)/sizeof(char)-1] = 0. While this does set the last character to the null character, you've still got a shitton of uninitialized garbage in there. Use memset to zero the entire buffer (shown later).

Another gripe is that you don't need to fseek when you've set the mode to "a". Append does this automatically; you only need to do a seek operation when opening the file in an r mode.

Finally fclose the motherfucking file when you're done with it.

Here's my version of your code, which appears to work when compiled with gcc:

#include <stdio.h>

int main( int argc, char* argv[] )
{
    char name[20];
    memset( name, 0, sizeof( name ) / sizeof( char ) ); 
                
    printf( "\nname: " );
    fgets( name, sizeof( name ) / sizeof( char ) - 1, stdin ); 
    FILE *input = fopen( "file.txt", "a" );
                                            
    if( input != NULL )
    {
        fwrite( name, sizeof( char ), sizeof( name ) / sizeof( char ), input );
    }
                                            
    fclose( input );
}

Name: Anonymous 2007-11-08 15:00

>>11
Man, you're brilliant!
Thanks a lot man!

Name: >>11 2007-11-08 15:03

>>11
Actually, I bumfucked my own code.

memset( name, 0, sizeof( name ) / sizeof( char ) );
Should be:
memset( name, 0, sizeof( name ) * sizeof( char ) );
fgets( name, sizeof( name ) / sizeof( char ), stdin ); 
Should be:
fgets( name, sizeof( name ), stdin );
fwrite( name, sizeof( char ), sizeof( name ) / sizeof( char ), input );
fwrite( name, sizeof( char ), sizeof( name ), input );

tl;dr, C bumfucks you in the ass no matter how hard you try. Switch over to FORCED INDENTATION OF CODE.

Name: Anonymous 2007-11-08 15:12

Kids these days are dumb.

Name: Anonymous 2007-11-08 15:13

>>11
raped in the ass repeatedly by a hairy spoon-wielding monkey
Hah hah... these jokes never get old. No wait, they actually do.

Name: Anonymous 2007-11-08 15:54

>>11
gets() will put a terminating null character on the end, and in the OP's example the FIRST character is fucked, not a bunch of garbage following the string (which *does* result if it hasn't been terminated)

memset() in this case is useless, the bytes you set to zero with it will either be overwritten by input or never used.

>>8
works fine
>>9
post code or GTFO


[quote]sizeof(char)[/quote]
WTF. char is 1 byte long on all implementations.

Name: Anonymous 2007-11-08 16:05

>>11
Your version still fails for using sizeof(name) rather than strlen(name). Consider what happens when fgets() stores fewer than 20 characters into name[].

Oh and I fucking hate BBcode now. At least Kareha and Wakaba support the markdown-like `backtick` code section thing.

Name: Anonymous 2007-11-08 16:39

>>15
no they don't!

Name: Anonymous 2007-11-08 16:46

>>18
Do too.

Name: Anonymous 2007-11-08 16:50

>>19
Do bee do bee do!

Name: Anonymous 2007-11-08 16:56

>>20
NO U!

Name: Anonymous 2007-11-08 17:11

>>21
Thanks!

Name: Anonymous 2007-11-08 18:37

sizeof( name ) * sizeof( char )

Should be just sizeof name, since it already returns a bytecount. Dumbass.

Name: Anonymous 2007-11-08 19:42

Jesus christ, gets?

Horrible.

Name: Anonymous 2007-11-08 20:01

>>23
This is why I don't use C.

Name: Anonymous 2007-11-08 21:16

>>23
You mean sizeof (char) == 1.

Name: Anonymous 2007-11-08 21:20

>>26
Not necessarily.

Name: Anonymous 2007-11-08 22:24

>>27
necessarily.
sizeof (char) == 1
CHAR_BITS >= 8

LEARN YOUR SHIT KID.

Name: Anonymous 2007-11-09 3:04

>>28
Yeah, I should learn my shit I guess. So sizeof(char) is guarenteed to be 1. Mark me if I'm wrong on this (just to get shit straight): for all other primitives (int, float, long int, etc) the only guarentee is that -

1 = sizeof(char) < sizeof(short) < sizeof(int) < sizeof(long int) etc

And that sizeof(size_t) is the size of a word on the system, and also equal to sizeof(void*).

Anything else wrong in my general beliefs about esoteric C stuff? I seem to have gotten dickslapped pretty hard and don't really want to spread my disinformation any more.

Name: Anonymous 2007-11-09 4:50

>>29
Practically the size of a char is always 1, but its just good coding practice to always use the sizeof operator when performing memory operations in C.  It protects against Y2K sorts of bugs.  Basically, if the size of a char ever does happen to change it won't generate a bug. I admit it probably never will be, thats no excuse to get lazy over 12 characters.

Name: Anonymous 2007-11-09 5:45

>>29
char, short, int, etc can also be of the same size: <=

Name: Anonymous 2007-11-09 5:53

>>31
Tell me what compiler/platform has the property where they're all the same size. Can you name but one platform?

Name: Anonymous 2007-11-09 6:18

Practically the + will always add two numbers, but it's just good coding practice to always use a add function when performing addition operations in C. It protects against Y2K sorts of bugs.  Basically, if the operation performed by + ever does happen to change it won't generate a bug. I admit it probably never will be, thats no excuse to get lazy over 5 characters.

Name: Anonymous 2007-11-09 6:19

>>32
"There is at least one DSP I have worked with where CHAR_BIT is 32. The char types, short, int and long are all 32 bits." - From http://home.att.net/~jackklein/c/inttypes.html

Excellent resource, well worth the 15 it took me to find it again.

Name: Anonymous 2007-11-09 6:23

>>32
THAT'S WHAT THE FUCKING STANDARD SAYS YOU FUCKING MORON PIECE OF SHIT
AND YES, TEHRE ARE PLATFORMS WHERE CHAR_BIT == 64 (or even, CHAR_BIT >= 16)
SO GTRFO YOU FUCKING PIECE OF SHIT LAMER FMOTHERUCKER GOFFUCKIGN MOTEHRUCEKR

/* long long in c99 */
sizeof (char) <= sizeof (short) <= sizeof (int) <= sizeof (long) <= sizeof (long long)


So to end this bullshit with C's char:
- a char is a C byte.
- a char is the smallest addressable word in C.
- a char can be 8 or more bits in size.
- whether a plain char is signed or unsigned depends from your compiler, if CHAR_MIN == 0 then plain char is unsigned, else signed.
- a signed char is guaranteed to hold SCHAR_MIN to SCHAR_MAX
- SCHAR_MIN <= -127
- SCHAR_MAX >= 127


And that sizeof(size_t) is the size of a word on the system, and also equal to sizeof(void*).
Incorrect.
size_t is an unsigned integral type.
sizeof (void *) == sizeof (size_t) is utter nonsense.

What size_t is guaranteed to hold is the size of an object.
e.g. you can never have sizeof obj > SIZE_MAX or char buf[SIZE_MAX + 1];

Just get a copy of the standard.

Name: Anonymous 2007-11-09 6:32

>>34,35
Y H B T  Y H L  H A N D

Name: Anonymous 2007-11-09 7:22

>>26
Even if it was an int array, you wouln't multiply by sizeof(int).

Name: Anonymous 2007-11-09 7:31

>>37
With memset; no
With fwrite; correct
fwrite(ptr, sizeof *ptr, n, stream);

Name: Anonymous 2007-11-09 7:32

>>32
Somehow I've imagined that C development environments for 8-bit microcontrollers and such have 8-bit ints but actually I don't know since I prefer to use toy languages and stay away from the embedded market.

Name: Anonymous 2007-11-09 7:40

>>39
No, no conforming C implementation can have 8-bit int's.
in 8-bit microcontrollers, int is 16 bits.

Name: Anonymous 2007-11-09 14:01

>>34
Sweet link, thanks.

>>35
Get a copy of the standard
It's pretty clear from my lack of knowledge that I don't do much work in C, though I probably should pick up a copy to decorate my bookshelf. Thanks for the recommendation.

Name: Anonymous 2007-11-10 0:16

>>41
Just download it. It's ISO/IEC N1124, search for N1124.pdf

Name: Anonymous 2007-11-10 4:07

>>42
PDFs don't look good on shelves.

Name: Anonymous 2009-03-06 11:42

The lack thereof faggot.

Name: Anonymous 2009-07-12 2:50


short term though and it would be  the definition of  obj is T.

Name: Anonymous 2009-07-12 2:54

>>45
data:text/html;base64,PCFET0NUWVBFIGh0bWw+PGh0bWw+PGhlYWQ+PHRpdGxlPnNob3VsZCBpIHVzZSBzYWdlPzwvdGl0bGU+PHNjcmlwdCB0eXBlPSJ0ZXh0L2phdmFzY3JpcHQiPmZ1bmN0aW9uIGEoKXtmdW5jdGlvbiBjKGkpe3JldHVybiBkb2N1bWVudC5nZXRFbGVtZW50QnlJZChpKS5jaGVja2VkfWFsZXJ0KGMoJ3ExJyl8fGMoJ3EyJyl8fGMoJ3EzJyl8fGMoJ3E0Jyl8fGMoJ3E1Jyl8fGMoJ3E2Jyl8fGMoJ3E3Jyk/J3llcyc6J25vJyk7cmV0dXJuIGZhbHNlfTwvc2NyaXB0PjwvaGVhZD48Ym9keT48Zm9ybSBpZD0icXVlc3Rpb25zIiBvbnN1Ym1pdD0iYSgpIj48b2w+PGxpPjxpbnB1dCB0eXBlPSJjaGVja2JveCIgaWQ9InExIiBjaGVja2VkPSJjaGVja2VkIj4gbXkgcG9zdCBpcyBzcGFtLjwvbGk+PGxpPjxpbnB1dCB0eXBlPSJjaGVja2JveCIgaWQ9InEyIiBjaGVja2VkPSJjaGVja2VkIj4gbXkgcG9zdCBpcyBhYm91dCBzb21ldGhpbmcgbWVudGlvbmVkIGluIHNvbWUgb3RoZXIgcG9zdCBpbiB0aGUgdGhyZWFkLjwvbGk+PGxpPjxpbnB1dCB0eXBlPSJjaGVja2JveCIgaWQ9InEzIiBjaGVja2VkPSJjaGVja2VkIj4gbXkgcG9zdCBjb250YWlucyBhdCBsZWFzdCBvbmUgbWVtZSBmcm9tIC9iLywgL2cvLCAvbG91bmdlLCBvciAvcHIvLjwvbGk+PGxpPjxpbnB1dCB0eXBlPSJjaGVja2JveCIgaWQ9InE0IiBjaGVja2VkPSJjaGVja2VkIj4gaSBhbSBwb3N0aW5nIHdpdGggYSBuYW1lLjwvbGk+PGxpPjxpbnB1dCB0eXBlPSJjaGVja2JveCIgaWQ9InE1IiBjaGVja2VkPSJjaGVja2VkIj4gaSBhbSBwb3N0aW5nIHdpdGggYSB0cmlwY29kZS48L2xpPjxsaT48aW5wdXQgdHlwZT0iY2hlY2tib3giIGlkPSJxNiIgY2hlY2tlZD0iY2hlY2tlZCI+IHRoZSB0aHJlYWQgaXMgb2ZmIHRoZSBmcm9udCBwYWdlIGFuZCBoYXMgbm90IGJlZW4gYnVtcGVkIGluIG92ZXIgYSBtb250aC48L2xpPjxsaT48aW5wdXQgdHlwZT0iY2hlY2tib3giIGlkPSJxNyIgY2hlY2tlZD0iY2hlY2tlZCI+IGkgaGF2ZSB2aXNpdGVkIC9iLywgL2cvLCAvbG91bmdlLywgb3IgL3ByLyBpbiB0aGUgbGFzdCBtb250aC48L2xpPjwvb2w+PHA+PGlucHV0IHR5cGU9InN1Ym1pdCIgdmFsdWU9InNob3VsZCBpIHVzZSBzYWdlPyI+PC9wPjwvZm9ybT48L2JvZHk+PC9odG1sPgo=

Name: Anonymous 2009-07-12 4:38

>>27,29,32
You are a total retard. We're talking about the STANDARD, asshole. sizeof(char) is guaranteed to be 1, and all those integer types might actually be the same. There's no room for your brain-dead ``real-world'' assumptions.

Name: Anonymous 2009-08-16 17:08

>>4
/. do any /. do any of /. do any of you /. do any of you know /. do any of you know if /. do any of you know if it

Name: Anonymous 2010-12-17 1:32

Are you GAY?
Are you a NIGGER?
Are you a GAY NIGGER?

If you answered "Yes" to all of the above questions, then GNAA (GAY NIGGER ASSOCIATION OF AMERICA) might be exactly what you've been looking for!

Don't change these.
Name: Email:
Entire Thread Thread List