interesting problem in turbo C++, involving fopen | | |
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. | | | | re: interesting problem in turbo C++, involving fopen
git_cs wrote:
[color=blue]
> 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 }[/color]
Would have been really nice if you had ignored those line numbers for
someone to test and compile it.
[color=blue]
>
>
>
> 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.[/color]
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.
[color=blue]
> 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?[/color]
Does not matter.
[color=blue]
>
> Is there gcc compiler for windows too?[/color]
Of course ...
[color=blue]
>[/color]
--
Karthik
Humans please 'removeme_' for my real email. | | | | re: interesting problem in turbo C++, involving fopen git_cs@rediffmail.com (git_cs) wrote:[color=blue]
>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.[/color]
OK, some quick comments:
[color=blue]
>1 #include<stdio.h>
>2 #include<conio.h>[/color]
Non-standard header, drop it.
[color=blue]
>3 main()[/color]
int main( void )
[color=blue]
>4 {
>5 int i,j;
>6 int ak;
>7 char ch;
>8 FILE *fp,*fs;
>9 clrscr();[/color]
Non-standard library function, drop it.
[color=blue]
>10 i=0;
>11 fs=fopen("ak4.txt","w");[/color]
Since you're not going to write a text file, open it in
"wb" (binary) mode.
[color=blue]
>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");[/color]
Use "rb" here.
[color=blue]
>19 fp=fopen("ak5.txt","w");[/color]
Use "wb" here.
[color=blue]
>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 }[/color]
fclose(fp);
fclose(fs);
return 0;
[color=blue]
>25 }[/color]
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 (irrwahn33@freenet.de)
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 | | | | re: interesting problem in turbo C++, involving fopen
git_cs wrote:
[color=blue]
> 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".[/color]
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()
---[color=blue]
> int main()[/color]
6,8c5
< int i, j;
< int ak;
< char ch;
---[color=blue]
> int i;[/color]
10d6
< clrscr();
18c14
< fs = fopen("ak4.txt", "r");
---[color=blue]
> fs = fopen("ak4.txt", "rb");[/color]
[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);
}
} | | | | re: interesting problem in turbo C++, involving fopen git_cs@rediffmail.com (git_cs) writes:
[color=blue]
> 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.[/color]
Compiler and OS version are not relevant for questions which are
on-topic here.
[color=blue]
> Pls observe the following program.[/color]
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.)
[color=blue]
> 1 #include<stdio.h>
> 2 #include<conio.h>[/color]
No such header in C.
[color=blue]
> 3 main()[/color]
Obsolete definition: I prefer `int main (void)'.
[color=blue]
> 4 {
> 5 int i,j;
> 6 int ak;
> 7 char ch;
> 8 FILE *fp,*fs;
> 9 clrscr();[/color]
No such function in C.
[color=blue]
> 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;[/color]
What's the purpose of setting `i' to `1' here?
[color=blue]
> 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 }[/color]
`main' returns an `int': Insert `return 0;' here.
[color=blue]
> 25 }[/color]
You don't do any error checking. All file functions can indicate that
an error occured, which should be checked.
[color=blue]
> 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.[/color]
The problem is that you open binary files in text mode. Use
`fopen ("...", "rb")' and `fopen ("...", "wb")' instead.
[color=blue]
> You can verify it on the screen output by pressing "Alt+F5",[/color]
System-specific assumption: Why do you believe to know what Alt+F5 does
on my system? ;)
[color=blue]
> Is there gcc compiler for windows too?[/color]
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. \_/ | | | | re: interesting problem in turbo C++, involving fopen
git_cs wrote:[color=blue]
>
> 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>[/color]
Unknown include file. Not standard.[color=blue]
>
> 3 main()[/color]
better declared as "int main(void)"
[color=blue]
> 4 {
> 5 int i,j;
> 6 int ak;
> 7 char ch;
> 8 FILE *fp,*fs;
> 9 clrscr();[/color]
Undefined function
[color=blue]
> 10 i=0;
> 11 fs=fopen("ak4.txt","w");[/color]
Failure to check for success of fopen.
[color=blue]
> 12 for(i=0;i<256;i++)
> 13 {
> 14 fputc(i,fs);
> 15 }
> 16 fclose(fs);[/color]
Failure to check for success of fclose
[color=blue]
> 17 i=1;
> 18 fs=fopen("ak4.txt","r");[/color]
Failure to check for success of fopen.
[color=blue]
> 19 fp=fopen("ak5.txt","w");[/color]
Failure to check for success of fopen.
[color=blue]
> 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 }[/color]
.... snip ....[color=blue]
>
> 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.
>[/color]
.... snip ...[color=blue]
>
> So, my friends is there a way out of this mess?[/color]
You are opening the files as text files. Open as binary files,
after fixing the problems I outlined above.
--
Chuck F (cbfalconer@yahoo.com) (cbfalconer@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address! | | | | re: interesting problem in turbo C++, involving fopen
git_cs writes:
[color=blue]
> 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.[/color]
<snip>[color=blue]
> Note: The same DES program runs just fine on Linux.[/color]
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.
[color=blue]
>
> 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.[/color] | | | | re: interesting problem in turbo C++, involving fopen
git_cs wrote:[color=blue]
> 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.[/color]
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:joewwright@comcast.net
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein --- |  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,501 network members.
|