can you help me complete this program...
it is an encryption program..
it needs another text file(1), that will convert the the contents of text file(#2) and then save it to another text file(#3)..it needs 3 text file..how can i do that??

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

void main(void)
{
	FILE *inputf;
	FILE *encrypt;

		char c;
		int ch;
		char encryptfilename[20];
		char output[20];
		cout<<"Input name of textfile to encrypt: ";
		cin>>encryptfilename;
if((inputf=fopen(encryptfilename,"r"))==NULL)
{
	cout<<"Error:filename cannot be opened \n";
	exit(0);
}
	cout<<"Input name of encrypted textfile: ";
	cin>>output;
	if((encrypt=fopen(output,"w"))==NULL)
	{
		cout<<"Error \n";
		exit(0);
	}
	c=fgetc(inputf);
	cout<<"\nThe file is encrypted";
	while(c!=EOF)
	{
		ch=0;
		ch=ch+c;
		ch=ch/2;
		c=c+ch;
		fputc(c,encrypt);
		c=fgetc(inputf);

	}
	fclose(inputf);
	fclose(encrypt);
	
}

Recommended Answers

All 12 Replies

1.You are using C codes rather than C++.(The only thing in which you are using C++ is Console IO)
2.Non-Standard Headers.
3.Never use void main
4.Use fstream objects rather than fopen().
5.

ch=0;
		ch=ch+c;
		ch=ch/2;
		c=c+ch;

can be better stated as

ch=c;
		ch=ch/2;
		c+=ch;

6.Please specify, what is the exact problem you are getting.(like, is the code not running or what?)
7.>>that will convert the the contents
What does convert imply?

this codes are running, ,but we are required to use 3 text files..the text file(#1) that will encrypt the contents of text file(#2) and then save it to text file(#3)..i don't know how to do the text file(#1) that will do the conversion...help

>the text file(#1) that will encrypt the contents of text file(#2)
How is a FILE suppose to do the conversion?

the contents of text file(#1) is like this:
a =z
b=y
c=x
d=w
e=v
f=u
g=t
h=s
i=r
j=q
k=p
l=o
m=n
n=m
o=l


i saw this example in my classmate, they didn't teach me.
i don't know how to use this in this program..

So the first file defines the 'encryption'
The second file contains the 'message' to be encrypted.
The third file you are supposed to generate as the encrypted message?

If so, you will need to open and parse the first file, using the data you find to develop the translation. I would interpret your sample file one to mean that when you see the letter 'a' in the message, you should put a 'z' in the encrypted message. But don't hard-code this specific mapping, you need to read the first file and interpret each of the lines.

Then you open the second file for input, the third file for output and start encrypting the input message, writing the output to the third file.

Show us what you understand (to show that you're trying, we won't help if you aren't putting in an effort) and identify specific issues like: "I'm expecting the program to do ___, but it is doing ___" or "The program won't compile, I'm seeing error '--insert exact compiler error here--' on line ___." and include your source.

When posting c++ code, please use c++ code tags
[code=c++] // Your code here

[/code]

can you tell some clues of what am i going to write in the first text file??

You don't write anything in the first file. You read from it.
You also read from the second file character by character and look for its encrypted counterpart in the first file. Then you write the third file accordingly.

no, my question is, what will i read in the first file? what is the content of the first file?

you showed us the content of the file...

a =z
b=y
c=x
d=w
e=v
f=u
g=t
h=s
i=r
j=q
k=p
l=o
m=n
n=m
o=l

So I'm presuming that you're will need to read each line of the file, parse it to find what's on the left and the right of the '=' and setup the 'encryption' to use that mapping.

I'm not willing to go much farther without seeing more of your own effort.

'a' || 'A' == 'z';
'b' || 'B' == 'y';
'c' || 'C' == 'x';
'd' || 'D' == 'w';
'e' || 'E' == 'v';
'f' || 'F' == 'u';
'g' || 'G' == 't';
'h' || 'H' == 's';
'i' || 'I' == 'r';
'j' || 'J' == 'q';
'k' || 'K' == 'p';
'l' || 'L' == 'o';
'm' || 'M' == 'n';
'n' || 'N' == 'm';
'o' || 'O' == 'l';
'p' || 'P' == 'k';
'q' || 'Q' == 'j';
'r' || 'R' == 'i';
's' || 'S' == 'h';
't' || 'T' == 'g';
'u' || 'U' == 'f';
'v' || 'V' == 'e';
'w' || 'W' == 'd';
'x' || 'X' == 'c';
'y' || 'Y' == 'b';
'z' || 'Z' == 'a';

i put this on the first text file

it is running but the 3rd text file is empty
and it has some errors in the end of running time
can you check it again.thanks!

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

void main()
{
	FILE *convert;
	FILE *inputf;
	FILE *encrypt;

	char c;
	int ch;
	char encryptfilename[20];
	char output[20];

	cout<<"Input name of textfile to encrypt: ";
	cin>>encryptfilename;

		if((inputf=fopen(encryptfilename,"r"))==NULL)
		{
			cout<<"Error:filename cannot be opened \n";
			exit(0);
		}

		while((inputf=fopen("convert.txt","r"))==NULL)
		{
			cout<<"Error:filename cannot be opened \n";
			exit(0);
		}

	cout<<"Input name of encrypted textfile: ";
	cin>>output;
	if((encrypt=fopen(output,"w"))==NULL)
	{
		cout<<"Error \n";
		exit(0);
	}
	while(c!=EOF)
	{
		ch=ch+c;
		ch=ch/2;
		c+=ch;

		fputc(c,encrypt);
		c=fgetc(inputf);

	c=fgetc(convert);
	cout<<"\nThe file is encrypted";

	}
	fclose(inputf);
	fclose(encrypt);
	fclose(convert);
	
}

Line 26 should be:

if ((convert=fopen("convert.txt","r"))==NULL)

I have a couple of points and several questions about the encryption loop:

while(c!=EOF)
	{
		ch=ch+c;
		ch=ch/2;
		c+=ch;

		fputc(c,encrypt);
		c=fgetc(inputf);

	c=fgetc(convert);
	cout<<"\nThe file is encrypted";

	}

The while c != EOF should work, but you should pre-initialize c to something.

The cout with the message should be after the loop (which should iterate for each character).

What does this code do?

ch = ch + c;
ch = ch / 2;
c += ch;

ch didn't have any specific value before we started and we never use it again. The first time through the loop, c doesn't have a specific value either.

If you initialized c with a c = fgetc(inputf); before the while loop, it might make more sense, but it still ignores the contents from file 1.

I think your loop was terminating early, because the c = fgetc(convert); was failing, because you never got convert open.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.