如何从按行读文件&&以及每行中有多少个数据块,并输出每个数据块

  1. 主要内容:
    【1】如何读文件
    【2】如何按行读文件
    【3】每行数据块总数的判断
    【4】每行数据块的输出

  2. 不多说了,大家都亟不可待了吧,上代码

#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
    string FileName;
    //cin>>FileName;
    FileName = "1.txt";
    ifstream infile(FileName.c_str());
    //string tempstr = "192.168.1.107 37777 admin admin";
    string tempstr;
    char* IP = new char[20];
    char* port = new char[10];
    char* username = new char[20];
    char* password = new char[20];
    int i,sum,len;
    string st;
    while(getline(infile,tempstr))
    {
        for(sum=i=0,len=tempstr.length();i<len;i++)
        {
            if(tempstr[i] != ' ')
                st+=tempstr[i];
            else if(tempstr[i] = ' ')
            {
                sum++;
                switch(sum)
                {
                case 1:
                    IP = (char*)st.data();
                    cout<<IP<<endl;
                    break;
                case 2:
                    port = (char*)st.data();
                    cout<<port<<endl;
                    break;
                case 3:
                    username = (char*)st.data();
                    cout<<username<<endl;
                    break;
                case 4:
                    password = (char*)st.data();
                    cout<<password<<endl;
                    break;
                default:
                    break;
                }
                st="";
            }
        }
        cout<<endl;
    }

    system("pause");
    return 0;
}

实验结果
这里写图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值