I am trying to switch from stdio to iostream, which is very difficult. I have the basics of downloading a file and closing it, but I really don't know what else a stream is or how they work.
In stdio, everything is relatively easy and straightforward compared to this. What I need to do is
- Read one character from a text file.
- A function call based on what this character is.
- Repeat until I read all the characters in the file.
What I have so far is not much:
int main() { std::ifstream("sometextfile.txt", std::ios::in); // this is SUPPOSED to be the while loop for reading. I got here and realized I have //no idea how to even read a file while() { } return 0; }
I need to know how to get one character and how this character is actually stored (is this a string? Int? A char? Can I decide for myself how to save it?)
As soon as I know that, I think I can handle the rest. I will store the symbol in the appropriate container, and then use the switch to do something based on what this symbol really is. It would look like this.
int main() { std::ifstream textFile("sometextfile.txt", std::ios::in); while(..able to read?) { char/int/string readItem; //this is where the fstream would get the character and I assume stick it into readItem? switch(readItem) { case 1: //dosomething break; case ' ': //dosomething etc etc break; case '\n': } } return 0; }
Please note that I need to check for spaces and newlines, hope this is possible. It would also be useful if, instead of one universal container, I could store numbers in int and characters in char. I can get around this, if not though.
Thanks to everyone who can explain to me how threads work and what is possible with them.
Jcrack
source share