Saving text file in an array

Hello, I'm trying to save a text file to an array, and then displaying the text file when the program is run.

There are 9988 characters in the file so I can't just list each one in the form of char encodedText[9988] = { t, j, p, ...etc}, so I guess I'm wondering how to get that information into the array. I've tried to do it by putting the name of the saved text file in the curly brackets but it doesn't seem to work as you can see from my code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <fstream>
using namespace std;
ifstream din;
ofstream dout;


int main()
{
	din.open("encoded.txt"); //opens the text file containing

char encodedText[9988] = { "encoded.txt" };  //declares an array of 9988 characters including spaces.
int e = 9987;

for (e = 0; e < 9988; e++)
{
	cout << encodedText[9988] << endl;
}
system("pause");
return 0;
}


Any help would be greatly appreciated.
Hi Michael,

It is not your question, but I'm not sure about the why you want to save your text file into an array. You should better read the input/output section of the tutorial http://www.cplusplus.com/doc/tutorial/files/ if you want to deal with text files for examples.

Now, for your question. When you do
 
char encodedText[9988] = { "encoded.txt" }; 

the encodedText array will be: [e,n,c,o,d,e,d,.,t,x,t,\0]
because you are actually asking to save the name into this array.

What you want to do is: reading the file character by character, and storing the character in the array. But one more time, you should better spend some time on the input/output section of the tutorial, and then what you want to do will be piece of cake ;)
Thanks very much for the link, i'll go through it and see if i can get what I'm after.

As for why I want to save the text file in an array:

I need to write a program to calculate the number and percentage of occurances of each letter using parallel arrays. One would contain the 26 alphabet letters and the other would contain the corresponding percentages of occurance.

I don't know how to do this but I thought I shoud start by first saving the text in an array so that I can at least try to find out the frequency of each letter.

As you can probably tell, I'm struggling with this :P.
Topic archived. No new replies allowed.