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;
}