The standard input (output) and file input (output) functions are summarized!

Source: Internet
Author: User

Any file must be opened and closed before and after use.
Istream, ostream, Cin. Get (), Cin. Getline (), Cin. Read (), cout. Write ()

Fopen, fgetc (), fputc (), GETC (), putc (), fgets (), fputs ()

Getchar (), putchar (), gets (), puts ()
++ ++ ++
Char infile [1, 255], outputfile [2, 255];
File * INF, * outf;
Strcpy (filename, m_stryuan );
Strcpy (outputfile, m_stryasuo );

INF = fopen (infile, "rb ");
Outf = fopen (outputfile, "WB ");

Int ch;
While (CH = fgetc (FP ))! = EOF)
{
Cal [CH] ++;
In_num ++;
}
Fclose (FP );

Fputc (T. leafnum, outf );

fseek (outf, 3 * t. leafnum, 0);
fputc (B. count, outf);
fclose (FP );
================================================= ========================================================== ========================================< br> ofstream outquanfile (" .txt ", IOS: Out);
for (INT J1 = 0; J1 {< br> outquanfile <"u12 [" }< br> outquanfile <"/N";

for (INT J3 = 0; J3 {< br> outquanfile <"V [" }< br> outquanfile <"/N";
outquanfile. close ();
================================================= ========================================================== ===========================< br> ifstream gettrainingdata ("training sample .txt ", IOS: In);
for (INT m = 0; m {< br> for (INT I = 0; I {< br> gettrainingdata> study_data [M]. input [I]; // obtain input data
}< br> for (Int J = 0; j {< br> gettrainingdata> study_data [M]. teach [J]; // get output data
}< BR >}< br> gettrainingdata. close ();

++ ++ ++
In some cases, we need to read the input stream as an uninterpreted byte sequence instead of a specific data type (such as char, Int, string) sequence.
The istream member function get () reads one byte at a time, Getline () reads one byte at a time, or ends with a line break or a termination character defined by a user.
1. Get (char & Ch): extract a character from the input stream, including white space characters, and store it in Ch. (Press enter and space calculation)
Char ch; while (CIN. Get (CH) {...}; cout. Put (CH );
2. Get (): enter a character from the input stream. It returns the character value instead of the istream object to be applied. The return type is int, not Char.
3. get (char * sink, streamsize size, char delimiter = '/N') // (character array, maximum number, Terminator). Before the second get, you must remove delimiter, cin. ignore (); delimiter will not be placed in the array, but will be reserved as the next character of istream.
Cin. Get (line, max_line); get_count = cin. gcount (); // actual read count cin. Ignore ();

* ** Cin. Getline (char * sink, streamsize size, char delimiter = '/N'), which discards delimiter, Cin. Getline (...)

Cin. Read (char * ADDR, streamsize size); // extract size consecutive bytes from the input stream and put them into ADDR;
Cout. Write (const char * sink, streamsize lenth); // (number of characters to be displayed)

The Getline of the standard library can be input to a string object.
Getline (istream & is, string STR, char delimiter );

----------------------------------------------------------------------
Ofstream OUTFILE ("Copy. Out", ios_base: Out); // ios_base: app
Before trying to read and write a file, first determine whether it has been successfully opened
If (! OUTFILE) {cerr <"cannot open copy. Out for output/N"; exit (-1 );}

Ifstream infile ("inputf.txt", ios_base: In );
If (! Infile) {cerr <"cannot open input file/N "};

Infile <name [20] <score;
OUTFILE <name [20] <"" <score;
----------------------------------------------------------------------
File * fopen (const char * fname, const char * mode); // (file name, open mode)
"R" open a text file for reading
"W" creates a text file for writing
"A" appended to a text file
"Rb" open a binary file for reading
"WB" creates a binary file for writing

1) read/write characters
Int fgetc (File * stream); // returns the next character of the stream, shaping
Int fputc (int ch, file * stream); // output the string ch to the output stream.

While (CH = fgetc (INF ))! = EOF) {cout <ch <Endl; fputc (CH, outf );}

(GETC is the same as fgetc, And put is the same as fputc)

2) read and write strings
Char * fgets (char * STR, int num, file * stream); // (string, number, file)
Int fputs (const char * STR, file * stream); // (string, written file)

3)
Int getchar (void); // get from stdin (standard input) and return the next character
Int putchar (INT ch); // writes ch to stdout (standard output ).

4)
Char * gets (char * Str); // read characters from stdin (standard input) and load them into STR (string) until a new line (/N) is encountered or the EOF
Int puts (char * Str); // writes STR (string) to stdout (standard output ).

 

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.