473,406 Members | 2,378 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

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.
Nov 14 '05 #1
7 3003
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.
Nov 14 '05 #2
gi****@rediffmail.com (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 (ir*******@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
Nov 14 '05 #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.

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);
}
}

Nov 14 '05 #4
gi****@rediffmail.com (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. \_/
Nov 14 '05 #5
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 (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!
Nov 14 '05 #6
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.

Nov 14 '05 #7
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:jo********@comcast.net
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Nov 14 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
by: Nick Coghlan | last post by:
Thought some folks here might find this one interesting. No great revelations, just a fairly sensible piece on writing readable code :) The whole article:...
6
by: Developwebsites | last post by:
I am taking advanced C++ at college and we use Borland Turbo C++ 4.5 compiler. How different is Turbo C++ from the standard C++? I know Borland used to call their versions of C++ and Pascal Turbo,...
2
by: SunRise | last post by:
Hi I am creating a C Program , to extract only-Printable-characters from a file ( any type of file) and display them. OS: Windows-XP Ple help me to fix the Errors & Warnings and explain...
6
by: Claude Yih | last post by:
Hi, everyone. I noticed an interesting thing about fread() this afternoon. Well, I can't see why so I post this message in the hope of getting some explanation. Please help me. I wrote the...
16
by: scott | last post by:
I am looking for a copy of Turbo C 1.5 from 1987 for some historical research I'm doing into computing from that time period.
27
by: Frederick Gotham | last post by:
I thought it might be interesting to share experiences of tracking down a subtle or mysterious bug. I myself haven't much experience with tracking down bugs, but there's one in particular which...
3
by: postrishi | last post by:
Hello Everybody , I am a new user. I am currently using Turbo C++ 3.0 editor in my engg.Can you tell me or post me a ebook on turbo c++ and NOT on c or C++.MInd it I want a book on TURBO C++ editor...
3
by: Untitled123 | last post by:
Hey guys!! I am Planning on Creating a Computer vs. User Chess game involving Artificial Intelligence... by just using TURBO C++ This is NOT impossible!! Please understand that my seniors...
4
by: shaieen | last post by:
#include<stdio.h> #include<conio.h> #include<string.h> main(){ typedef struct info { char fname(void),mname(void),famname(void); int age,cont; char adr; }...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.