Exporting a cipher from a text file.

I seem to be having a problem with this program I am coding. I understand what to do in context, I just am unaware of how to output it in code.

What I am trying to do is decrypt a cipher from one text file, and output the possible solutions into another text file.

The cipher I am using is [qw)tg"fqkpi"itgcv#""Iq"vq<"jvvru<11dkv0n{13dirO:n

And I have created a "rough draft" of my code.

#include<iostream>
#include<fstream>

using namespace std;

int main(){

char message [49];

ofstream file, file2;
file.open ("Jumbled Cipher.txt");
file2.open ("Unjumbled.txt");

if (file2.is_open())
{
file2 << file, message[49];
file2.close();
}
else cout<<"Error Outputting File!";
return 0;
}

Thank You!
Last edited on
You are insane. :-)
Hello MrPolynomial,


PLEASE ALWAYS USE CODE TAGS (the <> formatting button), to the right of this box, when posting code.

It makes it easier to read your code and also easier to respond to your post.

http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/

Hint: You can edit your post, highlight your code and press the <> formatting button.
You can use the preview button at the bottom to see how it looks.

I found the second link to be the most help.


In your code you are defining both "file" and "file2" as output streams. I would say that "file" should be an "ifstream" so that you can read the files. As an output stream you can not read from the file.

This may help you get started:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include<iostream>
#include <string>
#include<fstream>
#include <chrono>
#include <thread>

//using namespace std;  // <--- Best not to use.

int main()
{
	const std::string inFileName{ "Jumbled Cipher.txt" };
	const std::string outFileName{ "Unjumbled.txt" };

	std::string message;

        // <--- For the input file.
	std::ifstream inFile(inFileName);

	if (!inFile)
	{
		std::cout << "\n File " << inFileName << " did not open" << std::endl;
		std::this_thread::sleep_for(std::chrono::seconds(5));  // <--- Needs header files chrono" and "thread".
		return 1;  //exit(1);  // If not in "main".
	}

        // <--- For the ooutput file.
	std::ofstream outFile(outFileName, std::ios::trunc | std::ios::ate);

	if (!outFile)
	{
		std::cout << "\n File " << outFileName << " did not open" << std::endl;
		std::this_thread::sleep_for(std::chrono::seconds(5));  // <--- Needs header files chrono" and "thread".
		return 2;  //exit(1);  // If not in "main".
	}

	// Code to read, process and write to output.

	return 0;
}

Lines 22 and 32 are optional along with the header files "chrono" and "thread". I use this to keep the console window open in my VS IDE. You may not need it, but worth keeping for the future.

In the output file the part , std::ios::trunc | std::ios::ate may not be necessary. I use it while writing the program to clear the file and set up the file for "append". Later I change this to just , std::ios::app to open the file for "append".

In the if statement the "return ?;" is there to exit the program because there is no reason to continue with a file stream that you can not use. You should understand by now that zero is a normal exit from the program. Therefore any number greater than zero means that there is a problem. By using different numbers for the "return" or "exit" it can help track down where something went wrong.

Chances are that when reading the file "std::getline(...)" will be your best choice. Without seeing the input, which you should include in your post, I can only guess at what you need to do.

You may find this helpful http://www.cplusplus.com/reference/string/string/
and http://www.cplusplus.com/reference/string/string/getline/

First you have to get the files to open properly then work up code to read the file followed be processing what was just read then write to the output file.

Hope that helps,

Andy
Topic archived. No new replies allowed.