Read in char from string

HI,
I am trying to read in each individual character from a text file using the fstream library. I am getting a stack overflow error when i debug using the IDE of visual C++ 2008. I believe that my problem is that i am allocating memory for only one char but my program is trying to read in the entire string. Thanks for all of your help!! Here's the code. By the way, I am trying to convert a text file to ascii by converting each character and outputting it to another file.
ifstream IF; //Input file
ofstream OF; //Output file
char inputchar[1001];
int asciiline;

for (z=0; z<1001; z++)
{
IF >> inputchar[z]; //Here's the problem (I think)
asciiline = static_cast<int>(inputchar[z]);
OF << asciiline;

}
Thank you for all of your help!!
-RDJ
Could you post your whole code and what is inside of your text documents? Also, please use the code brackets. You're trying to take say:

TEST // From file 1.

And turn it into:

######## // In file 2.
The error is due to your array being so large. When arrays are declared inside a function, they have automatic storage duration, and are stored on the stack. The stack space on most platforms is very limited (on the order of kilobytes) so the stack isn't large enough to hold the array, causing the error.
From what you posted there isn't a problem that causes the error you described.

But are you aware that the following happens with your code:

Say input text is: 123
Then the output text will be: 495051

I have a few questions about your code. You say that you're try to convert a text file to ascii, but from what? What's the original format of the text file? Also, why are you using an array, when you are outputting characters one at a time? This seems like a character-by-character operation.
First off, thank you all for your help!
Here is all of my code so far...

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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#include <iostream>
#include <windows.h>
#include <string>
#include <fstream>
using namespace std;

int main()
{
	string a;
	ifstream IF;
	string yorncontinue;
	ofstream OF;
	char line[1000];
	char inputfile[30];
	char outputfile[30];
	int x;
	string yornencode;
	int asciiline;
	int y;
	int z;
	for(;;)
	{
	for(;;)
	{
	cout << "Enter your input file (.txt):";
	cin >> inputfile;
	for (x=0;x<31;x++)
	{
		if ((inputfile[x]=='.')&&(inputfile[x+1]=='t')&&(inputfile[x+2]=='x')&&(inputfile[x+3]=='t')&&(inputfile[x+4]=='\0'))
		{
			cout << "This is a valid file!" << endl;
			y=1;
			Sleep(3000);
			system("cls");
			break;
		}

	}
	if (y==1)
	break;
	else
	cout << "This is an invalid File! Must be a text file (.txt) ...  EX: file.txt" << endl;
	}


	system("cls");
	cout << "I am about to encode this text file into ascii" << endl;
	cout << inputfile;
	cout << endl;
	cout << "Would you like to Encode it?(Y/N): " << endl;
	cin >> yornencode;
	if ((yornencode == "Y")||(yornencode == "y"))
	break;
	}
	for(;;)
	{
	x = 0;
	system("cls");
	cout << "Output file name (.txt): ";
	cin >> outputfile;
for (x=0;x<31;x++)
	{
		if ((outputfile[x]=='.')&&(outputfile[x+1]=='t')&&(outputfile[x+2]=='x')&&(outputfile[x+3]=='t')&&(outputfile[x+4]=='\0'))
		{
			cout << "This is a valid file!" << endl;
			Sleep(3000);
			system("cls");
			break;
		}

	}
	cout << "You will be exporting the encoded text to the file..." << endl;
	cout << outputfile;
	cout << endl;
	cout << "Is this correct? (Y/N): ";
	cin >> yorncontinue;
	if ((yorncontinue == "Y")||(yorncontinue == "y"))
	break;
	}
	IF.open ( inputfile );
	OF.open ( outputfile, ios::app );

	for (z=0; z<1001; z++)
	{
		IF >> line[z];
		if ((line[z] = '\0')||(line[z+1] = '\0')||(line[z+2] = '\0'))
		break;
		asciiline = static_cast<int>(line[z]);
		OF << asciiline;
	}
	IF.close ();
	OF.close ();


return 0;
}


My text file will contain any characters and numbers, along with some basic punctuation marks such as (" . , !) and others. Yeah I am aware that It would be a big paragraph of numbers but I think that it would be cool to encode a file to ascii and decode it from ascii back to text.
If my problem is that the stack is too small in size, then are there any other suggestions of how I could accomplish this task, or how do I make the stack larger?
I am open to all suggestions and comments but ease up on the criticism because I am just getting started in the amazing world of C++!!
Thank you for all of your help!!
-RDJ
Yep you left out the reason for the error. It's line 86. line[z+1] and line[z+2] may go beyond the line. 'line' is now 1000 so it must be 'z < 1000'.

This
if ((line[z] = '\0')||(line[z+1] = '\0')||(line[z+2] = '\0'))

must be
if ((line[z] == '\0')||(line[z+1] == '\0')||(line[z+2] == '\0')) // Note == instead of =
that's why I recommend
if (('\0' == line[z])||('\0' == line[z+1])||('\0' == line[z+2]))
but is still not correct if z == 999. Why do you check for 3 times '\0'?
Thank you for all of your help.
I will try your recommendation of making z < 1000 and changing = to ==.
also, I had no reason really why i checked 3 times, I just figured if someone pressed enter two times and had more text after that I would want to read that text also. This was my way of checking when the end of the document was. I then did further reserch and discovered an
"eof" command that would check when the end of the file was.
Thanks I really appreciate it!
-RDJ
Topic archived. No new replies allowed.