C + + full line read. txt file __c++

Source: Internet
Author: User

Read the file Lhc.txt as follows:

Zhongguo 1 2 3 4 Chinese


#include <iostream> #include <sstream>
#include <string>
#include <fstream>

int main ()
{
std::string filename = "Lhc.txt";
Std::ifstream infile (Filename.c_str ());
Std::string str1, str2;
int a[4];

Std::string linestr;
Std::stringstream SS;
while (Std::getline (INFILE,LINESTR))
{
SS << Linestr;
SS >> Str1;
for (int i=0; i<4; i++)
SS >> A[i];

SS >> str2;
}
Std::cout << "str1 is" << str1 << "str2 are" << str2 << Std::endl;

for (int i=0; i<4; i++)
Std::cout << "The" << I << "num is:" << a[i] << Std::endl;
return 1;

}

Attention is: Ss.clear (); If you want to implement multiple types of conversions by using the same StringStream object, be aware that the clear () member function must be called after each conversion.

-----------------------------------------------------------------------------------

Write data to a. txt file

#include <stdio.h>  /This is C?

Int Main () {

        std::cout << "Hello, World" << Std::endl;
        FILE *f_buffer;
        F_buffer = fopen ("Zz_lhc.txt", "w");//This'll create the file, if it does not exit.< br>         fprintf (F_buffer, "%s  ", "Hello");
        Double A = 2.3;
        fprintf (F_buffer, "%f\n", a);  //\ n  will change to another line.
        fprintf (F_buffer, "%f\n", a);
        fclose (F_buffer);

        return 1;
}

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.