Hi, I was having this exercise where I had to write some input/output functions in C++ but call C functions to do the job and throw an exceptions if errors occurred.
So now I have to write to file using fprintf and I suppose I'll use scanf to get input from console
When I was reading file I used this simple code (don't know if it's the best way to go)
Code:
FILE* fp = open_file(filename, "r");
for (char ch = 0; fscanf(fp, "%c", &ch) != EOF;)
printf("%c", ch);
I think than when I'm getting some random text from console I also have to work with chars coz I don't know if next thing I'm getting is char or float or anything else from the input
When I have to do something like this in C++ I would use something like this
Code:
for(std::string line; std::getline(std::cin, line);){
if(line == "__STOP__") break; // woud use some keyword to terminate input
//do something with line for example write line to some file
}
Is there some smart way to do the above example without using some terminating string?
How can I read some long random texts from console using scanf("%c", &ch)?
Is there a way to trigger EOF while using code something like this
Code:
while(scanf("%c", &ch) != EOF){//do something with char
}
Is there a way to trigger std::cin.eof() if I'm using std::getline(std::cin, line) or even std::cin >> ch //char ch;
Any help appreciated.
EDIT : Why is my code looking so ugly except the middle one? Same tags around all of them.