Odd... it worked twice for me. Came out as:Code:a a a a
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'
Mmm, seems to work OK here on Ubuntu/gcc (with the unnecessary conio.h removed)
Ideally, putc(c[i],fp); should be followed with fflush(fp) as well.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
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.
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).
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.
@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.
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...
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.
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.
Like I said, nothing glaringly obvious.
But you could do this, and tell us what you see.
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.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; }
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.
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...
Strange... seems your compiler (Pelles C anyway) has trouble reading EOF's... at least I think that's what is going on.
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.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
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.
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..
> 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.
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); }
Can anyone tell me why we had to use fflush in this code even though the file pointer was moving around correctly.