C++ Project Won't Write to file After Export From xCode - c++

I am having a strange issue. I have a function (writedata) that take a string and writes it to a file. Is works fine and files are created when I build and run the program within xCode.
When I export the compiled project from xCode and just try to run the executable terminal app no file is ever written. I can see the process completes in terminal with no errors given. I even get the 'cout<<"Done. File Written!"' message but no file.
void writedata(string writedata, string filename){
ofstream data(filename, ios::app);
if (data.is_open())
{
data<<writedata;
cout<<"Done. File Written!";
data.close();
}
else cout << "Unable to open file";
data.close();
}
Does anyone know why this is happening?

Related

"Unrecognized file" when I try to open it C++

I'm using Netbeans 8.2 and I am programming with C++.
I'm trying to read a file. I have put it in the correct project folder of sources and I have used #include <fstream> .
ifstream myfile ("figures.txt");
code...
myfile.close();
I have the file in the correct place. But this message pops up:
Unrecognized file
Then, my other part of code also gives me error, because never detects the file I'm talking about open.
if (myfile.is_open()){
code...
}else{
cout << "file not open" << endl;
}
Auto solved, the .txt must be in the project folder, not in nbproject folder. At least it has worked doing it this way. Any more reccomendations will be accepted!

I can't read a .dat file using fstream

I know there are a ton of questions pertaining to this subject but I cannot get this to work. The program ran fine on my laptop, but when I try to compile and run it in the the schools Linux lab the program cannot open the file. I have tried defining the absolute file position but nothing has worked. The file name is correct and everything but when I try to run the program it displays "failed". I'm using gedit and compiled the program with bash.
ifstream fin("rainfall.dat"); // If the file cannot open display failed
if(fin.fail()){
cout << "failed" << endl;
return 1;
}
try
#include <errno.h>
if(fin.fail())
perror("open failed ");
this will give you a human readable message for the last error

ifstream stopped working in eclipse

I have a C++ project that I haven't used in a year. It used to run perfectly, opening files with:
Matrix pmatrix;
pmatrix.readFromFile("pmtx.txt");
and in the function:
void Matrix::readFromFile(string filename){
ifstream tmfile;
tmfile.open(filename.c_str());
if (!tmfile) {
cout << "unable to open tm file";
//return 1;
}
etc
}
It keeps telling me "unable to open tm file". I've been breaking my head about this. Obviously the file is in the directory of the src and I copied it to Debug and Release, I gave it full permissions. I use eclipse.
Any ideas why this just stopped working?
There was something strange screwed up with the eclipse project. I created a new project, copied in existing files and it worked.

unable to open file using complete path in ubuntu with boost C++

Playing around wit boost library using ubuntu, with the following code the file main.cpp gets open and being printed but when I try to open some other file on some other location, it does not work.
Here is the code
std::string line;
boost::filesystem::ifstream file ("main.cpp") ;
if (file.is_open())
{
while ( getline (file,line) )
{
std::cout << line << '\n';
}
file.close();
}
else std::cout << "Unable to open file"<< std::endl;
Here is the path that I am giving and its not working.
boost::filesystem::ifstream file ("/home/0circle/workspace/practice/main.cpp") ;
can someone tell me what could be the problem and how to cure it ?
Thanks :)
Make sure the file exists and you have permissions for reading the file:
execute in shell: ls -l /home/0circle/workspace/practice/main.cpp
verify if you have read rights (first column of the output of the
previous shell command). See this!
try to find out reason of failure: See this!

file writing: works well when compiling directly on terminal but not when through Qt 5

Im writing the following code to write a value from a function to a file.
int main(){
ofstream myfile;
myfile.open("data1.txt");
double angle = image();
myfile<<angle<<"\r";
cout<<"abc";std::cout<<angle<<"\n";
myfile.close();
return 0;
}
when complied and run directly on terminal, the file is updated.
BUT, if i run tthe out file from inside qt creator using std::system command, file is not updated, the previous value is retained.
from qt creator i call:
std::system("./binaryName");
where "binaryName" is my out file
what might be happening?