.txt file into an array

I have a program to write that takes a file and encodes it by putting the ascii value and adding 5 to it and then decodes it by subtracting 5 to the ascii value and showing the character.

I have the file i want encode and decode but im am not sure on how to get every character of the file and convert it.
I was thinking get the contents of the file and put each character into an array and making a loop to run through each character and convert into the ascii but im not sure of the syntax on how to do this.
Any help on this program would be appreciated.
Thank you!
Last edited on
Use the get() member of your fstream object to extract individual characters from the file. Then these chars can be put into a string or array (preferably string) and then encrypted with a simple -=5; They will already be in ascii when you extract them.
Last edited on
Alright here's my code so far to encode the file and it is not showing the ASCII value just some random symbols i was wondering what i am doing wrong or suggestions. ModShop you said to put the chars into a string but how do you do that? If anybody could check out my code and help me id appreciate it.



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
40
41
42
43
44
45
46
47
48
49
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
#include <cstring>
using namespace std;

//Function prototypes

int main()
{
	ifstream inputFile;
	const int SIZE = 101;
	char fileName[SIZE];
	char charHold;
	

	//Opening File for output
	ofstream outputFile("encoded.txt");//opening output File
	 

	cout << "Enter File Name: " ; //requesting file name or input
	cin >> fileName;

	inputFile.open(fileName);//opening the file for input

	//testing if file is valid
	if(!inputFile)
	{
		cout << "Error: File " << fileName << " " << "does not exist or cannot be opened.";
		return 0;
	}

	inputFile.get(charHold);
	while(!inputFile.eof())
	{
		outputFile.put(static_cast<int>(charHold) + 5);
		inputFile.get(charHold);
		outputFile << charHold;
	}

	inputFile.close();
	outputFile.close();
	cout << "The files have been encoded" ;

	system("pause");


}
Should i not use static_cast to convert it to an integer?
You don't even need to convert it to an int. Just extract the char and then perform the encoding on it directly.
1
2
3
4
5
char letter;
letter = infile.get();
letter += 5;

outfile << letter;
I had converted it and like i said i got weird symbols..
THis is what the code gave me in the encoded file..

jxyynsl%sfrj%nx%[nhytw3Mn&

is that an ascii value? are they not suppose to be integers?
That is the extended ascii value for the encrypted chars. chars are really just integers that represent characters. You could insert a casted version of the chars to put ints in the file if you really wanted to i suppose.
 
file << (int)somechar;
closed account (zwA4jE8b)
http://www.cdrummond.qc.ca/cegep/informat/Professeurs/Alain/images/ASCII1.GIF
Topic archived. No new replies allowed.