Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
494 views
in Technique[技术] by (71.8m points)

windows - 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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...