#include <iostream> #include <fstream> #include <cstdlib> int main() { std::fstream f1("/tmp/test"); if (!f1) { std::cerr << "f1 failed\n"; } else { std::cerr << "f1 success\n"; } FILE *f2 = fopen("/tmp/test", "w+"); if (!f2) { std::cerr << "f2 failed\n"; } else { std::cerr << "f2 success\n"; } }
Creating a file in / tmp / does not work for me with fstream, but with fopen. What could be the problem? (I get f1 unsuccessfully and f2 success when / tmp / test does not exist yet)
You have to say that you open the file for output, like this
std::fstream fs("/tmp/test", std::ios::out);
Or use a stream instead of fstream, which by default opens a file for output:
std::ofstream fs("/tmp/test");
Your first method call does not automatically create the file: see fstream .
If you want your first method call to create a file, use:
std::fstream f1("/tmp/test", fstream::out);
, fstream, ,
std::fstream f1("/tmp/test", std::fstream::in | std::fstream::out);
,
Source: https://habr.com/ru/post/1724686/More articles:Keyboard input in Tcl - tclhttps://translate.googleusercontent.com/translate_c?depth=1&pto=aue&rurl=translate.google.com&sl=ru&sp=nmt4&tl=en&u=https://fooobar.com/questions/1724682/help-to-create-a-generic-class-to-avoid-code-duplication&usg=ALkJrhio9zBeHoE66Z53lBbocNol44u9rgFocus on style sheets and browser compatibility - htmlBlackBerry - disable the display of the default menu on the screen - user-interfaceWhat modules should I consider in Python if I want to use CGI sessions? - pythontfs2010 local build - build-processMultiple selectors with jQuery - jqueryHow to create a standalone executable for each form for a C # solution in VS 2008 with multiple projects with multiple forms in the same assembly? - c #C # how to call a method in a database base? - c #JasperReports with resolution other than 72dpi - javaAll Articles