I am to open a text file, copy it, increment its ASCI characters by ten and then write it into a created text file. This is the code that I have so far.(visual c++ 6.0). The problem is that I cannot increment the characters. It gives me and overwrites my original file with ÌÌ characters.

int main(){

	const int size=30;
	char filename[size];

	cout<<"Enter the file name that you will like to encrypt: \n";
	cin.getline(filename, 30);

	fstream dataFile;

	dataFile.open(filename,ios::out|ios::binary);
	if (!dataFile){
		cout<<"could not open.. exitting\n";
		exit (1);
	}

	char list[size];

	dataFile.write(reinterpret_cast<char *>(&list),size);

	if(dataFile.fail()){
		cout<<"failed to write..exitting";
		exit (1);
	}
	dataFile.close();

	char filename2[size];

	cout<<"Enter the file name that you will like to create: \n";
	cin.getline(filename2, 30);

	for (int i=0; i<size; i++){
		list[i]+= 10;	
	}

	
	ofstream outFile(filename2);

	outFile.write(reinterpret_cast<char *>(&list), size);

	outFile.close();

	return 0;
}

Recommended Answers

All 7 Replies

So what's the problem, other than not using CODE tags as requested in the Rules. Maybe you need to read Read Me: Read This Before Posting too.

ok. Im sorry of not using code tags. The problem is that I cannot increment the characters. It gives me and overwrites my original file with ÌÌ characters.

char list;
dataFile.write(reinterpret_cast<char *>(&list),size);

You probably want to put something into list after you declare it and before you write it to file. If you must use the binary mode of read/write from file, then use the read() method to read from the file into the string. Otherwise, I'd encourage you to look into the getline() method or the >> operator overloaded for the istream class, depending on whether you have whitespace in the file that you want read into list or not.

oh thanks Lerner. I'll try it out

Follow your code:

int main(){

	const int size=30;
	char filename[size];

	cout<<"Enter the file name that you will like to encrypt: \n";
	cin.getline(filename, 30);

	fstream dataFile;

	// open your file for OUTPUT
	dataFile.open(filename,ios::out|ios::binary);         
	if (!dataFile){
		cout<<"could not open.. exitting\n";
		exit (1);
	}

	// create a char buffer.  It just contains junk because you did 
	// not initialize the contents
	char list[size];                           

	// Overwrite your file with the junk
	dataFile.write(reinterpret_cast<char *>(&list),size);    

	if(dataFile.fail()){
		cout<<"failed to write..exitting";
		exit (1);
	}
	dataFile.close();

	char filename2[size];

	cout<<"Enter the file name that you will like to create: \n";
	cin.getline(filename2, 30);

	// encrypt the junk
	for (int i=0; i<size; i++){
		list[i]+= 10;	              
	}

	
	ofstream outFile(filename2);

	// output the junk
	outFile.write(reinterpret_cast<char *>(&list), size);  

	outFile.close();

	return 0;
}

If you comment your code, you would have seen the problem (I hope). The only thing is you must comment based on what the code itself tells you, not what you think it's doing.

ok. i got it to copy the encryption into the new file, I overloaded it. Now i just need to write ONLY the encrypted characters. The problem is that it writes junk after the encryption.

If you want help trying to figure it out you'll need to post the updated version of your code. In addition sample input with observed output and expected output would helpful.

If you keep working on it and figure it out yourself, good for you.

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.