fstream datoteka;
datoteka.open("Informacije.txt", fstream::in | fstream::out | fstream::app);
if(!datoteka.is_open()){
ifstream datoteka("Informacije.txt")
datoteka.open("my_file.txt", fstream::in | fstream::out | fstream::app);
}/*I'm writing IN the file outside of that if statement.
Run Code Online (Sandbox Code Playgroud)
所以它应该做的是创建一个文件,如果它之前没有创建,如果它被创建写入该文件.
你好,所以我想从我的程序中检查文件是否已经存在,如果它已经存在则打开程序并且我可以在其中写入,如果文件未打开(之前没有创建)程序创建它.所以问题是当我创建一个.csv文件,并完成写入,我想检查写入是否真的存在,该文件无法打开.在.txt文件中,一切都是空白的.
小智 7
如果文件名不存在,则创建该文件。否则,fstream::app,如果文件filename已经存在,则将数据附加到文件而不是覆盖它。
int writeOnfile (char* filetext) {
ofstream myfile;
myfile.open ("checkSellExit_file_output.csv", fstream::app);
myfile << filetext;
myfile.close();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
datoteka.open(filename, std::fstream::in | std::fstream::out | std::fstream::app);
工作良好.
#include <fstream>
#include <iostream>
using namespace std;
int main(void)
{
char filename[ ] = "Informacije.txt";
fstream appendFileToWorkWith;
appendFileToWorkWith.open(filename, std::fstream::in | std::fstream::out | std::fstream::app);
// If file does not exist, Create new file
if (!appendFileToWorkWith )
{
cout << "Cannot open file, file does not exist. Creating new file..";
appendFileToWorkWith.open(filename, fstream::in | fstream::out | fstream::trunc);
appendFileToWorkWith <<"\n";
appendFileToWorkWith.close();
}
else
{ // use existing file
cout<<"success "<<filename <<" found. \n";
cout<<"\nAppending writing and working with existing file"<<"\n---\n";
appendFileToWorkWith << "Appending writing and working with existing file"<<"\n---\n";
appendFileToWorkWith.close();
cout<<"\n";
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
22540 次 |
| 最近记录: |