C++读取每行特定列的整数

23 篇文章 0 订阅

输入文本如下:

12345 23455 65477 34634
23355 55456 56666 35667
23445 63456 24566 24645
24545 24545 24555 23455
23455 24355 24356 24356

代码如下:

/* 
读取每行的第二个整形数值 
*/  
#include<iostream>  
#include<fstream>  
#include<string>
using namespace std;  
  
int main(){  
    int temp1,m;  
	string s;
    char buffer[30];  
    ifstream fin;  
    fin.open("number.txt",ios::in);  
    if(!fin.is_open()){  
        cout<<"Error opening file";exit(1);  
    }  
    //读取每行的第二个整形数值  
    /*//法一 
    while(!fin.eof()){ 
        fin>>temp1>>m>>temp1>>temp1; 
        cout<<m<<endl; 
    } 
    */  
    /*//法二  
    while(!fin.eof()){  
        fin.seekg(5,ios::cur);  
        fin>>m;  
        fin.getline(buffer,30);  
        cout<<m<<endl;  
    }*/
	//法三
	while(!fin.eof()){
		getline(fin,s);
		m=atoi(s.substr(6,5).c_str());
		cout<<m<<endl;
	}
    return 0;  
}

结果如下:



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值