C ++ input/output stream and file operations

Source: Internet
Author: User

1. c ++ functions that retain a single character in the input and output of C

(1) putchar (c) -- character output

Routine:

# Include <iostream>

Using namespace STD;

Int main ()

{

Char A, B, C;

A = 'B'; B = 'O'; C = 'y ';

Putchar (a); putchar (B); putchar (c); putchar ('/N ');

Putchar (66); putchar (79); putchar (89); putchar (10); // 10 is the ASCII code for line feed.

Return 0;

}

(2) getchar () -- Character Input Function

Routine:

# Include <iostream>

Using namespace STD;

Int main ()

{

Char C;

C = getchar ();

Putchar (C + 32); // converts it to lowercase letters. The uppercase and lowercase letters include [/] ^-and 6 characters. Therefore, 26 and 32 are not added. 'A' = 65

Putchar ('/N ');

Return 0;

}

Cout can also be output: cout <(C = getchar () + 32 );

2. Input and Output of scanf () and printf () Functions

Scanf (format control, output table column );

Printf (format control, output table column );

Routine:

# Include <iostream>

Using namespace STD;

Int main ()

{

Int;

Float B;

Char C;

Scanf ("% d % C % F", & A, & C, & B );

Printf ("A = % d, B = % F, c = % C/N", A, B, C );

Return 0;

}

3. Standard input and output streams CIN and cout

Including header files # include <iostream>

The Controller of the input and output streams must be added to the header file # include <iomanip>

Cout <"Dec:" <dec <A <Endl; // returns an integer in decimal format.

Cout. Put (97); // display the character -- use the cout member function put (c)

Cin member function get ()

(1) Without Parameters

Char c = cin. Get (); // use the member function get () of CIN to read a character from the keyboard to C.

(2)

Cin. Get (CH); // receives a character from the input stream and assigns it to the character variable ch

(3) with three parameters

Cin. Get (character array, number of characters N, termination character)

Routine:

# Include <iostream>

Using namespace STD;

Int main ()

{

Char ch [20];

Cout <"in put a string:" <Endl;

Cin. get (CH, 10, '/N');/* gets the first 10-1 = 9 Characters of the input stream and assigns them to the first 9 elements of the CH array, the linefeed is the terminator of the input stream */

Cout <ch <Endl;

Return 0;

}

For example, input: 1 2 34567890 // press the line feed key to terminate the stream input.

The output is: 1 2 34567 // read the first nine data records into the CH array.

Cin member function Getline () -- reads a line of characters from the input stream

Cin. Getline (character array (or character pointer), number of characters N, Terminator character)

Routine:

# Include <iostream>

Using namespace STD;

Int main ()

{

Char ch [20];

Cout <"input a string:" <Endl;

Cin> CH;

Cout <"the string read with CIN is:" <ch <Endl;

Cin. Getline (CH, 20, '/'); // read 19 characters to the CH array or end '/'

Cout <"the second part is:" <ch <Endl;

Cin. Getline (CH, 20); // read 19 characters to the CH array or when '/N' ends,'/'is still read as a character

Cout <"the third part is:" <ch <Endl;

Return 0;

}

Other member functions of the istream class, such as EOF (), read data in the input stream. If it reaches the end (when a file Terminator is encountered), EOF () returns true, otherwise it is false.

Routine:

# Include <iostream>

Using namespace STD;

Int main ()

{

Char C;

While (! Cin. EOF () // if no file Terminator exists

If (C = Cin, get ())! = '') // Check whether the read character is a space character

Cout. Put (C );

Return 0;

}

4. Open and Close Disk Files

Open

Ofstream OUTFILE;

If (OUTFILE. Open ("f1.data", IOS: APP) = 0) // open

Cout <"failed to open! ";

........................

OUTFILE. Close (); // close

5. File writing

# Include <fstream>
# Include <iostream>

# Include <string> // it must not be missing; otherwise, error c2679 is reported.
Using namespace STD;

Int main ()

{
String STR;
Ofstream out ("d.txt", IOS: Out); // equivalent to ofstream out ("d.txt ")

If (! Out) // If opening fails, out returns 0

{

Cerr <"failed to open! "<Endl;

Exit (0 );

}
STR = "the moonlight in front of the bed/N is suspected to be frost on the ground/n raised his head to the moon/n bowed his head to his hometown/N ";
Out <STR <Endl;
Return 0;
}

6. Read files to the console

# Include <fstream>
# Include <iostream>
# Include <string>
Using namespace STD;
Int main ()

{
Ifstream infile ("D: // new // d.txt", IOS: In); // define the object of the input file. Open the source file d.txt in the input format. The second parameter can be used.
If (! Infile)
{
Cerr <"failed to open! "<Endl;
Exit (1 );
}
For (string STR; Getline (infile, STR);) // open row by row and display row by row.
Cout <STR <"/N ";
Infile. Close ();
Return 0;
}

Or set for (string STR; Getline (in, STR );)
Cout <STR <"/N ";

