-
Text file problem.
I was just wondering if anyone could explain what is happening in the following bit of code. I know that the code will keep getting data from the file and outputting it to the screen @ a char @ a time, but on each itteration of the loop how does the program know which character it should be outputting next. I understand it will stop once the end of file is reached, but how is the code keeping a count of which character should be displayed?
void main()
ifstream InFile;
char InChar;
InFile.open("Test.txt");
InFile >> InChar;
while(!InFile.eof())
{
cout << InChar << endl;
InFile >> InChar;
}
InFile.close();
getchar();
}
-
Can someone reply on this cos I can't get my head around the problem. Please! Thanks.
-
i always thought you use get and put for getting chars from files because...well im a noob but i think you should here:
Code:
#include <iostream.h>
#include <fstream.h>
#include <conio.h> // you need this for getchar() right?
int main()
{
ifstream InFile;
char InChar;
InFile.open("C:/Test.txt"); //you gotta have a directory don't you? i use U:/ at school, but you prolly should use C:/
InFile.get(InChar);
while(! InFile.fail())
{
cout << InChar //i took away the endline because then it will output everything including endlines in your file
InFile.get(InChar);
}
InFile.close();
getchar(); //i never knew how to get this working but if it works for you cool
return 0;
}
i hoped i helped in some way, that program should be able to input an entire paragraph word for word :D
-
Well, in C, when you declare a *FILE, it contains a pointer called the "file position indicator", which tracks the current position in the specified filestream. This pointer is taken care of by the operating system and works the similarly for C++ programs.
-
It knows what to do because the InChar variable is being used to store the characters read in from the file. The cout statement in the loop outputs the value of the InChar variable to the screen and the InFile >> InChar; statement stores the characters from the file into the variable.
-
>but how is the code keeping a count of which character should be displayed?
The variable that you use to read from the stream is a simple char, it won't hold anymore so when you print out that char and try to read another the stream will have moved ahead one char (ie. the next character in the file). If you were reading an array of 20 chars then that array would be filled and the stream would move ahead 20 characters.
This isn't exactly how streams work, but I didn't want to confuse you too much. ;) You can safely assume that the stream you create an instance of will handle that type of thing so there's no need to worry about the actual mechanics of it.
-Prelude
-
The file stream moves forward each time a character is read. The character variable gets overwritten each time you read from the file. What was in the variable is lost once the next character gets loaded in. There is no "count" unless you are maybe thinking about the position in the file from which you are reading from, the position that gets incremented by one byte with each act of reading a single character from that file.