interesting problem in turbo C++, involving fopen

Discussion in 'C Programming' started by git_cs, Apr 30, 2004.

  1. git_cs

    git_cs Guest

    Hey, guys and gals
    Somedays ago, I had asked for the DES algorithm in C language.
    Although I have written the algorthim in C myself, I am facing a
    peculiar problem, which I hope some of u guys and gals solve.
    I use Turbo C++ version 3.0 and WINXP as the operating system.
    Pls observe the following program.

    1 #include<stdio.h>
    2 #include<conio.h>

    3 main()
    4 {
    5 int i,j;
    6 int ak;
    7 char ch;
    8 FILE *fp,*fs;
    9 clrscr();
    10 i=0;
    11 fs=fopen("ak4.txt","w");
    12 for(i=0;i<256;i++)
    13 {
    14 fputc(i,fs);
    15 }
    16 fclose(fs);
    17 i=1;
    18 fs=fopen("ak4.txt","r");
    19 fp=fopen("ak5.txt","w");
    20 i=0;
    21 while(i!=EOF)
    22 {
    23 i=fgetc(fs);
    24 printf("%d\n",i);
    25 fprintf(fp,"%c",i);
    26 fprintf(fp,"%d ",i);
    24 }
    25 }



    I run the above program in Turbo C++, and winxp as operating system.

    Then the file "ak4.txt", is created which will contain all the ascii
    characters from 0-255.
    But, from line 18, when I open the file "ak4.txt", and try to copy its
    contents to another file
    "ak5.txt", you will find a very interesting output.

    First, that character whose ascii value is "13", has been ignored.
    You can verify it on the screen output by pressing "Alt+F5", or in the
    new file "ak5.txt".

    Second, at character with ascii value "26", the while loop at line 21,
    exits . In the file "ak5.txt", only 0-25 ascii characters(except 13)
    are copied.


    So, if insert the following line

    if(i==26)
    continue;

    before line 14, then all the characters other than 26,13 will be
    copied in file "ak5.txt".




    Whew!!!, I hope I have been able to explain u.

    My question: I am doing a project on DES algorithm. When I encrypt a
    file, any ascii value from 0-255, might be the output. So, when I
    want to decrypt it, those ascii values 13,26 do appear sometimes, and
    cause the decryption to go haywire(13) or to exit the program
    itself(26).

    So, my friends is there a way out of this mess?

    Have any of you faced the same problem. If yes? how did you solve it?

    Will changing the compiler solve the problem?

    Is there gcc compiler for windows too?

    Please suggest some ideas. Because I intend to make a project which
    runs on windows.



    Note: The same DES program runs just fine on Linux.
    git_cs, Apr 30, 2004
    #1
    1. Advertising

  2. git_cs

    Karthik Guest

    git_cs wrote:

    > Hey, guys and gals
    > Somedays ago, I had asked for the DES algorithm in C language.
    > Although I have written the algorthim in C myself, I am facing a
    > peculiar problem, which I hope some of u guys and gals solve.
    > I use Turbo C++ version 3.0 and WINXP as the operating system.
    > Pls observe the following program.
    >
    > 1 #include<stdio.h>
    > 2 #include<conio.h>
    >
    > 3 main()
    > 4 {
    > 5 int i,j;
    > 6 int ak;
    > 7 char ch;
    > 8 FILE *fp,*fs;
    > 9 clrscr();
    > 10 i=0;
    > 11 fs=fopen("ak4.txt","w");
    > 12 for(i=0;i<256;i++)
    > 13 {
    > 14 fputc(i,fs);
    > 15 }
    > 16 fclose(fs);
    > 17 i=1;
    > 18 fs=fopen("ak4.txt","r");
    > 19 fp=fopen("ak5.txt","w");
    > 20 i=0;
    > 21 while(i!=EOF)
    > 22 {
    > 23 i=fgetc(fs);
    > 24 printf("%d\n",i);
    > 25 fprintf(fp,"%c",i);
    > 26 fprintf(fp,"%d ",i);
    > 24 }
    > 25 }

    Would have been really nice if you had ignored those line numbers for
    someone to test and compile it.

    >
    >
    >
    > I run the above program in Turbo C++, and winxp as operating system.
    >
    > Then the file "ak4.txt", is created which will contain all the ascii
    > characters from 0-255.
    > But, from line 18, when I open the file "ak4.txt", and try to copy its
    > contents to another file
    > "ak5.txt", you will find a very interesting output.
    >
    > First, that character whose ascii value is "13", has been ignored.


    Uh !! That is actually a CR (carriage return) so it would have taken
    as the end-of-line. You got to take that into account.

    > You can verify it on the screen output by pressing "Alt+F5", or in the
    > new file "ak5.txt".
    >
    > Second, at character with ascii value "26", the while loop at line 21,
    > exits . In the file "ak5.txt", only 0-25 ascii characters(except 13)
    > are copied.
    >
    >
    > So, if insert the following line
    >
    > if(i==26)
    > continue;
    >
    > before line 14, then all the characters other than 26,13 will be
    > copied in file "ak5.txt".
    >
    >
    >
    >
    > Whew!!!, I hope I have been able to explain u.
    >
    > My question: I am doing a project on DES algorithm. When I encrypt a
    > file, any ascii value from 0-255, might be the output. So, when I
    > want to decrypt it, those ascii values 13,26 do appear sometimes, and
    > cause the decryption to go haywire(13) or to exit the program
    > itself(26).
    >
    > So, my friends is there a way out of this mess?
    >
    > Have any of you faced the same problem. If yes? how did you solve it?
    >
    > Will changing the compiler solve the problem?

    Does not matter.

    >
    > Is there gcc compiler for windows too?

    Of course ...

    >


    --
    Karthik
    Humans please 'removeme_' for my real email.
    Karthik, Apr 30, 2004
    #2
    1. Advertising

  3. (git_cs) wrote:
    >Hey, guys and gals
    > Somedays ago, I had asked for the DES algorithm in C language.
    >Although I have written the algorthim in C myself, I am facing a
    >peculiar problem, which I hope some of u guys and gals solve.
    > I use Turbo C++ version 3.0 and WINXP as the operating system.
    > Pls observe the following program.


    OK, some quick comments:

    >1 #include<stdio.h>
    >2 #include<conio.h>

    Non-standard header, drop it.

    >3 main()

    int main( void )

    >4 {
    >5 int i,j;
    >6 int ak;
    >7 char ch;
    >8 FILE *fp,*fs;
    >9 clrscr();

    Non-standard library function, drop it.

    >10 i=0;
    >11 fs=fopen("ak4.txt","w");

    Since you're not going to write a text file, open it in
    "wb" (binary) mode.

    >12 for(i=0;i<256;i++)
    >13 {
    >14 fputc(i,fs);
    >15 }
    >16 fclose(fs);
    >17 i=1;
    >18 fs=fopen("ak4.txt","r");

    Use "rb" here.

    >19 fp=fopen("ak5.txt","w");

    Use "wb" here.

    >20 i=0;
    >21 while(i!=EOF)
    >22 {
    >23 i=fgetc(fs);
    >24 printf("%d\n",i);
    >25 fprintf(fp,"%c",i);
    >26 fprintf(fp,"%d ",i);
    >24 }

    fclose(fp);
    fclose(fs);
    return 0;

    >25 }


    BTW: Your indentation is a mess, you didn't provide even minimal
    error checking, and posting code with line numbers is usually not
    a good idea.

    HTH
    Regards
    --
    Irrwahn Grausewitz ()
    welcome to clc: http://www.ungerhu.com/jxh/clc.welcome.txt
    clc faq-list : http://www.faqs.org/faqs/C-faq/faq/
    clc OT guide : http://benpfaff.org/writings/clc/off-topic.html
    Irrwahn Grausewitz, Apr 30, 2004
    #3
  4. git_cs wrote:

    > Hey, guys and gals
    > Somedays ago, I had asked for the DES algorithm in C language.
    > Although I have written the algorthim in C myself, I am facing a
    > peculiar problem, which I hope some of u guys and gals solve.
    > I use Turbo C++ version 3.0 and WINXP as the operating system.
    > Pls observe the following program.
    >
    > 1 #include<stdio.h>
    > 2 #include<conio.h>
    >
    > 3 main()
    > 4 {
    > 5 int i,j;
    > 6 int ak;
    > 7 char ch;
    > 8 FILE *fp,*fs;
    > 9 clrscr();
    > 10 i=0;
    > 11 fs=fopen("ak4.txt","w");
    > 12 for(i=0;i<256;i++)
    > 13 {
    > 14 fputc(i,fs);
    > 15 }
    > 16 fclose(fs);
    > 17 i=1;
    > 18 fs=fopen("ak4.txt","r");
    > 19 fp=fopen("ak5.txt","w");
    > 20 i=0;
    > 21 while(i!=EOF)
    > 22 {
    > 23 i=fgetc(fs);
    > 24 printf("%d\n",i);
    > 25 fprintf(fp,"%c",i);
    > 26 fprintf(fp,"%d ",i);
    > 24 }
    > 25 }
    >
    >
    >
    > I run the above program in Turbo C++, and winxp as operating system.
    >
    > Then the file "ak4.txt", is created which will contain all the ascii
    > characters from 0-255.
    > But, from line 18, when I open the file "ak4.txt", and try to copy its
    > contents to another file
    > "ak5.txt", you will find a very interesting output.
    >
    > First, that character whose ascii value is "13", has been ignored.
    > You can verify it on the screen output by pressing "Alt+F5", or in the
    > new file "ak5.txt".
    >
    > Second, at character with ascii value "26", the while loop at line 21,
    > exits . In the file "ak5.txt", only 0-25 ascii characters(except 13)
    > are copied.
    >
    >
    > So, if insert the following line
    >
    > if(i==26)
    > continue;
    >
    > before line 14, then all the characters other than 26,13 will be
    > copied in file "ak5.txt".




    I applied the following changes to your code. Some are to make it C99
    compliant; some are because I'm allergic to unused variables. The key
    change is in the open statement. Try running it to see what happens.
    BTW, the extra ^M 13 in ak5.txt is because I did not apply the same
    change to the fopen to write ak4.txt in the first place. To make sure
    that the results are predictable across platforms using the same
    encoding (ASCII), I ought to have sent a '\n' to each of the output
    files before closing them. The resulting code follows the list of
    changes. Try running it.

    2d1
    < #include<conio.h>
    4c3
    < main()
    ---
    > int main()

    6,8c5
    < int i, j;
    < int ak;
    < char ch;
    ---
    > int i;

    10d6
    < clrscr();
    18c14
    < fs = fopen("ak4.txt", "r");
    ---
    > fs = fopen("ak4.txt", "rb");



    [resulting code]
    #include<stdio.h>

    int main()
    {
    int i;
    FILE *fp, *fs;
    i = 0;
    fs = fopen("ak4.txt", "w");
    for (i = 0; i < 256; i++) {
    fputc(i, fs);
    }
    fclose(fs);
    i = 1;
    fs = fopen("ak4.txt", "rb");
    fp = fopen("ak5.txt", "w");
    i = 0;
    while (i != EOF) {
    i = fgetc(fs);
    printf("%d\n", i);
    fprintf(fp, "%c", i);
    fprintf(fp, "%d ", i);
    }
    }
    Martin Ambuhl, Apr 30, 2004
    #4
  5. (git_cs) writes:

    > Hey, guys and gals
    > Somedays ago, I had asked for the DES algorithm in C language.
    > Although I have written the algorthim in C myself, I am facing a
    > peculiar problem, which I hope some of u guys and gals solve.
    > I use Turbo C++ version 3.0 and WINXP as the operating system.


    Compiler and OS version are not relevant for questions which are
    on-topic here.

    > Pls observe the following program.


    I do /observe/ it, but it would have been better if I could also easily
    /test/ it: please leave out the line numbers in the future.

    (I have taken the liberty of replacing each tab with a sequence of four
    spaces in the following quoted code.)

    > 1 #include<stdio.h>
    > 2 #include<conio.h>


    No such header in C.

    > 3 main()


    Obsolete definition: I prefer `int main (void)'.

    > 4 {
    > 5 int i,j;
    > 6 int ak;
    > 7 char ch;
    > 8 FILE *fp,*fs;
    > 9 clrscr();


    No such function in C.

    > 10 i=0;
    > 11 fs=fopen("ak4.txt","w");
    > 12 for(i=0;i<256;i++)
    > 13 {
    > 14 fputc(i,fs);
    > 15 }
    > 16 fclose(fs);
    > 17 i=1;


    What's the purpose of setting `i' to `1' here?

    > 18 fs=fopen("ak4.txt","r");
    > 19 fp=fopen("ak5.txt","w");
    > 20 i=0;
    > 21 while(i!=EOF)
    > 22 {
    > 23 i=fgetc(fs);
    > 24 printf("%d\n",i);
    > 25 fprintf(fp,"%c",i);
    > 26 fprintf(fp,"%d ",i);
    > 24 }


    `main' returns an `int': Insert `return 0;' here.

    > 25 }


    You don't do any error checking. All file functions can indicate that
    an error occured, which should be checked.

    > First, that character whose ascii value is "13", has been ignored.
    > [...] Second, at character with ascii value "26", the while loop at
    > line 21, exits . In the file "ak5.txt", only 0-25 ascii
    > characters(except 13) are copied.


    The problem is that you open binary files in text mode. Use
    `fopen ("...", "rb")' and `fopen ("...", "wb")' instead.

    > You can verify it on the screen output by pressing "Alt+F5",


    System-specific assumption: Why do you believe to know what Alt+F5 does
    on my system? ;)

    > Is there gcc compiler for windows too?


    Yes, but that won't help you. The only correct way to react to
    incorrect code is to correct it.

    Martin


    --
    ,--. Martin Dickopp, Dresden, Germany ,= ,-_-. =.
    / ,- ) http://www.zero-based.org/ ((_/)o o(\_))
    \ `-' `-'(. .)`-'
    `-. Debian, a variant of the GNU operating system. \_/
    Martin Dickopp, Apr 30, 2004
    #5
  6. git_cs

    CBFalconer Guest

    git_cs wrote:
    >
    > Somedays ago, I had asked for the DES algorithm in C language.
    > Although I have written the algorthim in C myself, I am facing a
    > peculiar problem, which I hope some of u guys and gals solve.
    > I use Turbo C++ version 3.0 and WINXP as the operating system.
    > Pls observe the following program.
    >
    > 1 #include<stdio.h>
    > 2 #include<conio.h>


    Unknown include file. Not standard.
    >
    > 3 main()


    better declared as "int main(void)"

    > 4 {
    > 5 int i,j;
    > 6 int ak;
    > 7 char ch;
    > 8 FILE *fp,*fs;
    > 9 clrscr();


    Undefined function

    > 10 i=0;
    > 11 fs=fopen("ak4.txt","w");


    Failure to check for success of fopen.

    > 12 for(i=0;i<256;i++)
    > 13 {
    > 14 fputc(i,fs);
    > 15 }
    > 16 fclose(fs);


    Failure to check for success of fclose

    > 17 i=1;
    > 18 fs=fopen("ak4.txt","r");


    Failure to check for success of fopen.

    > 19 fp=fopen("ak5.txt","w");


    Failure to check for success of fopen.

    > 20 i=0;
    > 21 while(i!=EOF)
    > 22 {
    > 23 i=fgetc(fs);
    > 24 printf("%d\n",i);
    > 25 fprintf(fp,"%c",i);
    > 26 fprintf(fp,"%d ",i);
    > 24 }
    > 25 }

    .... snip ....
    >
    > First, that character whose ascii value is "13", has been ignored.
    > You can verify it on the screen output by pressing "Alt+F5", or in
    > the new file "ak5.txt".
    >
    > Second, at character with ascii value "26", the while loop at line
    > 21, exits . In the file "ak5.txt", only 0-25 ascii characters
    > (except 13) are copied.
    >

    .... snip ...
    >
    > So, my friends is there a way out of this mess?


    You are opening the files as text files. Open as binary files,
    after fixing the problems I outlined above.

    --
    Chuck F () ()
    Available for consulting/temporary embedded and systems.
    <http://cbfalconer.home.att.net> USE worldnet address!
    CBFalconer, Apr 30, 2004
    #6
  7. git_cs

    osmium Guest

    git_cs writes:


    > Somedays ago, I had asked for the DES algorithm in C language.
    > Although I have written the algorthim in C myself, I am facing a
    > peculiar problem, which I hope some of u guys and gals solve.
    > I use Turbo C++ version 3.0 and WINXP as the operating system.
    > Pls observe the following program.

    <snip>
    > Note: The same DES program runs just fine on Linux.


    Linux does not make any distinctions between "text" and "binary" files; but
    Windows does. There is hidden code in Windows which converts the CR, LF
    pair to a single character in Windows, and that code is biting you. The
    solution is to open the files is binary mode for *all* environments. This
    will be harmless in Unix/Linux (it will be ignored) but is the main change
    you need to make it work in Windows.




    >
    > 1 #include<stdio.h>
    > 2 #include<conio.h>
    >
    > 3 main()
    > 4 {
    > 5 int i,j;
    > 6 int ak;
    > 7 char ch;
    > 8 FILE *fp,*fs;
    > 9 clrscr();
    > 10 i=0;
    > 11 fs=fopen("ak4.txt","w");
    > 12 for(i=0;i<256;i++)
    > 13 {
    > 14 fputc(i,fs);
    > 15 }
    > 16 fclose(fs);
    > 17 i=1;
    > 18 fs=fopen("ak4.txt","r");
    > 19 fp=fopen("ak5.txt","w");
    > 20 i=0;
    > 21 while(i!=EOF)
    > 22 {
    > 23 i=fgetc(fs);
    > 24 printf("%d\n",i);
    > 25 fprintf(fp,"%c",i);
    > 26 fprintf(fp,"%d ",i);
    > 24 }
    > 25 }
    >
    >
    >
    > I run the above program in Turbo C++, and winxp as operating system.
    >
    > Then the file "ak4.txt", is created which will contain all the ascii
    > characters from 0-255.
    > But, from line 18, when I open the file "ak4.txt", and try to copy its
    > contents to another file
    > "ak5.txt", you will find a very interesting output.
    >
    > First, that character whose ascii value is "13", has been ignored.
    > You can verify it on the screen output by pressing "Alt+F5", or in the
    > new file "ak5.txt".
    >
    > Second, at character with ascii value "26", the while loop at line 21,
    > exits . In the file "ak5.txt", only 0-25 ascii characters(except 13)
    > are copied.
    >
    >
    > So, if insert the following line
    >
    > if(i==26)
    > continue;
    >
    > before line 14, then all the characters other than 26,13 will be
    > copied in file "ak5.txt".
    >
    >
    >
    >
    > Whew!!!, I hope I have been able to explain u.
    >
    > My question: I am doing a project on DES algorithm. When I encrypt a
    > file, any ascii value from 0-255, might be the output. So, when I
    > want to decrypt it, those ascii values 13,26 do appear sometimes, and
    > cause the decryption to go haywire(13) or to exit the program
    > itself(26).
    >
    > So, my friends is there a way out of this mess?
    >
    > Have any of you faced the same problem. If yes? how did you solve it?
    >
    > Will changing the compiler solve the problem?
    >
    > Is there gcc compiler for windows too?
    >
    > Please suggest some ideas. Because I intend to make a project which
    > runs on windows.
    >
    >
    >
    > Note: The same DES program runs just fine on Linux.
    osmium, Apr 30, 2004
    #7
  8. git_cs

    Joe Wright Guest

    git_cs wrote:
    > Hey, guys and gals
    > Somedays ago, I had asked for the DES algorithm in C language.
    > Although I have written the algorthim in C myself, I am facing a
    > peculiar problem, which I hope some of u guys and gals solve.
    > I use Turbo C++ version 3.0 and WINXP as the operating system.
    > Pls observe the following program.
    >
    > 1 #include<stdio.h>
    > 2 #include<conio.h>
    >
    > 3 main()
    > 4 {
    > 5 int i,j;
    > 6 int ak;
    > 7 char ch;
    > 8 FILE *fp,*fs;
    > 9 clrscr();
    > 10 i=0;
    > 11 fs=fopen("ak4.txt","w");
    > 12 for(i=0;i<256;i++)
    > 13 {
    > 14 fputc(i,fs);
    > 15 }
    > 16 fclose(fs);
    > 17 i=1;
    > 18 fs=fopen("ak4.txt","r");
    > 19 fp=fopen("ak5.txt","w");
    > 20 i=0;
    > 21 while(i!=EOF)
    > 22 {
    > 23 i=fgetc(fs);
    > 24 printf("%d\n",i);
    > 25 fprintf(fp,"%c",i);
    > 26 fprintf(fp,"%d ",i);
    > 24 }
    > 25 }
    >
    >
    >
    > I run the above program in Turbo C++, and winxp as operating system.
    >
    > Then the file "ak4.txt", is created which will contain all the ascii
    > characters from 0-255.
    > But, from line 18, when I open the file "ak4.txt", and try to copy its
    > contents to another file
    > "ak5.txt", you will find a very interesting output.
    >
    > First, that character whose ascii value is "13", has been ignored.
    > You can verify it on the screen output by pressing "Alt+F5", or in the
    > new file "ak5.txt".
    >
    > Second, at character with ascii value "26", the while loop at line 21,
    > exits . In the file "ak5.txt", only 0-25 ascii characters(except 13)
    > are copied.
    >
    >
    > So, if insert the following line
    >
    > if(i==26)
    > continue;
    >
    > before line 14, then all the characters other than 26,13 will be
    > copied in file "ak5.txt".
    >
    >
    >
    >
    > Whew!!!, I hope I have been able to explain u.
    >
    > My question: I am doing a project on DES algorithm. When I encrypt a
    > file, any ascii value from 0-255, might be the output. So, when I
    > want to decrypt it, those ascii values 13,26 do appear sometimes, and
    > cause the decryption to go haywire(13) or to exit the program
    > itself(26).
    >
    > So, my friends is there a way out of this mess?
    >
    > Have any of you faced the same problem. If yes? how did you solve it?
    >
    > Will changing the compiler solve the problem?
    >
    > Is there gcc compiler for windows too?
    >
    > Please suggest some ideas. Because I intend to make a project which
    > runs on windows.
    >
    >
    >
    > Note: The same DES program runs just fine on Linux.


    You've gotten lots of good advice. Think about this..

    #include <stdio.h>
    #include <ctype.h>

    int main(void)
    {
    int i;
    FILE *fp, *fs;
    fs = fopen("ak4.bin", "wb");
    for (i = 0; i < 256; i++) {
    fputc(i, fs);
    }
    fclose(fs);
    fs = fopen("ak4.bin", "rb");
    fp = fopen("ak5.txt", "w");
    while ((i = fgetc(fs)) != EOF) {
    printf("%d\n", i);
    fprintf(fp, "%c", isprint(i) ? i : '.');
    fprintf(fp, " %d\n", i);
    }
    return 0;
    }


    --
    Joe Wright mailto:
    "Everything should be made as simple as possible, but not simpler."
    --- Albert Einstein ---
    Joe Wright, Apr 30, 2004
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Maurice Hulsman
    Replies:
    1
    Views:
    1,833
    Guus Bosman
    Jul 25, 2004
  2. Replies:
    6
    Views:
    396
    Ian T
    Dec 10, 2004
  3. Rhino
    Replies:
    11
    Views:
    644
    Chris Uppal
    Feb 6, 2006
  4. Nonee
    Replies:
    2
    Views:
    2,651
    Neredbojias
    Oct 25, 2005
  5. Michel Rouzic
    Replies:
    4
    Views:
    1,832
    Michel Rouzic
    Apr 28, 2008
Loading...

Share This Page