簡體   English   中英

從C ++中獲取文件輸入

[英]Getting input from a file in C++

我目前正在開發一個應用程序,它從文本文件中獲取輸入並相應地進行。 概念是輸入文件將在此fomat中包含詳細信息

A AND B
        B OR C

每個行都將由空格分隔,輸入必須從文本文件中獲取並由邏輯處理。 我使用TCPP編譯器,我在閱讀輸入時遇到問題。 請幫我解決這個問題......

一次讀取一行輸入通常用std::getline ,如下所示:

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

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

如果你遇到這樣的問題,你可能會考慮尋找一本關於C ++的(更好的)書而不是你現在(希望)使用的書。

以下可以直接使用:

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;
}

你使用的headr文件如下:

包括

或#include,你可以使用fileobject.getline(),(檢查其正確的語法。)函數在C ++中或char用char通過使用fileobject.get(ch)類函數

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM