c++读取文件内容并保存到二维数组

每行数据最后需要Tab处理

 #include <iostream>
#include <fstream>
#include <string>
using namespace std; int spilt_n(string s)//每行以‘\t’来统计有多个子字符串
{
const char *ch;
int i,count = ;
ch = s.c_str();
for(i=;i<s.length()-;i++)
{
if(ch[i]=='\t')count++;
}
// cout<<"count:"<<count<<endl;
return count;
} int main()
{
ifstream in;
ofstream out; in.open("D://test.txt",ios::in);
if(!in)
{
cout<<"open file fail!";
}else cout<<"open file success!"<<endl; string str[][];//存放数据
string temp;
getline(in,temp,'\n');
int num;
num = spilt_n(temp);
in.close();
in.open("D://test.txt",ios::in);
// cout<<"num:"<<num<<endl;
int mid = ;
int count =;
while(!in.eof())
{
// mid = 0;
if(mid<num)
{
getline(in,str[count][mid++],'\t');
// cout<<"count"<<count<<",mid"<<mid<<":"<<str[count][mid++]<<endl;
}else{
mid = ;
count++;
}
} // cout<<str[0]<<endl;
int i,j;
for(i=;i<count;i++)
{
for(j=;j<num;j++)
{
cout<<str[i][j]<<"\t";
}
cout<<endl;
} in.close();
return ;
}
上一篇:使用Vue2完成“小红书” app


下一篇:vulnhub writeup - 持续更新