Reading a line of characters in C ++

Source: Internet
Author: User

Header file <fstream>. It is best not to use fstream. h. when I was writing a program, I found that the latter would produce unsatisfactory output. The specific reason is unclear, probably due to the C ++ standard problem.

 

Solution1:

E. G.1

Char STR [90];
Infile> STR;
OUTFILE <STR <Endl;
Infile> STR;
OUTFILE <STR <Endl;
Infile> STR;
OUTFILE <STR <Endl;

Infile format:

* *************** Kusdafi ********************

* ************* Hlkafshd *********************

* *********** Hkalsf ** 8

Output file OUTFILE format: (keep consistent with infile)

* *************** Kusdafi ********************

* ************* Hlkafshd *********************

* *********** Hkalsf ** 8

Program Description: Str [90] can be read into a line of characters through infile> STR, as long as the number of characters in this line is smaller than 89 (the ending character '/0' is to be stored in the last position), you can read it and use OUTFILE <STR <Endl; output in the inverted output file.

Disadvantage: When this line contains spaces, infile> STR can only end when spaces are read.

Program effect when spaces exist: Input File Format

1234 5678

9001 90002

Corresponding output file format:

1234

5678

9001

 

Solution2:

Advantage: The Getline statement can be used to read a row without spaces.

E.g.

Char starsingnaltemp1 [81];
Char starsingnaltemp2 [81];
Char starsingnaltemp3 [81];

Infile. Getline (starsingnaltemp1, 81, '/N ');

Infile. Getline (starsingnaltemp2, 81, '/N ');
Infile. Getline (starsingnaltemp3, 81, '/N ');

OUTFILE. Write (starsingnaltemp1, 80 );

OUTFILE <Endl;
OUTFILE. Write (starsingnaltemp2, 80 );

OUTFILE <Endl;
OUTFILE. Write (starsingnaltemp3, 80 );
OUTFILE <Endl;

OUTFILE <"0 1 0" <Endl;

Explanation:

Infile. Getline (starsingnaltemp1, 81, '/N ');

Read a line of characters. When '/N' (carriage return) is encountered, the read operation is completed. A maximum of 80 characters (excluding strings)

Terminator '/0'), and 81st-bit save Terminator. Here, this sentence is equivalent to infile. Getline (starsingnaltemp1, 81 );

If it is written as infile. Getline (starsingnaltemp1, 80), and a row in the file is exactly 80 characters, the function reports an error, with one less bucket.

OUTFILE. Write (starsingnaltemp1, 80 );

Write the 80 characters stored in the array into the output file. Note that the 80 characters do not contain the count of the terminator '/0'.

The 80-bit ending character should be written as OUTFILE. Write (starsingnaltemp1, 79 );

When the red statement OUTFILE <Endl; is not written, the output results are as follows:

Input file:

1234 5678

1256 7890

1122

Output file: all data is placed on one row.

1234 million and 5678 million mouths ...... 1256 million or 7890 million mouths ...... 1122 ...... 0 1 0

When the red statement OUTFILE <Endl; is written, the input and output results are as follows:

Input file:

1234 5678

1256 7890

1122

Output file:

1234 5678

1256 7890

1122

0 1 0

Related Article

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.