-
Errrr help please
This displays whats in the text file but how can i make it store what it displays in test. I did test=infile.get() but it doesnt work.
I had it working with something I did but I didn't save and I don't remember how I did it...err very aggervating....its drivin me crazy
:mad: :mad: :confused: :confused:
Code:
cout<<"Enter The Input File(C:/example.txt): ";gets(input_file);fflush(stdin);
ifstream infile;
infile.open (input_file, ifstream::in);
while (infile.good())
cout<<(char) infile.get();
test=infile.get();
cout<<test<<endl;
infile.close();
-
as far as I know, this should work:
Code:
char test;
infile >> test;
-
-
//obtain file name to obtain text;
cout<<"Enter The Input File(C:/example.txt): ";
cin >> input_file;
//variables to use in reading file, displaying input, and saving input
char test[1000];
char ch;
int index = 0
ifstream infile;
//open stream and associate file with stream
infile.open (input_file);
Code:
//read in first char
infile >> ch;
//while stream is "good" and haven't overead buffer
while (infile.good() && index < 998)
{
//display input on screen
cout<<(ch);
//save inpu to buffer
test[index++] = ch;
//get next input
infile >> ch;
}
//change char array to string
test[index] = '\0';
//output buffer
cout<<test<<endl;
infile.close();
-
Thanks that worked but what can I do if my file is bigger than 1000 characters?
-
If you declare too big an array you will overflow the programs static variable stack so you would need to use dynamic memory to declare the array, or use a list to hold the input rather than an array, which would also likely use dynamic memory.
-
how would you do that? :confused:
-
char * buffer = new char[1000000];
declares a buffer to hold 999,999 char (and a terminal null char) dynamically, assuming there is that much memory available in RAM. Whenever you declare dynamic memory, you also should release it . To do so with an array you would do this:
delete [] buffer;
I suggest you read up on dynamic memory in your favorite tutorial or text. It is a topic that is really too big to discuss fully in this type of forum. It is something you should learn how to use, if you wish to do much of anything besides simple programs however.