Like Tree3Likes

WhY the difference in outputs?

This is a discussion on WhY the difference in outputs? within the C Programming forums, part of the General Programming Boards category; Code: #include<stdio.h> int main(void) { char c[]={26,33}; char t; FILE *fp; fp=fopen("random.txt","wb"); putc(c[0],fp); putc(c[1],fp); fclose(fp); fp=fopen("random.txt","rb"); while((t=getc(fp))!=EOF) printf("%c(%d)\n",t,t); fclose(fp); } ...

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

    WhY the difference in outputs?

    Code:
    #include<stdio.h>
    
    int main(void)
    {
        char c[]={26,33};
        char t;
        FILE *fp;
     
        fp=fopen("random.txt","wb");
         
        putc(c[0],fp);
        putc(c[1],fp); 
       
        fclose(fp);
    
        fp=fopen("random.txt","rb");
    
        while((t=getc(fp))!=EOF)
    	printf("%c(%d)\n",t,t);
    
    	fclose(fp);
    }
    Code:
    #include<stdio.h>
     
    int main(void)
    {
        char c[]={26,33};
        char t;
        FILE *fp;
     
        fp=fopen("random.txt","w");
         
        putc(c[0],fp);
        putc(c[1],fp); 
       
        fclose(fp);
    
        fp=fopen("random.txt","r");
    
        while((t=getc(fp))!=EOF)
    	printf("%c(%d)",t,t);
    
    	fclose(fp);
    }
    The only difference in the above two codes is that in the first code the file random.txt is opened in binary mode, whereas in the second code the file is opened in text mode, but still in the second code the control never enters the while loop because getc encounters ASCII 26 in the first run itself and returns -1 (EOF), but this doesn't happen in the first case and the control enters the while loop in the first case. Why??

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    So what happens if you change the variable t to an int (which it should be)?

    Gotta tell you, Juice - that is THE most distracting avatar, ever! The poor guy looks like he's having a fit!
    Last edited by Adak; 12-27-2011 at 12:04 AM.
    juice likes this.

  3. #3
    Registered User
    Join Date
    Aug 2011
    Posts
    385
    Quote Originally Posted by Adak View Post
    So what happens if you change the variable t to an int (which it should be)?
    Still the same results.. Please tell me why this difference in text mode and binary mode.


    Quote Originally Posted by Adak View Post
    Gotta tell you, Juice - that is THE most distracting avatar, ever!
    Don't worry its a cool avatar.
    Last edited by juice; 12-27-2011 at 12:16 AM.

  4. #4
    Registered User
    Join Date
    Aug 2011
    Posts
    385
    Am still confused whether -1 (EOF) is returned when fgetc() or fscanf() or any such standard function encounters the ASCII value 26, or whether it is returned when any of these functions reach the end of file...

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Run this:
    Code:
    #include <stdio.h>
    
    int main(void) {
       int c, i, j, k;
       FILE *fp;
             
       if((fp=fopen("test1", "wb")) == NULL) {
          printf("Error: unable to open the input file");
          return 0;
       }
       fprintf(fp, "%d %c %d\n",26,'e',35);
       printf("Writing two numbers and a letter, to a binary file\n\n1): 26\t2): e\t3): 35\n");
       fclose(fp);
     
       //get a word from the text file
       printf("\nClosing the file, and reopening it - and reading the data:\n\n");
       if((fp=fopen("test1", "rb")) == NULL) {
          printf("Error: unable to open the input file");
          return 0;
       }
       fscanf(fp,"%d %c %d",&i,&j,&k);
       
       printf("1): %d\t2): %c\t3): %d\n",i,j,k);
       fclose(fp);
       printf("\n\n");
       return 0;
    
    }
    In this case, putc() is producing an error, which puts an error onto the stream, and returns an EOF - but that has nothing to do with 26. That's just what putc() was designed to do.
    Last edited by Adak; 12-27-2011 at 12:46 AM.

  6. #6
    Registered User
    Join Date
    Aug 2011
    Posts
    385
    Quote Originally Posted by Adak View Post
    Run this:
    Code:
    #include <stdio.h>
    
    int main(void) {
       int c, i, j, k;
       FILE *fp;
             
       if((fp=fopen("test1", "wb")) == NULL) {
          printf("Error: unable to open the input file");
          return 0;
       }
       fprintf(fp, "%d %c %d\n",26,'e',35);
       printf("Writing two numbers and a letter, to a binary file\n\n1): 26\t2): e\t3): 35\n");
       fclose(fp);
     
       //get a word from the text file
       printf("\nClosing the file, and reopening it - and reading the data:\n\n");
       if((fp=fopen("test1", "rb")) == NULL) {
          printf("Error: unable to open the input file");
          return 0;
       }
       fscanf(fp,"%d %c %d",&i,&j,&k);
       
       printf("1): %d\t2): %c\t3): %d\n",i,j,k);
       fclose(fp);
       printf("\n\n");
       return 0;
    
    }
    In this case, putc() is producing an error, which puts an error onto the stream, and returns an EOF - but that has nothing to do with 26. That's just what putc() was designed to do.
    Couldn't understand what ur trying to tell. This code is running just fine.
    Last edited by juice; 12-27-2011 at 12:56 AM.

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I'm trying to say that when you reached the end of the file in the one example above, it was caused by putc() or getc(), returning an error - had nothing to do with 26 being in the file. That's just what putc() and getc() do on error - they return EOF.

    Although I have to say, I use either of these functions very much.

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,265
    Quote Originally Posted by juice
    Am still confused whether -1 (EOF) is returned when fgetc() or fscanf() or any such standard function encounters the ASCII value 26, or whether it is returned when any of these functions reach the end of file...
    Well...
    Quote Originally Posted by C99 Clause 7.19.1 Paragraph 3 (part)
    EOF
    which expands to an integer constant expression, with type int and a negative value, that is returned by several functions to indicate end-of-file, that is, no more input from a stream;
    Quote Originally Posted by C99 Clause 7.19.7.1 Paragraph 3
    If the end-of-file indicator for the stream is set, or if the stream is at end-of-file, the end-of-file indicator for the stream is set and the fgetc function returns EOF. Otherwise, the fgetc function returns the next character from the input stream pointed to by stream. If a read error occurs, the error indicator for the stream is set and the fgetc function returns EOF.
    Quote Originally Posted by C99 Clause 7.19.6.2 Paragraph 16
    The fscanf function returns the value of the macro EOF if an input failure occurs before any conversion. Otherwise, the function returns the number of input items assigned, which can be fewer than provided for, or even zero, in the event of an early matching failure.
    Therefore:
    • EOF is a negative integer value. Don't get hung up about it being -1, because that does not matter anyway.
    • EOF is returned by certain input related functions in the C standard library to denote end of file or some kind of input error.


    Quote Originally Posted by juice
    WhY the difference in outputs?
    This business about the ASCII value 26 is something implementation specific: I believe that you took up this investigation from your other thread, especially my post #57. Read my conclusion: "even if you get a count of 1, it does not mean that such a value is appended to files; it would mean that such a value is treated as an end-of-file marker". So that would be my answer: the difference in outputs is because in binary mode, 26 is not special, but in text mode, it is treated specially as an end-of-file marker.
    Tclausex likes this.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,761
    What makes you think EOF belongs in a text file? You can't type EOF, which makes the whole issue contrived. Of course if you use putc or getc in weird ways, like reading what should be a binary file, since you are writing non-human readable data, expect interpretations to be weird.

    I believe the failure case for the binary function fread or fwrite is just any number smaller than the number of things you told it to write or read. At least, it indicates a problem. If you want to know about EOF, specifically, after reading binary file, you have to call feof(stream) appropriately.

  10. #10
    Registered User andrew89's Avatar
    Join Date
    Dec 2011
    Location
    Indiana, United States
    Posts
    80
    So something like the following might work?:
    Code:
    fp=fopen("test1","wb") //write to file in binary
         foo write data to file
    fclose(fp);
    fp=fopen("test1","a+") // write EOF if it exists
    foo write EOF to file
    Would this possibly work?
    If, for some reason, I don't make any sense at all, it's highly likely that I've been up all night on some strange bender that isn't normal. I likely unarmed and far from dangerous. While I haven't been known to physically harm anyone or anything else, there have been unsubstantiated reports that I have driven others into both a hazy stupor as well as a blinding rage. Otherwise, I hope I helped.

  11. #11
    Registered User
    Join Date
    Aug 2011
    Posts
    385
    Quote Originally Posted by laserlight View Post
    If the end-of-file indicator for the stream is set, or if the stream is at end-of-file, the end-of-file indicator for the stream is set and the fgetc function returns EOF
    What is this end-of-file indicator? What is the difference between EOF and end-of-file indicator...

  12. #12
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,761
    So something like the following might work?:
    Code:
    fp=fopen("test1","wb") //write to file in binary
         foo write data to file
    fclose(fp);
    fp=fopen("test1","a+") // write EOF if it exists
    foo write EOF to file
    Would this possibly work?
    No. You can do it, but that would be wrong.

    What is the difference between EOF and end-of-file indicator...
    EOF indicates an error state, and using a specfic value helps us know what kind of error it is.

    You still can't type EOF, so it doesn't belong in a text file.
    Last edited by whiteflags; 12-27-2011 at 01:30 AM.

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,265
    Quote Originally Posted by andrew89
    So something like the following might work?
    I don't understand what you are suggesting in the first place. You can certainly write EOF to file, but then if EOF is -1, what you would be doing is writing -1 to file. It would lose its meaning as the EOF macro.
    andrew89 likes this.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  14. #14
    Registered User
    Join Date
    Aug 2011
    Posts
    385
    Wat the hell is going on.....

  15. #15
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,265
    Quote Originally Posted by juice
    What is this end-of-file indicator?
    Read up on the feof function.

    Quote Originally Posted by juice
    What is the difference between EOF and end-of-file indicator...
    The end-of-file indicator is opaque to you: it is a hidden value set such that feof will tell you that end-of-file condition for the file stream has been set.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Difference between the outputs.
    By psaikia in forum C Programming
    Replies: 9
    Last Post: 12-05-2011, 03:05 PM
  2. Different outputs on different OS
    By darksifer in forum C Programming
    Replies: 13
    Last Post: 10-19-2010, 02:45 PM
  3. why i getting different outputs?
    By nkrao123@gmail. in forum C Programming
    Replies: 2
    Last Post: 12-08-2009, 08:33 AM
  4. strings and outputs
    By Mule in forum C++ Programming
    Replies: 3
    Last Post: 03-02-2003, 05:39 PM
  5. Outputs
    By kas2002 in forum C++ Programming
    Replies: 3
    Last Post: 10-29-2002, 07:12 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21