文件的读写

34 篇文章 0 订阅
30 篇文章 0 订阅

eg:

文本中每一行的数据结构,它们是以tab键为间隔的

Afghanistan    Baghlan    36.12    68.7
Afghanistan    Balkh    36.758    66.896

 

 

#include "stdafx.h"
#include "fstream"
#include<string>

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
    string strLine;
    string strCountry,strCity,strLat,strLong;
    ifstream file;
    int ipos=0;

    file.open("D://test//testfile//Debug//CityList.txt");
    while(std::getline(file,strLine))  //利用全局函数读取文本中的每一行。
    {
        ipos = strLine.find('/t');
        strCountry = strLine.substr(0,ipos);
        strLine = strLine.substr(ipos+1);

        ipos =strLine.find('/t');
        strCity = strLine.substr(0,ipos);
        strLine = strLine.substr(ipos+1);

        ipos =strLine.find('/t');
        strLat = strLine.substr(0,ipos);
        strLong = strLine.substr(ipos+1);

        cout<<strCountry<<"/t"<<strCity<<"/t"<<strLat<<"/t"<<strLong<<endl;
    }

    file.close();

    system("pause");
    return 0;
}

 

 

PS:

String.substr()
可用性
Flash Player 5。
用法
my_str.substr(start, [length])
参数
start 一个整数,指示 my_str 中用于创建子字符串的第一个字符的位置。如果 start 为一个负数,则起始位置从字符串的结尾开始确定,-1 表示最后一个字符。
length 要创建的子字符串中的字符数。如果没有指定 length,则子字符串包括从 start 开始直到字符串结尾的所有字符。
返回
指定字符串的子字符串。
说明
方法;返回字符串中的字符,这些字符从 start 参数所指定的索引开始,直至达到 length 参数所指定的字符数为止。substr 方法不更改由 my_str 指定的字符串,而是返回一个新字符串。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值