从流c ++中获取字符

时间:2016-04-24 19:31:19

标签: c++ file stream character

FILE *in_fp;   

 void getChar() 
     {
        if ((nextChar = getc(in_fp)) != EOF) 
        {
            if (isalpha(nextChar))
                charClass = LETTER;
            else if (isdigit(nextChar))
                charClass = DIGIT;
            else charClass = UNKNOWN;
        }
        else
            charClass = EOF;
    }

我希望在c ++中使用函数等效函数getc() 我搜索但我找不到

请注意 如果我使用FILE,编译器会显示此错误

source.cpp(47): error C4996: 'fopen': This function or variable may be unsafe. Consider using fopen_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.

1 个答案:

答案 0 :(得分:0)

我想你想打开一个文件并使用C ++ I / O机制读取一个字符。您可以使用I / O流来完成此操作。

你可以这样做:

#include <fstream>
....
std::ifstream in;//an input file stream
char nextChar;

in.open("your_file_name");//open a file for reading
if(in)
{//if opened successfully
    in.get(nextChar);//read a character from the stream
    .....//your other code
}