Hello all,

I am having an issue with ifstream.

Short explanation:

1. I ask the user for the filename and read it via cin
2. I store the input in a c++ string
3. I use the input as the filename

Here's where it fails:
When a user enters something like input4.txt. When only alpha characters with one period is used the program works fine i.e. input.txt, myinput.txt, etc work fine. But when a user enters something like input4.txt, input10.txt, etc I get one of the following 2 errors:

in xcode: pointer being freed was not allocated
in gcc on linux: I just get incorrect output

Here's a snippet of my code:

cout << "Please enter the input file's name $";
cin >> inputFile;
ifstream infile;
infile.open (inputFile.c_str());

if (!infile.is_open()) {
  cout << "Error, could not open input file\n";
  exit(1);
}

If you need more information please feel free to let me know. Otherwise, does anyone know what I'm doing wrong and what I could do make this work?

Thanks!!

Assuming inputFile is type string, looks fine to me. I assume the file actually exists and the permissions are correct, file is available, etc.? To narrow down the bug, try hard-coding the filename. See if it makes a difference.

infile.open ("input4.txt");

But here's a thought. Why are you using gcc? If this is C++, use g++.

Pointer not being freed? If inputFile is type string, what pointer? Sounds like there is relevant code that you haven't posted OR there is a problem BEFORE this.

  1. Try the hard coded filename.
  2. Confirm that you're using a C++ compiler.
  3. Confirm that inputFile is type string.
  4. Make sure that inputFile actually holds the string you think it does.
  5. Permissions, file existence, etc. etc.

Thanks for the quick reply.

To answer your points:
1. Hard coded filename works fine for all test cases
2. I use the c++ compiler not the c compiler. My bad! Of course being c++ code I use g++ filename.cpp -o filename.o as my command. g++ being part of the gcc package caused me to say gcc compiler
3. inputFile is of type string. Tried using both c style string (i.e. an array of chars) and modifying the open() call as well as c++ style string and calling open() as you see above. Neither works in this case
4. inputFile gets printed out to the screen after the user inputs the filename for testing and always confirmed that it holds the string the user inputs
5. permissions were even set to 777 so everyone can access the file and no go. THe file does exists (the program checks for that) and even successfully opens the file (as no error message was printed).

Seems like a really peculiar bug and I would love to hear if anyone has encountered this problem or has any possible solutions.

More information is available:
using a filename like "input2z.txt" (without the quotation marks) works fine, as does "input.txt". Seems like its the pattern of a number followed by a period that gives me the issue.

Any help will be much appreciated!!!
Thanks!

I guess it wouldn't have compiled at all if you were using gcc. Just grasping at straws. Code looks fine.

Try the code not as a snippet, but as the whole program:

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

int main()
{
    string inputFile;
    cout << "Please enter the input file's name $";
    cin >> inputFile;
    ifstream infile;
    infile.open (inputFile.c_str());
 
    if (!infile.is_open()) {
        cout << "Error, could not open input file\n";
        exit(1);
    }

    infile.close();
    return 0;
}

See if you get the same bad results. If you get good results, your answer to question 1:

Hard coded filename works fine for all test cases

suggests that perhaps the string doesn't hold what you think it does. It either has some weird control character in there or the input stream has something leftover in it or whatever. Try flushing the input stream before the cin statement and try displaying inputFile to the screen to make sure it contains what you think it does.

The only other thing I can think of is that the file is locked by the operating system for whatever reason, but then it wouldn't work when you hard-coded the filename either. If inputFile is astring, I can't think of anything about the pattern you describe that would not make it work unless, again, the string does not in fact hold the filename the user entered. Again, display it to make sure and try it as its own program as mentioned above. See if there is any difference. Baffling.

Guess I missed #4, so cross that off the list.

4. inputFile gets printed out to the screen after the user inputs the filename for testing and always confirmed that it holds the string the user inputs

But wait a minute:

1. Hard coded filename works fine for all test cases
5. permissions were even set to 777 so everyone can access the file and no go. THe file does exists (the program checks for that) and even successfully opens the file (as no error message was printed).

See red bold. I thought the error message was printing. The code snippet stops with the file opening, so something bad is happening AFTER this and the file DOES get opened successfully? And you're sure it's the SAME file that works just fine when you hard code it? You many need to elaborate. Do you use the inputFile string for anything except that filename? If so, perhaps the error is unrelated to the infile stream and the file.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.