Reading a single character from fstream?

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.

+8
source share
5 answers

You can also abstract streambuf_iterator whole idea of ​​getting a single character with streambuf_iterator s if you want to use any algorithms:

 #include <iterator> #include <fstream> int main(){ typedef std::istreambuf_iterator<char> buf_iter; std::fstream file("name"); for(buf_iter i(file), e; i != e; ++i){ char c = *i; } } 
+8
source

You can also use the standard for_each algorithm:

 #include <iterator> #include <algorithm> #include <fstream> void handleChar(const char& c) { switch (c) { case 'a': // do something break; case 'b': // do something else break; // etc. } } int main() { std::ifstream file("file.txt"); if (file) std::for_each(std::istream_iterator<char>(file), std::istream_iterator<char>(), handleChar); else { // couldn't open the file } } 

istream_iterator skips whitespace. If they make sense in your file, use istreambuf_iterator instead.

+7
source

fstream :: get

The next time you have a similar problem, go to cplusplusreference or a similar site, find the class , you have a problem reading the description of each method. This usually solves the problem. Googling also works.

+2
source

This has already been answered, but whatever. You can use the comma operator to create a loop that behaves like a for every loop that goes through the entire file, reads each character each time, and stops when it is done.

 char c; while((file.get(c), file.eof()) == false){ /*Your switch statement with c*/ } 

Explanation : The first part of the expression in the for loop (file.get(c), file.eof()) will work as follows. First, file.get(c) which reads the character and stores the result in c . Then, due to the comma operator, the return value is discarded, and file.eof () is executed, which returns bool if the end of the file has been reached. Then this value is compared.

Side Note: ifstream::get() always reads the next character. This means that calling it will read the first two characters in the file twice.

0
source
 while (textFile.good()) { char a; textFile.get(a); switch(a) { case 1: //dosomething break; case ' ': //dosomething etc etc break; case '\n': } } 
-2
source

All Articles