开发者

Reading a file - won't open

I am trying to open a file in C++ but it seems to be giving me a bit of hassle, here is the code that deals with opening the file so far:

void CreateHistogram(string str_file, vector<HistogramWord> &result) {
    string line;
    long location;
    HistogramWord newWord;
    const char * filename = str_file.c_str();

    //ifstream myfile (str_file.c_str());
    ifstream myfile (filename);
    //myfile.open(filename);
    if (myfile.is_open()) {
        while (myfile.good()) {
 开发者_运维知识库           getline(myfile, line);
            line = clarifyWord(line);

Okay, just for a bit of explanation, HistogramWord is a struct that is defined in the header and from what I have read in the online documentation, the filename has to be of type const char *, so that is what I have done. Converted str_file to be a const char *.

Now, I have tried a few different things which is why some of the code is commented out. When it gets to the line if (myfile.is_open()), it always evaluates to false. Anyone seem to know why?

Thanks, Brandon


OK IO 101

If you don't give the complete filepath but only the filename then the current working directory will be appended to the filename.

So if your .exe is in C:\temp and you call your program from this directory and your filename is test.txt then the complete filename in this case will be C:\temp\test.txt

This will only work if the .exe and the test.txt are both under C:\temp.

In all other cases it will fail. You could create the absolute path by using win API or the linux equivalent - I don't know what platform you are on.

Now in order to read a succsfully opened file this will suffice :

void CreateHistogram(string str_file, vector<HistogramWord> &result) {
string line;
long location;
HistogramWord newWord;

ifstream myfile (str_file.c_str());
if (myfile.is_open()) {
    while (getline(myfile, line)) {
        line = clarifyWord(line);
}
else{
   //throw exception, print error message etc
   throw std::exception(std::string("Couldn't open file : " + str_file).c_str());
}
}

edit : Thanks @ Shahbaz


My best guess is that Windows is "hiding extensions for known file types" so the name of the file is actually different than what you have put in windows. For example if it's a .txt file, and you name it test.txt, the actual name would be test.txt.txt which is quite a stupid thing windows does.

To change this, go to My Computer -> Toold -> Folder Options -> And uncheck the box that says "Hide extensions for Known File Types". This is for XP. If you have another windows it should be more or less the same path. If you don't see the toolbar, try ALT+t (tools) or ALT+f (file) to make it appear.

This problem give quite many of us a trouble in the first semester of college.


What fixed it for me was using forward slashes instead of double backslashes in my filepath.

e.g.

inFile.open("path/to/file.txt")

instead of

inFile.open("path\\to\\file.txt")
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