Change the two sentences to string STR;
While (Getline (in, STR ))
Cout <STR <"/N ";

7. File Replication

# Include <fstream>
# Include <iostream>
# Include <string>
Using namespace STD;
Int main ()
{
Ifstream in ("d.txt ");
If (! In)
{
Cerr <"failed to open the source file! "<Endl;
Exit (1 );
}
Ofstream out ("B .txt ");
If (! Out)
{
Cerr <"failed to open the target file! "<Endl;
Exit (1 );
}
For (string STR; Getline (in, STR );)
Out <STR <Endl; // note that it is out
Cout <"file copied successfully! "<Endl;

In. Close ();

Out. Close ();
Return 0;
}

 
8. Customize the void display (char * filename) function for reading a file)

# Include <fstream>
# Include <iostream>
# Include <string>
Using namespace STD;
Void display (char * filename)
{
Ifstream infile (filename, IOS: In );
If (! Infile)
{
Cerr <"failed to open! "<Endl;
Exit (1 );
}
Char ch;
While (infile. Get (CH ))
Cout. Put (CH );
Cout <Endl;
Infile. Close ();
}

Int main ()
{
Display ("d.txt ");
Return 0;
}

 

9. Comprehensive application of file read/write

# Include <fstream>
# Include <iostream>
Using namespace STD;

// Read from the keyboard and save it to the file d.txt
Void save_to_file ()
{
Ofstream ofile ("d.txt ");
If (! Ofile)
{
Cerr <"Opening failure d.txt! "<Endl;
Exit (1 );
}
Char C [80];
Cin. Getline (C, 80); // one line of characters is read from the keyboard
For (INT I = 0; C [I]! = 0; I ++) // process characters one by one, knowing that '/N' is over
If (C [I]> = 65 & C [I] <= 90 | C [I]> = 97 & C [I] <= 122)
{
Ofile. Put (C [I]); // Save the token to the local file d.txt
Cout <C [I];
}
Cout <Endl;
Ofile. Close ();
}

