I am trying to open these two files and read their contents into two different arrays, but whenever I try and open them I get the unable to open file dialog? I don't see anything incorrect but I am not a strong c++ user.
std::ifstream inFile;
inFile.open("fives.txt");
if (inFile.is_open())
{
while (! inFile.eof() )
{
getline (inFile,line);
fives[loop] = line;
cout << fives[loop] << endl;
loop++;
}
inFile.close();
}
else cout << "Unable to open file";
inFile.open("search.txt");
loop=0;
if (inFile.is_open())
{
while (! inFile.eof() )
{
getline (inFile,line);
search[loop] = line;
cout << search[loop] << endl;
loop++;
}
inFile.close();
}
else cout << "Unable to open file";
The files must exist in the current directory, where the current directory is the directory from which the program was executed (not necessarily the one where the executable is saved at).
In your case, you saved the files with the resources, not with the resulting binary (I'm guessing you're running from within the VC++, by default it sets the current directory to where the binary is stored), so the program cannot find them. Use either relative path to where the resources are, or copy the files you're looking for into the directory you're running from.
Related
I'm trying to read multiple files in a folder so I can parse through their data.
I first try to fill the list using a text document with all the file names in it, then based on that vector of string, continuously call ifstream so I can read every file and process the word data.
The problem I'm running into is that ifstream is failing to open all of the files, except one in the middle of the list?
Heres the output, its failing to read the dbfiles but they all have the right names?
These files aren't more than 8GB a piece so it should be able to handle it but it's not?
maybe theres a problem with the file paths?
std::ifstream dbfiles(argv[1]);
if (!dbfiles)
{
std::cerr << "Failed to open database " << argv[1] << " for reading." << std::endl;
}
std::string word;
std::vector<std::string> dbfile_names;
std::string file_name;
while (getline(dbfiles, file_name))
{ //reading in the file names
dbfile_names.push_back(file_name);
}//populate list of dbs
dbfiles.close();
for (unsigned int j = 0; j < dbfile_names.size(); j++)
{ //for every single file
std::ifstream dbfile(dbfile_names[j].c_str());
if (!dbfile)
{
std::cout << "Failed to open database file" << dbfile_names[j] << " for reading. READ FAILURE" << std::endl;
}else{
std::cout << "currently reading " << dbfile_names[j] << std::endl;
}
while (dbfile >> word)
{
//do stuff with the segments of data
//here I gather the data word by word and process it
}
dbfile.close();
}
I went into my debugger and found that due to getline, all the file names had a /r at the back of them.
The post over here Getting std :: ifstream to handle LF, CR, and CRLF?, helped describe the problem and how to easily fix it.
My files are now reading accordingly
i just started reading on how to open and edit files.
when working with ifstream, if the file doesnt exist, it wont be created.
in reference to the code below, when would the condition (!outfile) be false, as if the file doesn't exists it will simply be created by the constructor, hence always making the condition false.
int main()
{
ofstream outfile ("test1.txt");
if (!outfile)
{
cout << "cannot create file test1.txt" << endl;
return 1;
}
outfile << 10 << " " << 345.12 << endl;
outfile << "This is a short text file";
outfile.close();
system("PAUSE");
return 0;
}
One way opening an ofstream could fail is if the file in the given path exists, but you do not have the permission to write to it. Alternatively, if the file does not exist but you do not have permission to create a file in the given path, opening the ofstream should also fail.
Another failing situation could be if the files does not exist, and the underlying device does not have sufficient free space/inodes to create one.
I'm trying to open multiple files to compile the data in them. My program compiles but when I run it i get the following error:
terminate called after throwing an instance of 'std::bad_alloc'
what(): std::bad_alloc
Aborted
So far my program is pretty lengthy so I'll just link the parts of it that deal with opening the files.
int main(int argc,char *argv[])
{
vector<Plays> yearsEntered;
Plays *MyPlays = new Plays();
if (argc < 2)
{
cout << "No filenames given." << endl;
return 0;
}
for(int i=1;i < argc; ++i)
{
string filename = argv[i];
cout << filename << endl;
filename.append(".csv");
cout << filename << endl;
ifstream inputFile(filename.c_str(), ios::in);
inputFile.open(filename.c_str());
//Error checking in case file fails to open
if (!inputFile)
{
cout << "Could not open file. " <<
"Try entering another file." << endl;
}
}
I'm not quite sure why I'm getting an error but if I had to guess i'd say it was something to do with the fact that argv[i] is a *char array and I'm setting it equal to a string. Also when i run the program it's run like this: ./Analyze 2009 2010 (etc). When i run it it'll print out the name of the file that I want to open so I know the problem is when it tries to open the file itself. This is my first time asking a question so if there's any convention I failed to follow let me know and I'll try to fix it.
You already opened your files once. You don't need to open them again.
The std::ifstream constructor is opening each file, then your invoking .open() for no reason. Remove the inputFile.open() line.
Change this:
ifstream inputFile(filename.c_str(), ios::in);
inputFile.open(filename.c_str());
To this:
ifstream inputFile(filename.c_str());
I want to dynamically read a file for the appended content. The file in written by another thread. I'm using ubuntu, and had a look at inotify, but wanted to try and implement a simple way for this since I only need to read the appended content. What I tried is as follows:
ifstream logFile("path_to_file", ifstream::in);
if(logFile.fail()) {
cout << "file not found" << endl;
exit(0);
}
while (true) {
string line;
getline(logFile, line);
if( logFile.eof() ) {
//wait for 2 seconds
logFile.clear();
}
if(!line.empty())
cout << line << endl;
However, when the EOF is encountered, the flag doesn't get reset when there is more content appended at the end and even if I call clear() to clear the error condition. What can be the issue in this approach? Do I need to open the file in a shared mode?
Thanks.
After the program reads the file, gets characters from the file, and finishes, the user is asked if they want another file read or not. If the user say yes, then the program asks for the file name, but then automatically says the file could not be opened and exits the loop. Please help me.
Here is the code:
do //do while opening the source file fails
{
cout << "Enter filename of source file: ";
cin.getline (filename,51);
sourceFile.open(filename); //opens the file with given filename
if (sourceFile.fail())
cout << "File could not be opened" << endl; //error if can't open
sourceFile.clear();
}
while (sourceFile.fail()); //exits if source file doesn't fail
This test:
while (sourceFile.fail())
will never be true because just before you get there you call:
sourceFile.clear()
which will clear any problem bits in the iostate for the stream.
I think you just want to get rid of the call to clear().
The canonical way to check if opening the file failed is to use std::basic_ios::operator !():
do
{
cout << "Enter filename of source file: ";
std::getline(std::cin, filename);
sourceFile.open(filename.c_str());
if (!sourceFile)
{
cout << "File could not be opened" << endl;
}
}
while (!sourceFile);