Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
147 views
in Technique[技术] by (71.8m points)

c++ - Am I missing something? I keep outputting "No file found!"

void getBookData(bookType books[], int& noOfBooks)
{
    ifstream infile;
    string file = "bookData.txt";
    infile.open(file.c_str());
    if (infile.fail()) {
        cout << "No file found!" << endl;
        infile.clear();
    }
    while (true) {
        string line;
        getline(infile, line, '
');
        if (infile.fail()) {
            break;  
        }
        cout << "Line: " << line << endl;
    }
    infile.close();
}

I've tried putting the file in every location I can think of, but somehow it's not loading in. Or, more likely, I'm doing something else wrong. This isn't anything like what the end result of my code is supposed to be like, right now I'm just trying to read out my file line by line.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I guess you really need help debugging why this is happening to you.

Try adding some more code to your routine to help you determine what is going on. One thing to try is to call getcwd.

#include <unistd.h>

...
char buf[PATH_MAX];
std::cout << "cwd: " << getcwd(buf, sizeof(buf)) << std::endl;
...

This should report to you where your program thinks it is running from.

Start with that first, and I am guessing the next steps will become obvious to you.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...