#include<iostrea>
#include<fstream>
int main()
{
ofstream fout;// 创建一个ofstream对象
fout.open("fileName.txt");//要写的txt 放到工程下
fout<<123456<<endl;//像cout一样使用
fout.close();//关闭
ifstream ifs("outlog.txt");
string str;
FILE *input;
char *str, line[4000];
input = fopen("1", "rt");
for (int i = 0; i < num; i++){
str = fgets(line, 4000, input);
if (str == NULL) break;
str = strtok(str, " ");
M[i*2]=atof(str);
}
二。数据长这样,要读入m1 m2
13 34
34 34
98 98
ifstream file1; file1.open("../data/1.txt"); while(! file1.eof()) { string s; getline(file,s); if(! s.empty) { stringstream ss; ss<<s; double m1; ss>>m1; ss>>m2; } }
另有
http://blog.csdn.net/syunqiang/article/details/6425302
#include <iostream> #include <sstream> #include <fstream> #include <string> int main(int args, char **argv) { std::ifstream fin("split.txt", std::ios::in); char line[1024]={0}; std::string x = ""; std::string y = ""; std::string z = ""; while(fin.getline(line, sizeof(line))) { std::stringstream word(line); word >> x; word >> y; word >> z; std::cout << "x: " << x << std::endl; std::cout << "y: " << y << std::endl; std::cout << "z: " << z << std::endl; } fin.clear(); fin.close(); return 0; }