简单的 C++写入文件在 Mac OS X 应用程序中不起作用 - 为什么? (为什么是我?)
代码非常简单:
#include <iostream>
#include <fstream>
#include <string>
int main(int argc, char* argv[] )
{
std::ofstream theStream;
theStream.open("trash.txt");
theStream << "some words" << std::endl;
theStream.close();
}
如果我从命令行运行它,那么我会在同一目录中获得预期的文件。如果将可执行文件的内容打包到 MacOS .app 中,则不会在任何地方写入任何文件。 (或者它可能只是被立即删除?)
这是我用来将可执行文件放入 .app 的简单脚本。也许这就是我出错的地方。
#!/bin/bash
appName=MyApp
if [ $1 ]
then
appName=$1
else
echo "usage: convertToApp executableFile"
exit
fi
if [ -e "$appName" ]
then
mkdir $appName.app
mkdir $appName.app/Contents
mkdir $appName.app/Contents/MacOS
mkdir $appName.app/Contents/Resources
cp $appName $appName.app/Contents/MacOS/$appName
echo -n 'APPL????' > $appName.app/Contents/PkgInfo
else
echo "specified file does not exist"
fi
知道为什么我看不到我想看的文件吗?
The code is simple enough:
#include <iostream>
#include <fstream>
#include <string>
int main(int argc, char* argv[] )
{
std::ofstream theStream;
theStream.open("trash.txt");
theStream << "some words" << std::endl;
theStream.close();
}
If I run it from the command line then I get the expected file in the same directory. If a package the contents of the executable within a MacOS .app, then no file is written anywhere. (Or perhaps it is just promptly erased?)
Here's the simple script I'm using to place the executable into the .app. Perhaps that is where I'm going wrong.
#!/bin/bash
appName=MyApp
if [ $1 ]
then
appName=$1
else
echo "usage: convertToApp executableFile"
exit
fi
if [ -e "$appName" ]
then
mkdir $appName.app
mkdir $appName.app/Contents
mkdir $appName.app/Contents/MacOS
mkdir $appName.app/Contents/Resources
cp $appName $appName.app/Contents/MacOS/$appName
echo -n 'APPL????' > $appName.app/Contents/PkgInfo
else
echo "specified file does not exist"
fi
Any idea why I can't see the file I want to see?
尝试使用完整路径或更改当前工作目录,而不是“trash.txt”。
Instead of "trash.txt", try to use the full path or change the current working directory.