The CI/O subset of the C++ standard library implements C-style stream input/output operations. The <cstdio> header file provides general file support and provides functions with narrow and multibyte character input/output capabilities, while the <cwchar> header file provides functions with wide character input/output capabilities.
Operations on files
Delete Files
std::remove
int remove( const char* fname ); |
Delete fname
the file identified by the string pointed to.
The behavior of this function is implementation-defined if the file is open for the current process or another process. Specifically, POSIX systems unlink filenames until the last running process closes the file, and do not reclaim file system space even if this is the last hard link to the file. Windows does not allow deletion of this file.
parameter
fname | - | Pointer to a null-terminated string containing the path identifying the file to be deleted |
return value
0 on success, non-zero on error.
Notice
POSIX specifies many additional details about the behavior of this function.
The standard library also defines the function template std::remove that receives a pair of iterators and values. This overload is one of the standard algorithms.
Call example
#include <iostream>
#include <fstream>
#include <cstdio>
int main()
{
bool ok = static_cast<bool>(std::ofstream("file1.txt").put('a')); // 创建文件
if (!ok)
{
std::perror("Error creating file1.txt");
return 1;
}
std::cout << std::ifstream("file1.txt").rdbuf() << '\n'; // 打印格式
std::remove("file1.txt"); // 删除文件
bool failed = !std::ifstream("file1.txt");
if (failed)
{
std::perror("Error opening deleted file");
return 1;
}
return 0;
}
output
Rename file
std::rename
int rename( const char *old_filename, const char *new_filename ); |
Change the file name of the file. The file is identified by old_filename
the string pointed to by , and new_filename
the new file name is identified by the string pointed by .
If new_filename
present, the behavior is implementation-defined.
parameter
old_filename | - | Pointer to a null-terminated string containing the path identifying the file to be renamed |
new_filename | - | Pointer to a null-terminated string containing the new path to the file |
return value
0 on success, non-zero on error.
annotation
POSIX specifies many additional details on the semantics of this function, which are copied into C++ by std::filesystem::rename.
Call example
#include <iostream>
#include <fstream>
#include <cstdio>
int main()
{
bool ok{std::ofstream("from.txt").put('a')}; // 创建并写入文件
if (!ok)
{
std::perror("Error creating from.txt");
return 1;
}
if (std::rename("from.txt", "to.txt"))
{
std::perror("Error renaming");
return 1;
}
std::cout << std::ifstream("to.txt").rdbuf() << std::endl; // 打印文件
return 0;
}
Create and open a temporary, automatically removed file
std::tmpfile
std::FILE* tmpfile(); |
Create and open temporary files.
The file is opened as a binary file, in update mode (as for std::fopen in "wb+"
mode). The file name of this file is guaranteed to be unique in the file system. At least TMP_MAX files can be opened during the lifetime of the program (this limit may be shared with tmpnam, and may be further limited by FOPEN_MAX).
If the program closes the file, for example by executing std::fclose, the file is automatically deleted.
std::tmpfile
If the program exits normally (by calling std::exit, returning from main, etc.), all files opened by calling will also be automatically deleted .
If the program exits abnormally, whether these temporary files are deleted is implementation-defined.
parameter
(none)
return value
The associated file stream, or NULL if an error occurred.
Notice
On some implementations (such as Linux), this function does create, open, and immediately delete the file from the file system: as long as the file open descriptor of the deleted file is managed by the program, the file will exist, but as long as it is deleted, Its name will not appear in any directory, so no other process can open it. Once the file descriptor is closed, or once the program terminates (normally or abnormally), the space occupied by the file will be reclaimed by the file system.
Elevated permissions are required on some implementations (such as Windows) because this function may create temporary files in system directories.
Call example
#include <iostream>
#include <cstdio>
#include <cstdlib>
int main()
{
std::FILE* tmpf = std::tmpfile();
std::fputs("Hello, world", tmpf);
std::rewind(tmpf);
char buf[6];
std::fgets(buf, sizeof buf, tmpf);
std::cout << buf << std::endl;
std::cout << "_ptr: " << tmpf->_ptr << std::endl;
std::cout << "_cnt: " << tmpf->_cnt << std::endl;
std::cout << "_base: " << tmpf->_base << std::endl;
std::cout << "_flag: " << tmpf->_flag << std::endl;
std::cout << "_file: " << tmpf->_file << std::endl;
std::cout << "_charbuf: " << tmpf->_charbuf << std::endl;
std::cout << "_bufsiz: " << tmpf->_bufsiz << std::endl;
std::cout << "_tmpfname: " << tmpf->_tmpfname << std::endl;
return 0;
}
output
Returns a unique file name
std::tmpnam
char* tmpnam( char* filename ); |
Creates a unique filename that does not name a currently existing file and stores it in filename
the string pointed to by . The function is sufficient to generate up to TMP_MAX unique file names, but some or all of them may already be in use, making them unsuitable as return values.
std::tmpnam
Modifies static state and is not required to be thread-safe.
parameter
filename | - | Pointer to a character array large enough to hold at least L_tmpnam bytes, to be used as the result buffer. If a null pointer is passed, a pointer to the internal static buffer is returned. |
return value
1) If filename
is not NULL filename
. Otherwise, a pointer to an internal static buffer. If a suitable file name cannot be generated, a null pointer is returned. If a suitable file name cannot be generated, NULL is returned.
Notice
Although std::tmpnam
the resulting file name is difficult to guess, it may be std::tmpnam
the name of a file created by another process between the time of return and the time this function attempts to create the file using the returned name. The standard function std::tmpfile and the POSIX function mkstemp do not have this problem (creating a unique directory using only the C standard library is still required std::tmpnam
).
The POSIX system additionally defines a similarly named function tempnam() , which provides directory selection (the default is the optionally defined macro P_tmpdir ).
Call example
#include <iostream>
#include <cstdio>
#include <string>
int main()
{
std::string name1 = std::tmpnam(nullptr);
std::cout << "temporary file name: " << name1 << std::endl;
char name2[L_tmpnam];
if (std::tmpnam(name2))
{
std::cout << "temporary file name: " << name2 << std::endl;
}
return 0;
}