-
Notifications
You must be signed in to change notification settings - Fork 0
/
filestream.cpp
70 lines (49 loc) · 1.19 KB
/
filestream.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include<iostream>
#include<fstream>
#include<string>
#include<vector>
#include<stdlib.h>
#include<sstream>
#include<typeinfo>
using namespace std;
vector<string>saveText;
template <class Type>
Type stringToNum(const string& str)
{
istringstream iss(str);
Type num;
iss >> num;
return num;
}
void readFile(string fileName) {
ifstream infile;
infile.open(fileName.data());
string lineStr;
string input_result;//临时存储用字符串
if (!infile.is_open()) {
cout << "文件打开失败!" << endl;
return;
}
int lineCnt = 0; //行统计
while (getline(infile, lineStr)) {
vector<string> vectorString;
stringstream input(lineStr);
//依次输出到input_result中,并存入vectorString中
while (input >> input_result){
cout <<"input_result:"<< input_result << endl;
vectorString.push_back(input_result);
}
for (int j = 0; j<vectorString.size(); j++) {
cout <<"第"<<j<<"个"<< stringToNum<float>(vectorString[j])-1<< endl;
}
if (lineCnt == 0)break;
}
infile.close(); //关闭文件流
}
/*
int main()
{
readFile("D:/keyanshuju/Random_demand.txt");
return 0;
}
*/