从C ++中获取文件输入

时间:2010-03-10 02:38:45

标签: c++ file-io

我目前正在开发一个应用程序,它从文本文件中获取输入并相应地继续。概念是输入文件将在此fomat中有详细信息

A AND B
        B OR C

每一行都将由空格分隔,输入必须从文本文件中获取并由逻辑处理。我使用TCPP编译器,我在阅读输入时遇到问题。请帮我解决这个问题...

3 个答案:

答案 0 :(得分:3)

一次读取一行输入通常使用std::getline完成,如下所示:

std::string line;
std::ifstream infile("filename");

while (std::getline(line, infile))
    // show what we read
    std::cout << line << "\n";

如果你遇到这样的问题,你可能会考虑找一本关于C ++的(更好的)书而不是你现在(希望)使用的那本书。

答案 1 :(得分:1)

以下可以直接使用:

BOOL ReadFile(CString filename)
{
    BOOL bRead = TRUE;

    std::ifstream m_strmFile;
    m_strmFile.open(filename, std::ios::in);

    char pszLine[256];
    memset(pszLine, 256, 0);

    if (m_strmFile)
    {
        // Read whatever number of lines in your file   
        for (unsigned int i = 0; i < 5/*number of lines*/; i++)
        m_strmFile.getline(pszLine, 256);
        // Do whatever you want to do with your read lines here...
    }
    else bRead = FALSE;

    return bRead;
}

答案 2 :(得分:0)

您使用的是headr文件:

包括

或#include,你可以使用fileobject.getline(),(检查它的正确语法。)函数在C ++中或char用char通过使用fileobject.get(ch)类函数