Thread: Contradictory outputs

  1. #16
    Registered User andrew89's Avatar
    Join Date
    Dec 2011
    Location
    Indiana, United States
    Posts
    80
    Odd... it worked twice for me. Came out as:
    Code:
    a a a a
    Last edited by andrew89; 12-26-2011 at 04:50 AM. Reason: forgot the last 'a'

  2. #17
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Mmm, seems to work OK here on Ubuntu/gcc (with the unnecessary conio.h removed)
    Code:
    $ gcc -o enc bar.c
    $ gcc -o dec foo.c
    $ ./enc 
    a b $ 
    $ odx output.txt 
    000000 00 1a 01 1a                                      >....<
    000004
    $ ./dec
    $ odx output.txt 
    000000 61 20 62 20                                      >a b <
    000004
    Ideally, putc(c[i],fp); should be followed with fflush(fp) as well.
    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. #18
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    In your program, the variable t is a char - and a char can't test for EOF. Make it an int (but print it out as a char, just like normal).

  4. #19
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    That fseek() call is suspicious. The third argument is supposed to be one of the values SEEK_SET, SEEK_CUR, or SEEK_END. The value 1 may be any of those, or none of those.

    It doesn't help that t is of type char. EOF as a value need not be representable as a char.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  5. #20
    Registered User andrew89's Avatar
    Join Date
    Dec 2011
    Location
    Indiana, United States
    Posts
    80
    @Salem, same compiler/OS here.

    Everyone else, I didn't even bother looking at the code so much. conio.h isn't my most liked of subjects.

  6. #21
    Registered User
    Join Date
    Aug 2011
    Posts
    385
    wow.... earlier only decoder was getting stuck in an infinite loop, now when I encode the file a character with ASCII value 164 gets appended at the end of the file. wtf...

  7. #22
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    The only apparent issue for DOS/Win machines would be that space is encoded as 0x1A (otherwise known as EOF - see previous threads).

    But this would only be a real problem were the files being opened in text mode.

    Assuming the "b" mode is accurate (and not otherwise bugged in the compiler/run-time), then things ought to work as expected.

    There are some nits to fix for sure, but no glaring "OMG, it'll never work" things.
    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.

  8. #23
    Registered User
    Join Date
    Aug 2011
    Posts
    385
    Don't know wats wrong, but just wanted to ask whether putc and getc are the appropriate functions to be used in binary mode, or do they cause problems just like ftell() does in text mode..
    Last edited by juice; 12-26-2011 at 09:46 AM.

  9. #24
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Like I said, nothing glaringly obvious.

    But you could do this, and tell us what you see.
    Code:
    #include<stdio.h>
    #include<string.h>
    #include<stdlib.h>
    #include<conio.h>
    
    int main(void)
    {
        int i,j;
        char c[]="abcdefghijklmnopqrstuvwxyz ";
        int t;
        char d[]={0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26};
        FILE *fp;
    
        fp=fopen("output.txt","rb+");
        if(fp==NULL)
            {
                printf("Cannot open file");
                            exit(0);
             }
    
        while((t=getc(fp))!=EOF)
        {
            fprintf(stderr,"Read %d\n",t);
            i=0;
            while(d[i]!=t)
                i++;
            fprintf(stderr,"Lookup index %d\n",i);
    
            if ( fseek(fp,-1L,SEEK_CUR) == -1 ) {
                fprintf(stderr,"seek ERROR\n");
                break;
            }
    
            if ( putc(c[i],fp) == EOF ) {
                fprintf(stderr,"write ERROR\n");
                break;
            }
        }
        fclose(fp);
        return 0;
    }
    Basic programming skills - if it isn't working, then use the debugger and/or add some code to print debug information and check EVERY POSSIBLE STATUS result.
    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.

  10. #25
    Registered User
    Join Date
    Aug 2011
    Posts
    385
    Output with Pelles C:
    Read 0
    Lookup Index 0

    Output with MSVC 2010:
    Read 26
    Lookup index 26
    and the values keep repeating indefinitely. Had to use pause/break to see the results...

  11. #26
    Registered User andrew89's Avatar
    Join Date
    Dec 2011
    Location
    Indiana, United States
    Posts
    80
    Strange... seems your compiler (Pelles C anyway) has trouble reading EOF's... at least I think that's what is going on.
    Code:
    // Salem's code with output.txt having the below in it:
    // a a 
    // a[space]a[space]
    andrew@home:~$ ./har
    Read 97
    Lookup index 5451
    Read 32
    Lookup index 7732
    Read 97
    Lookup index 5451
    Read 32
    Lookup index 7732
    Read 10
    Lookup index 10
    I don't know if this is any better, but I hope someone can at least use this to identify an error or something. I'm far too tired to figure out what Salem just got my computer to yell at me, so I'm going to take a nap and look again with a 'hopefully' clear head.
    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.

  12. #27
    Registered User
    Join Date
    Aug 2011
    Posts
    385
    Quote Originally Posted by Salem View Post
    Basic programming skills - if it isn't working, then use the debugger and/or add some code to print debug information and check EVERY POSSIBLE STATUS result.
    Maybe am not that good at debugging, but my whole day has been lost in just trying to figure out whats wrong with this code, and thats why my message#1 was about ftell, cause I planted ftell at various positions to figure out where the file pointer is flowing.
    Besides, I planted printf's after every instruction to figure out whats wrong and all I could get was that the contents of t were not changing after the first iteration in the while loop under MSVC. I wonder if theres anything else I could try cause this is all I know..

  13. #28
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > don't know if this is any better, but I hope someone can at least use this to identify an error or something
    Yes, you're trying to decode an input file.

    > Read 97
    Otherwise known as 'a'.

    > Lookup index 5451
    Since 97 isn't in the d lookup array, it just wanders through memory racking up the value of i until some random location matches the input in t.
    Or it crashes with a memory fault.

    > Maybe am not that good at debugging, but my whole day has been lost in just trying to figure out whats wrong with this code,
    Trust me, you'll have many more whole days of confusion ahead of you.

    Like I said some posts ago, add fflush(fp) to the loop.
    Also (for debug), print the result of ftell(fp) as well - to see if you're stepping through the file OK.
    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.

  14. #29
    Registered User
    Join Date
    Aug 2011
    Posts
    385
    fflush worked with MSVC, but the program terminates after first iteration of while loop in Pelles C. Here is a version of the code for decoder which works with both the compilers, the only modification is line no. 31. Can't understand why the code didn't work without fflush cause the file pointer was flowing correctly, and also, why the fflush didn't work with pelles C.

    Code:
    #include<stdio.h>
    #include<string.h>
    #include<stdlib.h>
    #include<conio.h>
     
    int main(void)
    {
        int i,j;
        char c[]="abcdefghijklmnopqrstuvwxyz ",t;
        char d[]={0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26};
        FILE *fp;
     
        fp=fopen("output.txt","rb+");
        if(fp==NULL)
            {
                printf("Cannot open file");
                            exit(0);
             }
         
         
        while((t=getc(fp))!=EOF)
        {
             
            i=0;
            while(d[i]!=t)
                i++;
             
            fseek(fp,-1L,1);
             
            putc(c[i],fp);
            fseek(fp,ftell(fp),SEEK_SET);
        }
        fclose(fp);
    }

  15. #30
    Registered User
    Join Date
    Aug 2011
    Posts
    385
    Can anyone tell me why we had to use fflush in this code even though the file pointer was moving around correctly.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Different outputs on different OS
    By darksifer in forum C Programming
    Replies: 13
    Last Post: 10-19-2010, 01:45 PM
  2. why i getting different outputs?
    By nkrao123@gmail. in forum C Programming
    Replies: 2
    Last Post: 12-08-2009, 08:33 AM
  3. Outputs
    By kas2002 in forum C++ Programming
    Replies: 3
    Last Post: 10-29-2002, 07:12 PM
  4. Replies: 28
    Last Post: 08-21-2002, 05:30 PM
  5. contradictory if statements
    By red_baron in forum C Programming
    Replies: 13
    Last Post: 05-04-2002, 06:52 PM