hi,
how to calculate the size of a file after opening and write somthing in to file.
fp=fopen(PATH,"a+");
fprintf(fp,"%s\n","text");
after writing into file how we know memory occupied by file.
thnx in advance.
Printable View
hi,
how to calculate the size of a file after opening and write somthing in to file.
fp=fopen(PATH,"a+");
fprintf(fp,"%s\n","text");
after writing into file how we know memory occupied by file.
thnx in advance.
Huh?
Well the most portable way to measure it's size would be to open it (preferably in read-only), seek to the end and find out the pos of the pointer, look into fseek() and ftell()
Calling ftell() before and after the fprintf() might tell you something.
"text" == 5 bytes.
But the actual size on disk may be more, at least the block size.
What robwhit is saying is that there are "two different file-sizes". The first, and most obvious one is the number of bytes "actually in use" in the file. The second one is the number of blocks or clusters of blocks the file occupies, which may be more than the "in use" size.
By the way robwith: text is a variable, so it's not easy to determine it's length from just looking at the code.
As suggested, if you want to know how long the file is "in actual use", just use ftell (possibly combined with fseek).
Note also that you can figure out "how much fprintf() printed" by taking it's return-value - all printf type functions return a count of the number of bytes they wrote to the destination. So if you know the size (in use) of the file originally, you can calculate the new size by using the returned count from printf.
--
Mats
fprintf returns number of characters (not bytes) printed... in text mode \n character can be represented by 2 bytes (on windows for example) so the file size may increase more than the number returned by printf...
vart, you are indeed correct - a newline is counted as one char, even if the machine/OS definition of "newline" is CR+LF.
And I also suspect that "wide chars" printf returns a count of how many wide-chars, rather than the number of bytes printed.
--
Mats
When newlines are CR+LF? :) But you're right, with that code snippet it's easy to tell. Anyway, I'm sure that code was just a sample, and the OP's real code is more complicated.Quote:
What scenario would it not be 5 bytes?
[edit] matsp beat me. [/edit]
a user could enter some formatting characters (%) and mess with the stack. So use this:Code:fgets(buf, sizeof(buf), stdin);
printf(buf); /* uh oh */
I guess it's just habit.Code:fgets(buf, sizeof(buf), stdin);
printf("%s", buf);
does this same thing asCode:printf("%s\n", "some constant text");
though. Preference as to which one to use, I guess.Code:puts("some constant text");
Yes, of course - at least unless you know for sure this isn't going to move anywhere (it's no point in writing portable code when you are doing OTHER unportablt stuff).
My point about printf and puts was that:
gets converted by gcc toCode:printf("Something to print\n");
Anyways, this isn't adding anything to the original post, so lets cut it now...Code:puts("something to print");
--
Mats