I need to create a temp file in a specified folder on MAC. However I am able to create the folder at the correct location but not able to create the file inside. I get an error saying it does not have permissions to create the file although i have passed 777 as file permission while creating the folder. please find my code below. I would like to mention that this problem is only on MAC. On windows it executes as expected. please let me know what the issue is.
wxString curDir = SeeWorkingDir() + wxT("Temp\\"); //Gets the working directory
if(!wxFileName::DirExists(curDir)) //Checks if directory exists
wxFileName::Mkdir( curDir, 0777, wxPATH_MKDIR_FULL ); // creates the directory
wxString jobcopy = wxFileName::CreateTempFileName(curDir); // should create temp file
I have absolutely no idea what your "SeeWorkingDir()" function is returning, but if it's returning the application directory then you shouldn't be writing anything into there.
Why not replace "SeeWorkingDir()" with a path to somewhere on the Macintosh that truly is writable, like the "/tmp/" directory?
You have not explained what your function SeeWorkingDir() does.
Maybe you should use wxFileName::GetTempDir to specify the directory where you store the file. This should be platform independent.
Or, in your call wxFileName::CreateTempFileName(curDir), you could specify the fileTemp parameter to open the file directly.
See the documentation:
wxWidgets 2.8.12 - wxFileName
Related
I have tried to use QFile to open a text file:
I tried
QFile file("serial_deviceIP.txt");
but the file.open() returns false.
However, if I switched to a global address like:
QFile file("C:/Users/shupeng/Documents/qgroundcontrol_peidong_mod/serial_deviceIP.txt");
it works. Why? How can I solve this?
In the first instance, the path to the file cannot be found.
QFile file("serial_deviceIP.txt");
This specifies the file with a relative path, and will only work if serial_deviceIP.txt is in the current working directory, which is likely to be the directory that contains the executable of your program.
QFile file("C:/Users/shupeng/Documents/qgroundcontrol_peidong_mod/serial_deviceIP.txt");
This is referencing an absolute file path, so the file will be found
You can also use Qt's Resource System to bundle the files with your application.
Create a .qrc file in your project and add any file you wish to use/load in your application to it.
Then you can load your file as:
QFile file( ":myfiles/serial_deviceIP.txt" );
See QT Resource System for more information.
What happens is that when we are developing our code we usually keep our project source dir on mind as the reference so we don't give an absolute path, but after building the current directory will change and it will be the build directory, so our application won't find the files without a absolute path.
A possible solution is to add a Resources in our project including our project directory. So just add the following line in the project_file.pro:
RESOURCES += ./
and then use the character : before the file's name when you go to read it, like it:
QFile foo(":bar.txt")
That just work for read it but not for write. So to write is necessary specify an absolute path.
I accidentally made a big blunder:
In my C++ program, I did:
std::string oldFilePath = "/Users/blah/somepath/foo.xml";
std::string newFileName = "foo.xml" //Blunder! Forgot to prefix the new path!
int status = rename(oldFilePath.c_str(), newFileName.c_str());
I forgot to prefix the new path, and just put the filename (without a path) for the new name that the file should be renamed to. As a result the file has vanished from the old path, and I don't know where its gone to!
Where is the file ? Is there a way to recover it ? (Time Machine is disabled for this folder, so I can't do that!)
EDIT: Where would the compiled file generated by Xcode for a C++ application be ?
EDIT: If you're running the program through xcode, it should be in:
~/Library/Developer/Xcode/DerivedData//Build/Products/Debug/
Don't forget the ~ in the above path!
If the operation succeeded (status == 0), the file would be in the current directory of the process when it was run. It is hard to predict where that might be, but $HOME is one plausible candidate (maybe /Users/blah/foo.xml). You should be able to find it, either with the find command or with Spotlight.
I don't use the XCode UI (or other IDEs), in part because I don't like the lack of control over things like 'where the program is put' and 'what is the current directory when I run the program' (and for the rest because I'm a dinosaur). AFAIK, the executable should be in a directory underneath the folder where you created the project. Again, Spotlight or find should be able to help you, at least if you chose a distinctive name for the program. The project directory is another place to look for the foo.xml file too.
In my program, I have a button that I want to open a text file in a relative directory. I'm using QDesktopServices like this:
QDesktopServices::openUrl(QUrl::fromLocalFile("file:///stuff/block_settings.txt"));
When the button is pressed, nothing happens.
The file is in a folder named "stuff" that resides in the same location as my .exe. It is the same directory used for all my other tasks.
What am I doing wrong?
Thanks.
The file is in a folder named "stuff" that resides in the same location as my .exe. It is the same directory used for all my other tasks. What am I doing wrong?
Seems like your full path is an overcomplication. I would suggest to use this intead:
QString QCoreApplication::applicationDirPath() [static]
Returns the directory that contains the application executable.
For example, if you have installed Qt in the C:\Qt directory, and you run the regexp example, this function will return "C:/Qt/examples/tools/regexp".
On Mac OS X this will point to the directory actually containing the executable, which may be inside of an application bundle (if the application is bundled).
Warning: On Linux, this function will try to get the path from the /proc file system. If that fails, it assumes that argv[0] contains the absolute file name of the executable. The function also assumes that the current directory has not been changed by the application.
So, you would be writing this code:
QDesktopServices::openUrl(QString("%1/stuff/block_settings.txt")
.arg(QCoreApplication::applicationDirPath()));
I fixed the issue. Changed to:
QDesktopServices::openUrl(QUrl("file:stuff\\block_settings.txt"));
Not sure how that works because I don't see that configuration on any tutorial anywhere but w/e
I am required to parse a text file in my VS project in mfc in c++. The text file is supposed to be a part of the entire exe product. For that purpose, I placed the text file in my resources folder and set the path in my code as:
char fileName[] = "../myFile.txt";
The problem I'm facing is that VS doesn't find this file in its Resources folder. I added the file in the project file, but that just gave me a corrupt file error. However, the file access works if I provide the absolute path to the file in my code i.e. "C/abc/myFile.txt"
I need the code running on all machines, hence need some method to get VS to read this file using a relative path. Can anybody please provide some assistance? I am a newbie and have tried all that's in my knowledge.
Actually, if it's a resource file it should be copied over to the bin folder, which means your fileName should just be:
char fileName[] = "myFile.txt";
if that doesn't work then, you might need to change the properties of your myFile.txt to ensure it does get copied over with the build process.
Here you can find an answer for your question: http://www.cplusplus.com/forum/general/54255/
I have written a c++ program and deployed it in say c:\my_app, and my executable's path is c:\my_app\my_app.exe. Say, my_app needs many files such as the_file.txt, which is located in c:\my_app\the_file.txt.
In my executable, I open the txt file as, xx.open("the_file.txt");
Moreover, I have associated my program with let's say .myp extension.
When I'm on Desktop, and want to open a file named example.myp, my program can not see the_file.txt. Because, it (somehow) assumes that it's currently working on Desktop.
Is there any easy way to handle this problem by changing shell command for open in HKEY_CLASSES_ROOT? The naive solution would be to change all file open operations with something like %my_app_location/the_file.txt". I don't want to do that.
Always use a full path name to open a file. In other words, don't open "foo.txt", open "c:\bar\foo.txt". To find the install directory of your EXE use GetModuleFileName(), passing NULL for the module handle.
These days you shouldn't add files to c:\my_app....
Instead use the ProgramData Folder and full paths.
Use SHGetSpecialFolderPathA with CSIDL_COMMON_APPDATA to get the ProgramData folder and the create your program directory and add your files.
You should set current directory for your app's folder with SetCurrentDirectory function. After that you can open file by name without full path