// Convert the letters in "d.txt" into uppercase letters and store them in "out.txt"
Void get_from_file ()
{
Char ch;
Ifstream infile ("d.txt ");
If (! Infile)
{
Cerr <"Opening failure d.txt! "<Endl;
Exit (1 );
}
Ofstream OUTFILE ("out.txt ");
If (! Infile)
{
Cerr <"Open failure out.txt! "<Endl;
Exit (1 );
}

While (infile. Get (CH) // After the characters are read one by one, run the following statement:
{
If (CH> = 97 & Ch <= 122)
Ch = ch-32;
OUTFILE. Put (CH );
Cout <ch;
}
Cout <Endl;
Infile. Close ();
OUTFILE. Close ();
}

Int main ()
{
Save_to_file ();
Get_from_file ();
Return 0;
}

======================================== C/C ++ character or character array reading Function conclusion =

1. Cin
1. Cin. Get ()
2. Cin. Getline ()
3. Getline ()
4. Gets ()
5. getchar ()

1. Cin>

Usage 1: the most basic and common usage. Enter a number:

# Include <iostream>
Using namespace STD;
Main ()
{
Int A, B;
Cin> A> B;
Cout <a + B <Endl;
}

Input: 2 [Press enter] 3 [Press enter]
Output: 5

Usage 2: accept a string. When "space", "tab", and "Press ENTER" are all completed

# Include <iostream>
Using namespace STD;
Main ()
{
Char A [20];
Cin>;
Cout <A <Endl;
}

Input: jkljkljkl
Output: jkljkljkl

Input: jkljkl // end with Space
Output: jkljkl

2. Cin. Get ()

Usage 1: cin. Get (character variable name) can be used to receive characters

# Include <iostream>
Using namespace STD;
Main ()
{
Char ch;
Ch = cin. Get (); // or CIN. Get (CH );
Cout <ch <Endl;
}

Input: jljkljkl
Output: J

Usage 2: cin. Get (character array name, number of characters received) is used to receive a line of string, can receive Spaces

# Include <iostream>
Using namespace STD;
Main ()
{
Char A [20];
Cin. Get (A, 20 );
Cout <A <Endl;
}

Input: jkl
Output: jkl

Input: abcdeabcdeabcdeabcdeabcde (25 characters)
Output: abcdeabcdeabcdeabcd (receiving 19 characters + 1 '/0 ')

3. Cin. Getline () // receives a string and can receive spaces and Output

Getline replaces the linefeed with a null character.

# Include <iostream>
Using namespace STD;
Main ()
{
Char M [20];
Cin. Getline (M, 5 );
Cout <m <Endl;
}

Input: jkljkljkl
Output: jklj

Accept 5 characters to m, and the last one is '/0'. Therefore, only 4 characters of output are displayed;

If you change 5 to 20:
Input: jkljkljkl
Output: jkljkljkl

Input: jklf fjlsjf fjsdklf
Output: jklf fjlsjf fjsdklf

// Extension:
// Cin. Getline () actually has three parameters: cin. Getline (accept the string to see the M, accept the number of 5, end character)
// When the third parameter is omitted, the default value is '/0'
// If you change cin. Getline () in the example to CIN. Getline (M, 5, 'A'), and output jklj when jlkjkljkl is input, output JK when jkaljkljkl is input.

When using multi-dimensional arrays, you can also use cin. Getline (M [I], 20) or the like:

# Include <iostream>
# Include <string>
Using namespace STD;

Main ()
{
Char M [3] [20];
For (INT I = 0; I <3; I ++)
{
For cout <"/N, enter the" <I + 1 <"string:" <Endl;
Cin. Getline (M [I], 20 );
}

Cout <Endl;
For (Int J = 0; j <3; j ++)
Cout <"output M [" <j <"] value:" <m [J] <Endl;

}

Enter 1st strings:
Kskr1

Enter 2nd strings:
Kskr2

Enter 3rd strings:
Kskr3

Output M [0] value: kskr1
Output M [1] value: kskr2
Output M [2] value: kskr3

4. Getline () // receives a string. It can receive spaces and output the string. It must contain "# include <string>"

# Include <iostream>
# Include <string>
Using namespace STD;
Main ()
{
String STR;
Getline (CIN, STR );
Cout <STR <Endl;
}

Input: jkljkljkl
Output: jkljkljkl

Input: jkl jfksldfj jklsjfl
Output: jkl jfksldfj jklsjfl

Similar to CIN. Getline (), but cin. Getline () belongs to the istream stream, while Getline () belongs to the string stream. It is a different function.

5. Gets () // receives a string, receives spaces, and outputs the string. It must contain "# include <string>"

# Include <iostream>
# Include <string>
Using namespace STD;
Main ()
{
Char M [20];
Gets (m); // cannot be written as M = gets ();
Cout <m <Endl;
}

Input: jkljkljkl
Output: jkljkljkl

Input: jkl
Output: jkl

Similar to an example in CIN. Getline (), gets () can also be used in multi-dimensional arrays:

# Include <iostream>
# Include <string>
Using namespace STD;

Main ()
{
Char M [3] [20];
For (INT I = 0; I <3; I ++)
{
For cout <"/N, enter the" <I + 1 <"string:" <Endl;
Gets (M [I]);
}

Cout <Endl;
For (Int J = 0; j <3; j ++)
Cout <"output M [" <j <"] value:" <m [J] <Endl;

}

Enter 1st strings:
Kskr1

Enter 2nd strings:
Kskr2

Enter 3rd strings:
Kskr3

Output M [0] value: kskr1
Output M [1] value: kskr2
Output M [2] value: kskr3

The usage of gets () and CIN. Getline () is similar, except that CIN. Getline () has one more parameter;

This section describes the examples of kskr1, kskr2, and kskr3 in this article. This example also applies to CIN> because there is no space entered here. If spaces are entered, for example, "Ks KR jkl [Press enter]", CIN will have received three strings: "Ks, KR, jkl "; for example, if "kskr 1 [Press enter] kskr 2 [Press enter]", then "kskr, 1, kskr" is received. This is not the result we want! Cin. Getline () and gets () do not generate this error because they can receive spaces;

6. getchar () // accepts one character and must contain "# include <string>"

# Include <iostream>
# Include <string>
Using namespace STD;
Main ()
{
Char ch;
Ch = getchar (); // cannot be written as getchar (CH );
Cout <ch <Endl;
}

Input: jkljkljkl
Output: J

// Getchar () is a C-language function, which can be compatible with C ++, but is not required or used as little as possible;

Summary:

1. Read a row

Gets (char a []); // accepts a row and ends with a carriage return.

Gets (M [I]) // It can be used in multi-dimensional arrays.

Cin. get (char a [], int 5, '/N') // read the first five characters of character array A and end with a line break. For example: A bc12ds, it will be read into 4 characters a BC, plus a '/0 ';

// Generally, the third parameter is not written. The default value is '/0'

Eg:

# Include <iostream>
# Include <string>
Using namespace STD;
Main ()
{
String STR;
Getline (CIN, STR );
Cout <STR <Endl;
}

2. Read a character

Getchar () // accepts one character and must contain "# include <string>"

Char CH = getchar ();

Cin. Get (char C) or CIN. Get ()

Char CH = cin. Get (); or CIN. Get (CH)

Getline () is similar to CIN. Getline (), but cin. Getline () belongs to the istream stream, while Getline () belongs to the string stream.

Cin. getline (char a [], int 5, '/N') // read the first five characters of character array A. The line break ends. For example, a bc12ds, it will be read into 4 characters a BC, plus a '/0 ';

// Generally, the third parameter is not written. The default value is '/0'

Cin. Getline (M [I], 20) // It can be used in multi-dimensional arrays.

Getline () // receives a string, receives spaces, and outputs the string, and must contain "# include <string>"

 

This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/Deutschester/archive/2009/06/24/4295723.aspx

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.