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

每行数据最后需要Tab处理

 

 1 #include <iostream>
 2 #include <fstream>
 3 #include <string>
 4 using namespace std;
 5 
 6 int spilt_n(string s)//每行以‘\t’来统计有多个子字符串
 7 {
 8     const char *ch;
 9     int i,count = 1;
10     ch = s.c_str();
11     for(i=0;i<s.length()-1;i++)
12     {
13         if(ch[i]=='\t')count++;
14     }
15 //    cout<<"count:"<<count<<endl;
16     return count;
17 }
18 
19 int main()
20 {
21     ifstream in;
22     ofstream out;
23 
24     in.open("D://test.txt",ios::in);
25     if(!in)
26     {
27         cout<<"open file fail!";
28     }else cout<<"open file success!"<<endl;
29 
30     
31     string str[1000][20];//存放数据
32     string temp;
33     getline(in,temp,'\n');
34     int num;
35     num = spilt_n(temp);
36     in.close();
37     in.open("D://test.txt",ios::in);
38 //    cout<<"num:"<<num<<endl;
39     int mid = 0;
40     int count =0;
41     while(!in.eof())
42     {
43     //    mid = 0;
44         if(mid<num)
45         {
46             getline(in,str[count][mid++],'\t');
47         //    cout<<"count"<<count<<",mid"<<mid<<":"<<str[count][mid++]<<endl;
48         }else{
49             mid = 0;
50             count++;
51         }    
52     }
53 
54 //    cout<<str[0]<<endl;
55     int i,j;
56     for(i=0;i<count;i++)
57     {
58         for(j=0;j<num;j++)
59         {
60             cout<<str[i][j]<<"\t";
61         }
62         cout<<endl;
63     }
64 
65 
66 
67     in.close();
68     return 0;
69 }

 

转载于:https://www.cnblogs.com/minmsy/p/5058012.html

  • 0
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值