c++ - If file exist, work with it, if no, create it -


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 file outside of if statement. 

so should create file if not created before, , if created write file.

hello there, wanted program check if file exists, sothe program open if , can write in it, if file not opened(have not been created before) program create it. problem when create .csv file, , finish writing , wanted check if written there, file cannot opened. in .txt file, blank.

datoteka.open(filename, std::fstream::in | std::fstream::out | std::fstream::app); works fine.

#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 not exist, create new file       if (!appendfiletoworkwith )        {         cout << "cannot open file, file 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 , working existing file"<<"\n---\n";           appendfiletoworkwith << "appending writing , working existing file"<<"\n---\n";          appendfiletoworkwith.close();          cout<<"\n";      }        return 0; } 

Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

thorough guide for profiling racket code -