tags:

views:

237

answers:

5

Instead of appending "abc" (and thus eventually getting a file full of abcabcabc....), no matter how many times I run this, myfile only containst "abc".... how can I append?

#include <stdio.h>
#include <string.h>

int main(){
    char strng[10];
    strcpy(strng,"abc");
    FILE *my_file;
    my_file = fopen("myfile","a+");
    if (my_file == NULL){ printf("problem\n");}
    fwrite(strng, sizeof(strng), 1, my_file);
    printf("appending %s\n",strng);
    fclose(my_file);
}
+4  A: 

Apart from the fact that:

fwrite(strng, sizeof(strng), 1, my_file);

should be:

fwrite(strng, strlen(strng), 1, my_file);

it works for me.

anon
Yeah, the original code would write '\0' into the file, and whatever he was viewing the file with later must have handled the file contents as a C string (nul-terminated). Instead of fwrite(strlen), use fputs!
Peter Cordes
A: 

Don't use sizeof, use strlen. sizeof is 10, so you're writing "abc\0\0\0\0\0\0\0".

Mike Dunlavey
Actually, he's writing "abc\0..." where ... is 6 chars of random trash
SiegeX
@SiegeX: You're right.
Mike Dunlavey
A: 

That program works and does what you said to do: append 10 characters (sizeof(strng)) to file, including \0 and rest of the array. Change sizeof to strlen.

Besides that you should not only print "problem" when you can't open file, but also not write to it.

And last problem in your code - you declared main as returning int, but end program without setting return code. If you end program correctly, always return EXIT_SUCCESS.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define BUF_SIZE 10
char buf[BUF_SIZE];

int main(int ac, char **av) {
    FILE *my_file;

    strncpy(buf, "abc", BUF_SIZE-1);
    buf[BUF_SIZE-1] = '\0';
    my_file = fopen("myfile", "a+");
    if (my_file == NULL) {
        fprintf(stderr, "problem opening file\n");
        return EXIT_FAILURE;
    } else {
        fwrite(buf, strlen(buf), 1, my_file);
        printf("appending %s\n", buf);
        fclose(my_file);
        return EXIT_SUCCESS;
    }
}
MBO
I also don't like the FILE* declaration after the strcpy().
SiegeX
I don't like it too. I only modified that program to correct problems
MBO
strncpy() is actually slightly evil in that if the size of the source string is the same as the target length, it will not zero-terminate. You can copy BUF_SIZE-1 characters and then manually write the last one as zero. Or you can use one of the more intelligent replacements, such as strlcpy() many Unixy systems (except Linux, sadly), or StringCchCopy() on Windows.
asveikau
A: 

When you fopen the file, the stream position is at the beginning of the file. Surely you have to seek to the end before writing in order to get the string appended?

Tony van der Peet
the "a+" mode positions the write position at the end of file.
anon
No, he opens file in read and append mode, that's what `a+` means
MBO
Thanks for that Neil, I withdraw my answer! If this is the case then original code should be producing a bigger file, even if what's in it is not displayable. In fact looking at the file size as it increases would be a good clue to the sizeof/strlen bug.
Tony van der Peet
A: 

I got it to work using strlen instead of sizeof. Thanks for the tips everyone!

Al
Please don't answer your own questions with "I got it working, thanks". Reply to the appropriate answer instead.
asveikau