Jump to content

Is it supposed to print out 2...

- - - - -

  • Please log in to reply
3 replies to this topic

#1
Tumour

Tumour

    Newbie

  • Members
  • Pip
  • 9 posts
The code below will printf out the no. of characters as 6, even if we enter only 2...
why.


#include<stdio.h>

#include<string.h>

 

int main()

{

	FILE *fp;

	char c;


	

	fp=fopen("random","w");


	while((c=getchar())!=EOF)

		putc(c,fp);


	printf("\nNo. of characters = %ld",ftell(fp));

	fclose(fp);


}



#2
Flying Dutchman

Flying Dutchman

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 889 posts
  • Location:::1
From cplusplus.com reference:

Quote

For text streams, the value is not guaranteed to be the exact number of bytes from the beginning of the file

A conclusion is where you got tired of thinking.
#define class struct    // All is public.

#3
Ancient Dragon

Ancient Dragon

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 400 posts
Also see my answer here
Visit Grandpa's Forums, a social networking forum, with family-oriented arcade games, blogs, discussion forums, and photo albums.

#4
fread

fread

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 787 posts
Wouldn't it be easier to just count how many characters you write to the file?

---------- Post added at 11:25 AM ---------- Previous post was at 11:04 AM ----------

Edit:I just saw looked at your post Dragon.
Perfection of means and confusion of ends seem to characterize our age. Albert Einstein :confused:




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users