1

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...

5
  • 1
    it would help if you would post what kind of code are you currently using, or is this a HW problem? Commented Mar 10, 2010 at 2:39
  • so what is the problem that you are facing with reading the input? Commented Mar 10, 2010 at 2:41
  • No.. its not a HW problem.. The error i get is "Error reading from file" and also I am not sure how to get the line by line input from the file Commented Mar 10, 2010 at 2:47
  • Make sure the file you are reading exists and make sure you have read permission to that file from OS. Commented Mar 10, 2010 at 7:39
  • Show us the code where the problem occurs and the sample input files. Commented Mar 10, 2010 at 19:53

3 Answers 3

3

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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

2 Comments

Are the following symbols in TCPP (Turbo C Plus Plus): BOOL, CString? I believe those are Microsoft terms.
@ Thomas You are correct. These terms are from MS VS. Anything wrong? @ TCPP: please use bool and const char* instead of BOOL and CString or typedef bool BOOL
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.