In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
The main purpose of this article is to show you how to use the file IO function in CCMG +, which is easy to understand and well-organized. I hope it can help you to solve your doubts. Let me lead you to study and learn how to use the file IO function in CAccord +.
A file (file) is usually a named storage area on a disk or solid state disk. The method of file pointer is mainly used in C, and the idea of "file stream" (that is, non-standard input and output) is mainly used in C++.
I. C
C regards a file as a series of consecutive bytes, each of which can be read separately. C provides two file modes: text mode and binary mode.
1.fopen function std::FILE* fopen (const char* filename, const char* mode)
Open the file filename and return the file stream associated with the file. Mode is used to determine the file access mode.
The parameter filename- associates the filename to which the file stream is associated mode- a string ending with a null character that determines the return value of the file access mode
If successful, a pointer to the object that controls the open file stream is returned, clearing the eof and error bits. When an error occurs, a null pointer is returned.
2.fcloseint fclose (FILE * stream)
The C library function int fclose (FILE * stream) closes the stream stream. Flush all buffers.
Parameters.
Stream is a pointer to a FILE object that specifies the stream to be closed.
Return value
If the stream closes successfully, the method returns zero. If it fails, EOF is returned.
3.FILE structure
The structure FILE for file manipulation is defined in the stdio.h header file of C language. File operations are performed by returning a file pointer (a pointer to the FILE structure) through fopen. You can view the definition of the FILE structure in the stdio.h header file, as follows:
But it seems to be hidden in visual studio. See https://docs.microsoft.com/en-us/cpp/porting/visual-cpp-change-history-2003-2015.
4.fprintf () and fscanf () functions
The file fprintf O function fprintf () and fscanf () function work like the printf () and scanf () classes, except that the former needs to specify the file to be processed with the first argument.
Int fprintf (FILE * stream, const char * format,...); the fprintf () function sends information (parameters) to the file specified by stream (stream) according to the specified format (format). Int fscanf (FILE * stream, const char * format,...) C library function int fscanf (FILE * stream, const char * format,...) Reads formatted input from the stream stream.
Stream-A pointer to a FILE object that identifies the stream.
Format-C string that contains one or more of the following: space characters, non-space characters, and format specifiers.
Success returns the number of successful assignments or writes, and fails or returns a negative number at the end of the file.
II. C++
The header file fstream defines three types to support the file IO: ifstream reads data from a given file, ofstream writes data to a given file, and fstream can read and write to a given file.
Each stream has an associated file pattern (file mode) that indicates how to use the file. Table 8.4 lists the file patterns and what they mean.
Each file stream type defines a default file mode, which is used when no file mode is specified. Files associated with ifstream are opened in in mode by default, files associated with ofstream are opened by default in out mode, and files associated with fstream are opened by default in in and out modes.
By default, when you open an ofstream, the contents of the file are discarded. The way to prevent an ofstream from emptying a given file is to specify either app or in mode at the same time:
/ / in all these statements, filel is truncated ofstream out ("filel"); / / implicitly open the file in output mode and truncate the file ofstream out2 ("file1", ofstream::out); / / implicitly truncate the file ofstream out3 ("file1", ofstream::out | ofstream::trunc); / / explicitly specify the app mode ofstream app ("file2", ofstream::app) to preserve the contents of the file. / implied as the output mode ofstream app2 ("fi1e2", ofstream::out | ofstream::app); third, the sample program # include#include#include#include#include#includeusing namespace std;const int MAX_NUM = 100 leading int a [Max _ NUM]; int n = 2 char* argv main (int argc, char* argv []) {FILE* file4 = fopen ("d.txt", "w"); if (! file4) {printf ("file4 open error!\ n") Return-1;} / / fprintf (file4, "name age sex position\ n"); int age, sex; char name [50], position [50]; printf ("please input 2 date: name age sex position:\ n"); while (while -) {scanf ("% s% d% d% s", & name, & age, & sex, & position) Fprintf (file4, "% s% d% d% s\ n", name, age, sex, position);} fclose (file4); FILE* file5 = fopen ("d.txt", "r"); if (! file5) {printf ("file5 open error!"); return-1;} int m = 2, ans, ans1 While (Mmura -) {fscanf (file5, "% s% d% d% s", & name, & age, & sex, & position); printf ("% s% d% d% s\ n", name, age, sex, position);} fclose (file5); fstream file1 ("d.txt"); if (! file1) {cout tmp) {str.push_back (tmp) Cout
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.