Opening .txt file via executable file (compiled c++ code) on Mac - c++

I have an issue with my compiled c++ code on Mac. I have written an app that read .txt file, but when I compile the source code via
g++ main.cpp -o MyApp
and run it, MyApp is not opening the .txt file. (I have both executable and .txt file in the same directory).
But when I just compile my .cpp file, not making executable, just compiling (getting .out from .cpp) and run it via terminal, it works perfectly.
g++ main.cpp
./a.out
I am opening the file by:
ifstream myfile;
myfile.open("list.txt");
Does anybody know how to fix this issue and connect the executable file with that text file? I would be very grateful.

Ok I have finally found the solution. I tried open .txt file with absolute path to and everything works fine. The only problem now is when I move the project to another directory so I have to compile it again with different path.
Helped:
myfile.open("/Users/macbook/Desktop/Program/list.txt");
instead of
myfile.open("list.txt");

Related

Atom is creating a filename.o in home directory after compiling and running a c++ file using gpp compiler package

I am using atom editor for my C++ related works. But while compiling and running the C++ file using gpp compiler package, a filename.o file is created in the home directory. This is happening each time I am compiling and running my code. Is there any solution to this problem?
Image for the issue
what to do to solve the following issue?
The *.o file is the object file created by the compiler and used by the linker to create the *.exe file. Since it is unnecessary, you can delete it if you already have the *.exe file.
You can delete *.o file instantly with -o /dev/null and after *.exe is created, *.o files are deleted automatically.

Problem with filestream path in C++ when copying files

Brace with me here. I'm trying to make a few simple, but useful C++ console apps that I want to run by starting them straight from cmd (like DiskPart for example). I have a finished product, compiled by gcc through VS Code powershell. The idea is: I have a main.exe and a data file stored in "veco/data.txt" next to the executable. The code works perfectly well when started from its folder where it was compiled, but when I try to copy it to WindowsApps folder (to be able to start it by typing its name in cmd) it just doesn't work...
Yes I did copy the folder too, I've tried to run it from powershell and I get 0 crash msgs...
Here's the part where I interact with the text file:
string s = _pathToExe + "veco\\data.txt";
ifstream is(s);
//i do stuff here
is.close();
where _pathToExe is path to the executable, without the actual name of the executable. ifstream is(s) seems to be the crashing point.
I compiled the code using two commands:
g++ -c <all .cpps>
g++ -o main.exe <all objs>
Can I make this work somehow? Any other suggestions are welcome too.
Note: I've tried copying to other locations, doesn't work there either.

freopen() doesn't work on mac

I am trying to run a code with freopen() on mac os,
but it doesn't print any output in the specified file.
although, it works perfectly on windows.
I am using the X-Code Editor and the input and the output files are in the same path as the cpp file
#include <cstdio>
int main(){
freopen("input.in","r",stdin);
freopen("output.out","w",stdout);
int x;
scanf("%d",&x);
printf("%d\n",x);
return 0;
}
That's because the working directory of your executable is not the directory where you have your .cpp files.
You may point to your files with absolute paths, ex. /Users/omar/Documents/input.in
or change the working directory from xcode settings (see Change the working directory in Xcode )
I personally tried changing the working directory, but it still didn't work...
Later I find out that if you compile your c++ file using Terminal, there would be no trouble at all!
To compile c++ using Terminal, type in these commands:
cd yourFileDirectory
g++ -o yourFileName yourFileName.cpp (this will compile your code)
./yourFileName (and this will run the code successfully!)

Fortran Open from current directory

I am writing a fortran program and I would like to know if it is possible to open a file from the same directory where the program itself is placed.
I am using Ubuntu 12.04 BTW.
For example, if I put the compiled program at the directory "/home/username/foo" I would like the program to open the file "/home/username/foo/bar.txt" and write "Hello!" in it.
My minimal working example is the following:
program main
implicit none
open(unit=20,file="bar.txt",action="write")
WRITE(20,*) "Hello!"
close(20)
end program main
When I compile using gfortran it opens and writes in the file "/home/username/bar.txt" no matter where I put the program file.
On the other hand, when I compile it for windows (using mingw) making a .exe file and execute it in windows it does what I want, it opens the file where the executable file is placed.
[EDIT] I just found out that if I execute the program by double clicking it, it will open the file in the program directory but when I execute it from Terminal it opens at "/home/username/", so maybe is more about the way I send the command from Terminal, currently I am doing it by the following command "/home/username/foo/myprogram".
I too am running Ubuntu 12.04 with gfortran 4.6.3, but I do not experience this. Where ever it is that I place my executable, there is bar.txt after execution.
That said, if you want a file at a specific place, then declare a character string as follows:
character(26) :: filename
filename="/home/username/foo/bar.txt"
and then open the file as
open(unit=20, file=filename)
and you are home free.
EDIT
I just noticed your edit. I imagine that you open terminal and do not cd to the location of the executable, but run the command for execution. That would indeed cause you to always have the file open in whatever folder you are currently in.

Trouble with the directory to read/write file in Eclipse CDT

I use fstream to read/write file in C++.
The structure of my project is:
project
/folder
source.cpp
source.o
source.out
data.txt
Makefile
My code to read file look like this:
ifstream f;
f.open("data.txt"); // because source.out is same directory with data.txt
But it can't read this file if I run it in Eclipse. It only work if I put data.txt file like this:
project
/folder
source.cpp
source.o
source.out
Makefile
data.txt
It's not good. I also don't want to fix my code to:
f.open("folder/data.txt")
when they are in same directory. Because if I run source.out outside eclipse, it doesn't work.
Anybody can give me some solutions?
You can set the "working directory" in the run configuration (arguments tab)