Hi all,
I wish to write the contents of a large char array into a file. I open the file for writing and say, fp is the pointer to the file.
and I write :
fprintf(fp,"%s",buff);
Does fprintf write the whole array into the file or is there any inbuilt restriction.Because ,the final expeced file is not containing the whole array, the file writing process stops at a certain stage..actually this is a part of a bigger prog and I am speculating that the problem might lie here.. Can some one explain the internal working of this procedure..
thanks

Recommended Answers

All 4 Replies

It will write the contents of buff up the the null. Assuming that it is a char array as opposed to a string, and the char array contains embedded nulls, you will get less output than expected. Perhaps use fwrite instead.

Try with:
fwrite (fp,i*sizeof(char),1,buff);
i is a number of characters that you want to write to file.
You have declaration of this function in stdio.h.
i also can be a strlen(fp).
strlen declaration is in string.h

>> fwrite (fp,i*sizeof(char),1,buff);

there is no need for sizeof(char) because it is guaranteed by the language to always be 1 regardless of platform or compiler. Just makes less typing and fewer characters to read :mrgreen:

fwrite (fp,i,1,buff);

i also can be a strlen(fp).
strlen declaration is in string.h

Yeah, that'll do wonders for any embedded nulls. :rolleyes:

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.