1

I am completely perplexed as to why this doesn't work. I want to read in one line of an input .txt file as one long string and then print it. It seems to me that the most common way is to read it in as a C-String, but my main program(the code I have posted is just an example) will be much simpler if I can read it in as a normal c++ string. What am I doing wrong?

    #include <iostream>
    #include <fstream>
    #include <string>
    using namespace std;

    int main(int argc, char *argv[]){
        ifstream inStream;
            string line;
        inStream.open(argv[1]);
        getline(inStream, line, '\n');
        inStream.close();
        cout << line;

        return 0;
    }

When I run this, it outputs nothing. I am sure that my .txt file is inputted correctly and has characters in it.

2

1 Answer 1

2

Dump getcwd() to cout to make sure you're about to open a file in the place you think it exists. Chances are you're not where you think you are.

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

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.