主要内容:
【1】如何读文件
【2】如何按行读文件
【3】每行数据块总数的判断
【4】每行数据块的输出不多说了,大家都亟不可待了吧,上代码
#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;
}
实验结果