简体   繁体   中英

Create a file C++ (cross-platform solution for Unix and Windows)

Is there a way from C++ code to use (fstream, FILE, etc.) to create a file on both Unix and Windows?

If not what you can do in order to have your code run on both Unix and Windows?

ofstream out(path);

or

FILE *fp = fopen(path, "w");

will create the file path if it does not exist.

fopen("foo.txt", "w"); // write-only
fopen("foo.txt", "w+"); // write+read
fstream filestr;
filestr.open ("foo.txt", fstream::out | fstream::trunc); // write-only
filestr.open ("foo.txt", fstream::in | fstream::out | fstream::trunc); // write+read

All of these will create the file, or truncate it if it already exists.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM