I am working on a C++ HW assignment dealing with file I/O. I've never done this before, and is probably pretty obvious, but I would like to ask a quick question about including a path to my output and input files. In the example I am storing it in the c drive of a temp directory, though it is not showing up anywhere. Am I going about this wrong, or am I not looking in the right place in my c drive? Thx for the help, here a little copy of my code
ifstream in;
ofstream out;
in.open("c:/temp/gradeBook.txt");
if(in.fail());
{
cout << "Input file opening failed.\n";
exit(1);
}
out.open("c:/temp/gradeBook2.txt");
if (out.fail());
{
cout << "Out put file opening failed. \n";
exit(1);
}
cout << "A new gradefile has been comepleted" << endl;
calculate(in,out);
out.close();
in.close();
system("PAUSE");
return 0;
}
Related
#include<iostream>
#include<string>
#include<fstream>
#include<unistd.h>
using namespace std;
int main(){
fstream f;
f.open("test.txt" , ios::app);
string s="Hello";
if(f.is_open()){
cout << "File Open"<<endl;
f << s;
sleep(15);
if(f.is_open()){
cout << "Still Open" << endl;
}else{
cout << "Not Open" << endl;
}
s= "Hey \n";
f<< s;
}else{
cout << "Not open" << endl;
}
f.close();
}
}
This code will write contents to test.txt file upon normal execution.
But if I delete the file while the program is running, then I find that the program throws no exception and it exits successfully. But I do not find the file text.txt after I deleted it.
What should I do to know whether writing to the file is happening successfully?
It depends on operating system you are using.
On Windows, the OS will prevent you from removing such file in first place.
On Linux, the OS will let you remove such file, but will keep handle to this file until it'll be freed by process which use it (in this case, when fstream will release resource).
Under Linux the file continues to exist until the program closes the fstream, but that doesn't mean you see it through ls. When the fstream is closed the file is really removed
Why does fin.fail() return true in the following code?
void loadName(){
int pointer; //holds size of file
fin.open("C:\\Users\\iAMspecial\\Desktop\\Names.txt", ios::app);
if (fin.fail()) {
cerr << "could not open intput file names.txt" << endl;
system("pause");
exit(1);
}
pointer++;
getline(fin,Names[pointer]);
for(int ndx = 0; !fin.eof(); ndx++){
pointer++;
getline(fin,Names[pointer]);
}
fin.close();
counter = pointer;
}
I've been struggling with std::ifstream in this function. I've scouted the other questions and even with all the advice, I can’t seem to get the function working.
A lot of the issues also seem to stem from Visual Studio, however I'm using a different IDE. Apologies in advance if I missed something really stupid.
I've made doubly sure of the file path, it is 100% correct. I'm truly stumped.
Picture of output:
The program is quite long, however if any other parts of it are relevant to the issues I'm having I'm happy to post it.
(Also note that the file path is temporary, I'm merely trying to have the function work, at that point I will have it work with different file paths).
Use fin.is_open() instead. fin.fail() is not for checking stream open errors.
if (!fin.is_open()) {
cerr << "Error: " << strerror(errno);
}
Also, the correct way to read file line-by-line is
std::string line;
while (getline(fin, line)) {
// Do whatever with line
}
Every system call that fails update the errno value.
You can have more information about what went wrong if you will print it:
cerr << "Error: " << strerror(errno);
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.
my partner wrote a bunch of code for one of my projects in a text editor, when i run the code it works perfectly.....
now i have copy and pasted all the code into qt creator, and im having an issue
stringstream ss;
string line;
ifstream myfile;
myfile.open("Instructors.txt");
if (myfile.is_open()){
while (getline(myfile,line)){
ss << line << ", ";
}
myfile.close();
}
else cout << "bad open" << endl;
above is the part fo my code that is having the issue, i can assure you all Instructors.txt is indeed in in the correct file, but everytime our code reaches this point imstead of opening the file i get thrown to the else "bad open" why would this be?
It's hard to say what it may be without any error code, what you can do is to improve your error message with something more meaningful (for you and for your customers):
else cout << "Error opening file: " << strerror(errno) << endl;
strerror (see reference) function returns a string for a given error code captured in by errno macro.
Otherwise you can do it much more C++ish using exceptions: first enable them for your stream:
myfile.exceptions(ifstream::failbit | ifstream::badbit);
Then catch them, all together is:
try
{
ifstream myfile("Instructors.txt");
myfile.exceptions(ifstream::failbit | ifstream::badbit);
while (getline(myfile, line))
{
ss << line << ", ";
}
myfile.close();
}
catch (ifstream::failure e)
{
cout << e.what() << endl;
}
Try to rewrite file name, may be it contains characters from different encodings.
double check the working directory, chances are it is in the build folder (where the executable gets dropped)
in QtCreator you can fix this by going to projects and selecting run; there you will be able to set the working directory
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.