Thread: File

  1. #1
    Registered User
    Join Date
    Aug 2011
    Posts
    385

    File

    Why does the following code report the number of characters to be 6 even if we enter only 2...

    Code:
    #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. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,667
    No idea - it works here.
    Code:
    $ cat bar.c
    #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);
    
    }
    $ gcc bar.c
    $ ./a.out 
    Hi
    
    No. of characters = 3$
    The three characters being "Hi" and a newline.

    Perhaps open your "random" file in a hex editor and see what extra bytes you have.
    (or even your text editor, if it doesn't barf on non-text files).
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Aug 2011
    Posts
    385
    Quote Originally Posted by Salem View Post
    No idea - it works here.
    Code:
    $ cat bar.c
    #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);
    
    }
    $ gcc bar.c
    $ ./a.out 
    Hi
    
    No. of characters = 3$
    The three characters being "Hi" and a newline.

    Perhaps open your "random" file in a hex editor and see what extra bytes you have.
    (or even your text editor, if it doesn't barf on non-text files).
    created a file with two characters 'a' and 'b'.
    Only two characters ('a' and 'b') appear in notepad, but 6 characters with hexadecimal values 61 0D 0A 62 0D 0A appear in hex editor.
    0D and 0A automatically get appended to any character I write.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,667
    Well that's OK then - you're using DOS/Windows, and each line ends with both a newline and a carriage return.

    If you enter "ab" then press return, then ctrl-z, you should see 4 chars.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,667
    Keep practising, and using your hex editor - it'll come to you eventually.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    Aug 2011
    Posts
    385
    ..... and I was excited that I have another virus peacefully reciding in my PC. Thanks!

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by juice View Post
    created a file with two characters 'a' and 'b'.
    Only two characters ('a' and 'b') appear in notepad, but 6 characters with hexadecimal values 61 0D 0A 62 0D 0A appear in hex editor.
    0D and 0A automatically get appended to any character I write.
    0xOD = carriage return
    0x0A = line feed

    The result of pressing <Enter> after each letter... windows writes cr/lf pairs ...
    Last edited by CommonTater; 12-18-2011 at 03:33 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 11
    Last Post: 09-25-2011, 12:22 AM
  2. Replies: 3
    Last Post: 07-17-2011, 03:51 AM
  3. Replies: 3
    Last Post: 03-08-2010, 02:43 PM
  4. Replies: 3
    Last Post: 11-21-2006, 07:26 PM
  5. Replies: 4
    Last Post: 07-06-2006, 02:53 AM