从txt文件读取内容并进行字符串分割
2017-04-24 16:45
429 查看
话不多说,直接上源码:
如果不通过单独写函数的形式也可以实现文件数据的读取和转型:
#include <iostream>
#include <stdio.h> //sscanf的头文件
#include <string>
#include <fstream>
using namespace std;
int main(){
//string mystr = "20 12 54 75";
ifstream in("text.txt");
string mystr, mystr1;
while(getline(in,mystr1)){
cout << mystr1.c_str() << endl;
mystr = mystr1; //如果没有这行赋值,离开这个循环,mystr1还是空的
}
cout << "method 1:" << endl;
int num[4];
sscanf(mystr.c_str(),"%d %d %d %d", &num[0], &num[1], &num[2], &num[3]); //method1 通过sscanf的方式也可以格式化分割,此时文件中的数据是用空格分开的
for(int i = 0; i < 4; i++){
cout << num[i] << '\t';
}
cout << endl;
cout << "method 2:" << endl;
ifstream inn("text.txt");
int num2[4];
for(int i = 0; i < 4; i++){
inn >> num2[i]; //通过逐项写入的方式,这两种方法都是必须知道要写如项的个数
}
for(int i = 0; i < 4; i++){
cout << num2[i] << '\t';
}
return 0;
}
#include <iostream> #include <string> #include <vector> #include <stdlib.h> //用atoi函数必须包含的头文件 #include <fstream> //用ifstream必须包含的头文件 using namespace std; vector<string> strSplit(string, string); vector<string> strSplit(string mystr, string split_char){ vector<string> strs; size_t index = 0; //size_t是无符号整数,其中定义了特殊的标识:npos。当find()函数查找失败时,返回npos用size_t类型的变量接受 int lastPosition = 0; //用于分割字符串 int initialSize = mystr.size(); cout << mystr.size() << endl; while(mystr.size() != 0){ index = mystr.find(split_char); if(index != string::npos){ strs.push_back(mystr.substr(lastPosition, index)); mystr = mystr.substr(index + 1, initialSize); initialSize = mystr.size(); } else{ strs.push_back(mystr); break; } } return strs; } int main(){ ifstream in("text.txt"); //绝对路径或者相对路径 string mystr1; string mystr; while(getline(in,mystr1)){ cout << mystr1.c_str() << endl; mystr = mystr1; //如果没有这行赋值,离开这个循环,mystr1还是空的 } string split_char = ","; vector<string> strs = strSplit(mystr, split_char); //cout << strs.size() << endl; int num[5]; for(size_t i = 0; i < strs.size(); i++){ num[i] = atoi(strs[i].c_str()); } for(int i = 0; i < 5; i++){ cout << num[i] << '\t'; } return 0; }
如果不通过单独写函数的形式也可以实现文件数据的读取和转型:
#include <iostream>
#include <stdio.h> //sscanf的头文件
#include <string>
#include <fstream>
using namespace std;
int main(){
//string mystr = "20 12 54 75";
ifstream in("text.txt");
string mystr, mystr1;
while(getline(in,mystr1)){
cout << mystr1.c_str() << endl;
mystr = mystr1; //如果没有这行赋值,离开这个循环,mystr1还是空的
}
cout << "method 1:" << endl;
int num[4];
sscanf(mystr.c_str(),"%d %d %d %d", &num[0], &num[1], &num[2], &num[3]); //method1 通过sscanf的方式也可以格式化分割,此时文件中的数据是用空格分开的
for(int i = 0; i < 4; i++){
cout << num[i] << '\t';
}
cout << endl;
cout << "method 2:" << endl;
ifstream inn("text.txt");
int num2[4];
for(int i = 0; i < 4; i++){
inn >> num2[i]; //通过逐项写入的方式,这两种方法都是必须知道要写如项的个数
}
for(int i = 0; i < 4; i++){
cout << num2[i] << '\t';
}
return 0;
}
相关文章推荐
- c++ 读取txt文件,split分割字符串
- C++ 读取文件内容,分割出字符串
- Java连接数据库+连续读取文件夹下txt文件+读取出txt文件后字符串简单分割连接处理+导入jar包
- c++字符串分割 和 c++读取文件内容
- 将TXT文件上传的服务器上并将读取内容绑定到DataTable进行验证
- 求助:C++中读取txt文件如何忽略特定符号后的内容?
- ASP读取显示TXT文件内容
- c#读取文件内容存放到int数组 array.txt
- java编程心得(六)——BufferedReader读取txt文本内容到指定字符串结束
- 读取项目中的txt文件内容
- JAVA读取WORD,EXCEL,PDF,TXT,RTF,HTML文件文本内容的方法示例
- c#读取文件内容存放到int数组 array.txt
- jsp读取txt文件后,将txt文件内容分页显示
- sql 读取txt 文件内容,并写入sql的方法
- 读取Htm文件内容到字符串
- PHP 读取文件内容代码(txt,js等)
- 请教Ado.Net按文本读取CSV/Txt文件时,如何禁止将内容转换成数字
- Java读取文件内容并转换为字符串
- sql 读取txt 文件内容,并写入sql的方法
- 使用Robot从txt文件中读取不同行的内容并显示(续)