Thread: Contradictory outputs

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

    Contradictory outputs

    The output of this code under pelles C is 0 0, and that under MSVC 2010 is 9 9, can someone please explain me wats wrong..

    Code:
    #include<stdio.h>
    #include<string.h>
    #include<stdlib.h>
    #include<conio.h>
    
    int main(void)
    {
        
    	FILE *fp;
    
    	fp=fopen("output.txt","r+");
    
    	fgetc(fp);
    	printf("%ld ",ftell(fp));
    	fgetc(fp);
    	printf("%ld",ftell(fp));
    }

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Read the documentation for ftell(). You have not opened the file in binary mode, so ftell() is not necessarily going to return any particular value.

    The C standard has this to say "For a binary stream, the value is the number of characters from the beginning of the file. For a text stream, its file position indicator contains unspecified information, usable by the fseek function for returning the file position indicator for the stream to its position at the time of the ftell call; the difference between two such return values is not necessarily a meaningful measure of the number of characters written or read."
    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.

  3. #3
    Registered User andrew89's Avatar
    Join Date
    Dec 2011
    Location
    Indiana, United States
    Posts
    80
    Here's something you may want to look at as well.

    C File I/O Tutorial - Cprogramming.com

  4. #4
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    If this part of grumpy's response is true: "For a text stream, its file position indicator contains unspecified information, usable by the fseek function for returning the file position indicator for the stream to its position at the time of the ftell call" then that's not what's happening here since the return values of ftell after each fgetc cannot return the stream to those locations (since the locations are different but the return values are the same). Also, I ran the code in MSVC2010 and it gives the expected answer of 1, 2 (as does gcc).

  5. #5
    Registered User
    Join Date
    Aug 2011
    Posts
    385
    Quote Originally Posted by oogabooga View Post
    I ran the code in MSVC2010 and it gives the expected answer of 1, 2 (as does gcc).
    wat the hell is wrong with my compiler...

  6. #6
    Registered User
    Join Date
    Aug 2011
    Posts
    385
    is there anyone else getting unexpected output..

  7. #7
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    The output is not unexpected.

    It is feasible for ftell() to return the same result before and after a call of fgetc(). For example, if end of file is reached, or if an error occurred opening the file. The OP has not tested for either of those conditions, and nothing has been said about contents of the file.

    The point of the text I quoted (which was directly from the C standard) is that the results of ftell() are not necessarily portable when used on a file opened in text mode (assuming the same file contents).
    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.

  8. #8
    Registered User
    Join Date
    Aug 2011
    Posts
    385
    Ok, here's the story.
    I am writing a substitution cipher which simply replaces any lower case letter with a predetermined number. But when I try to decode it using a decoder it gets stuck in an infinite loop. Can you figure out whats wrong with the decoder.

    Encoder
    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","wb+");
            
            if(fp==NULL)
            {
                    puts("Cannot open file");
                    exit(0);
             }
    	
    	while((t=getchar())!=EOF)
    	{
    		
    		i=0;
    		while(c[i]!=t)
    			i++;
    				
    		putc(d[i],fp);
    		
    	}
    	fclose(fp);
    }


    Decoder
    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);
    		
    	}
    	fclose(fp);
    }

  9. #9
    Registered User andrew89's Avatar
    Join Date
    Dec 2011
    Location
    Indiana, United States
    Posts
    80
    Read over your source and pay very close attention to your variable 't' especially when it's involved in that while loop and during the initiation of your array.

  10. #10
    Registered User
    Join Date
    Aug 2011
    Posts
    385
    I've done that and placed debug code at various places to figure out wats wrong, the first letter is getting decoded and stored in the file correctly but then the program gets stuck at second letter(space bar ASCII value 32 in my case). I wonder wats wrong.

  11. #11
    Registered User
    Join Date
    Aug 2011
    Posts
    385
    HOLY... F***...
    Can someone understand and tell me wats wrong with this stupid code.....

  12. #12
    Registered User andrew89's Avatar
    Join Date
    Dec 2011
    Location
    Indiana, United States
    Posts
    80
    First, language not appreciated.

    Second, it's your fault, so don't yell at us like we're somehow hindering you.

    Lastly, I can't help you until you find some alternative to using conio.h as my operating system--Ubuntu Lucid--won't even compile using that header, much less be able to make any kind of use out of it.

    If that's you're current code, can you post the contents of output.txt, and if it's not your current code, we'll need that too. I can try to debug it with my eyes and a sleepy mind if you'd like.

  13. #13
    Registered User
    Join Date
    Aug 2011
    Posts
    385
    Quote Originally Posted by andrew89 View Post

    Lastly, I can't help you until you find some alternative to using conio.h as my operating system--Ubuntu Lucid--won't even compile using that header, much less be able to make any kind of use out of it.
    remove conio.h. Its not needed in this code.

    Quote Originally Posted by andrew89 View Post
    If that's you're current code, can you post the contents of output.txt, and if it's not your current code, we'll need that too. I can try to debug it with my eyes and a sleepy mind if you'd like.
    Run encoder.exe and provide the following four characters as input:
    'a(ASCII 97)' , 'space(ASCII 32)' , 'a(ASCII 97)' , 'space(ASCII 32)', 'ctrl z', [Hit enter], 'ctrl z' [Hit enter].

    Output.txt gets generated (with encoded characters).
    Now run decoder.exe. It is supposed to modify output.txt with original characters (a, space, a ,space) but it gets stuck after the first iteration of while loop. Seems as if the contents of t are not changing after first iteration.

    Help me man... I'll give you anything you ask for.
    Last edited by juice; 12-26-2011 at 04:17 AM.

  14. #14
    Registered User andrew89's Avatar
    Join Date
    Dec 2011
    Location
    Indiana, United States
    Posts
    80
    Can you attach your output.txt? Mine is just blank.

  15. #15
    Registered User
    Join Date
    Aug 2011
    Posts
    385
    Encoded output.txt
    Attached Files Attached Files

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