How to Read from a Text File, Character by Character in C++

后端 未结 8 1802
孤街浪徒
孤街浪徒 2020-12-06 10:17

I was wondering if someone could help me figure out how to read from a text file in C++, character by character. That way, I could have a while loop (while there\'s still te

相关标签:
8条回答
  • 2020-12-06 10:34

    @cnicutar and @Pete Becker have already pointed out the possibility of using noskipws/unsetting skipws to read a character at a time without skipping over white space characters in the input.

    Another possibility would be to use an istreambuf_iterator to read the data. Along with this, I'd generally use a standard algorithm like std::transform to do the reading and processing.

    Just for example, let's assume we wanted to do a Caesar-like cipher, copying from standard input to standard output, but adding 3 to every upper-case character, so A would become D, B could become E, etc. (and at the end, it would wrap around so XYZ converted to ABC.

    If we were going to do that in C, we'd typically use a loop something like this:

    int ch;
    while (EOF != (ch = getchar())) {
        if (isupper(ch)) 
            ch = ((ch - 'A') +3) % 26 + 'A';
        putchar(ch);
    }
    

    To do the same thing in C++, I'd probably write the code more like this:

    std::transform(std::istreambuf_iterator<char>(std::cin),
                   std::istreambuf_iterator<char>(),
                   std::ostreambuf_iterator<char>(std::cout),
                   [](int ch) { return isupper(ch) ? ((ch - 'A') + 3) % 26 + 'A' : ch;});
    

    Doing the job this way, you receive the consecutive characters as the values of the parameter passed to (in this case) the lambda function (though you could use an explicit functor instead of a lambda if you preferred).

    0 讨论(0)
  • 2020-12-06 10:38

    Here is a c++ stylish function your can use to read files char by char.

    void readCharFile(string &filePath) {
        ifstream in(filePath);
        char c;
    
        if(in.is_open()) {
            while(in.good()) {
                in.get(c);
                // Play with the data
            }
        }
    
        if(!in.eof() && in.fail())
            cout << "error reading " << filePath << endl;
    
        in.close();
    }
    
    0 讨论(0)
  • 2020-12-06 10:39

    You could try something like:

    char ch;
    fstream fin("file", fstream::in);
    while (fin >> noskipws >> ch) {
        cout << ch; // Or whatever
    }
    
    0 讨论(0)
  • 2020-12-06 10:44

    There is no reason not to use C <stdio.h> in C++, and in fact it is often the optimal choice.

    #include <stdio.h>
    
    int
    main()  // (void) not necessary in C++
    {
        int c;
        while ((c = getchar()) != EOF) {
            // do something with 'c' here
        }
        return 0; // technically not necessary in C++ but still good style
    }
    
    0 讨论(0)
  • 2020-12-06 10:47

    Re: textFile.getch(), did you make that up, or do you have a reference that says it should work? If it's the latter, get rid of it. If it's the former, don't do that. Get a good reference.

    char ch;
    textFile.unsetf(ios_base::skipws);
    textFile >> ch;
    
    0 讨论(0)
  • 2020-12-06 10:48

    Assuming that temp is a char and textFile is a std::fstream derivative...

    The syntax you're looking for is

    textFile.get( temp );
    
    0 讨论(0)
提交回复
热议问题