私は次のプログラムを実行しようとしています:どのように入力ファイルと出力ファイルを読み書きする
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ifstream inFile;
ofstream outFile;
double first=1.49, second=2.59, third=3.69, fourth=4.79;
inFile.open("prices.txt");
char response;
if(!inFile.fail())
{
cout << "A file by the name prices.txt exists.\n" << "Do you want to continue and overwrite it\n" << " with the new data (y or n);"; cin >> response;
if(tolower(response) == 'n')
{
cout << "The existing file will not be overwritten." << endl;
return -1;
}
}
outFile.open("prices.txt");
if (inFile.fail())
{
cout << "\n The file does not exist and can not be opened" << endl;
cout << "The file has been successfully opened for output." << endl;
outFile << first << "\n" << second << "\n" << fourth << "\n" << endl;
outFile.close();
exit(1);
cout << "The file has been successfully opened for output. " << endl;
outFile << first << "\n" << second << "\n" << third << "\n" << fourth << endl;
outFile.close();
return 0;
}
}
はまだありません。このプログラムはprices.txtファイルに値を書き込みません。一度プログラムを実行すると、ファイルが存在しないと表示されます。それを2回目に実行すると、ファイルがすでに存在していて、上書きするかどうかが示されます。私のMacを検索しているうちに、どこにでもこのファイルが見つかりません。
私はXcodeでそれを実行すると間違って何をしていますか?友人はVisual Studio 2008で全く同じコードを実行しています。どんな助けもありがとうございます。
ファイルを書き込むための十分な権限がありますか? –
Xcodeは単なるC++開発の理想的なIDEではありません。ハイブリッドC++/Objective-Cは十分ですが、まっすぐなC++プログラムを作成している場合は、クロスプラットフォームのIDE(例えば、[Code :: Blocks'](http://www.codeblocks .org/downloads/26#mac)または['NetBeans'](http://netbeans.org/downloads/index.html)を参照してください)。これはまた、誰もが同じIDEを使用している場合、あなたが持っているもののようなクロスプラットフォーム開発の問題のトラブルシューティングに役立ちます! – darvids0n
re outFile.open( "prices.txt"); if(inFile.fail())//このテストをoutFileにする必要があります – Mark