简体   繁体   中英

Getting input from a file in C++

I am currently developing an application, which gets the input from a text file and proceeds accordingly. The concept is the input file will have details in this fomat

A AND B
        B OR C

Each and every line will be seperated by a blank space and the input must be taken from the text file and processed by logic. I use a TCPP compiler and i am facing problems reading the input. Please help me with the issue...

Reading input a line at a time is normally done with std::getline , something like this:

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

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

If you're having trouble with things like this, you might consider looking for a (better) book on C++ than whatever you're now (hopefully) using.

Following can be used straightaway:

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

are you using headr files like:

include

or #include and you can make use of the fileobject.getline(), (do check its proper syntax.) function in C++ or for char by char use fileobject.get(ch) kind of function

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM