string 类使用问题
#include<iostream>
#include<fstream>
#include<cstring>
#include<string>
using namespace std;
int readFileLBL(string strFileName,string s[])
{
char *fileName=new char[strFileName.size()+1];
strcpy(fileName,strFileName.c_str());
int lineNum=0;
ifstream fin(fileName);
if(fin.is_open())
{
while(getline(fin,s[lineNum]))
{
lineNum++;
}
fin.close();
}
else
{
}
return lineNum;
}
int main()
{
string s[1000],str("Test");
int lineNum;
lineNum=readFileLBL("n",s);
for(int i=0;i<lineNum;i++)
{
s[i]="<td>"+s[i]+"</td";
cout<<s[i]<<endl;
}
cout<<"EEEEEEEEEEEEEEEE"<<endl;
cout<<str<<endl;
return 0;
}
//文件"n"的内容
DNS request timed out.
timeout was 2 seconds.
Server: google-public-dns-b.google.com
Address: 8.8.4.4
DNS request timed out.
timeout was 2 seconds.
Name: www.a.shifen.com
Address: 220.181.111.147
Aliases: www.baidu.com
麻烦大写保存文件n的内容,然后编译运行这个程序,然后把输出的结果给我,谢谢.本来是应该输出<td>DNS request timed out.</td>.....的,可是总是输出</td>NSDNS request timed out.,我实在是找不出原因了,希望大家可以帮忙.
[解决办法]
#include<iostream>
#include<fstream>
#include<cstring>
#include<string>
using namespace std;
int readFileLBL(string strFileName, string s[])
{
char *fileName = new char[strFileName.size() + 1];
strcpy(fileName, strFileName.c_str());
int lineNum = 0;
ifstream fin(fileName);
if(fin.is_open())
{
while(getline(fin, s[lineNum]))
{
lineNum++;
}
fin.close();
}
else
{
}
return lineNum;
}
int main()
{
string s[1000], str("Test");
int lineNum;
lineNum = readFileLBL("D:\\n.txt", s);
for(int i = 0; i < lineNum; i++)
{
s[i] = "<td>" + s[i] + "</td>";
cout << s[i] << endl;
}
cout << "EEEEEEEEEEEEEEEE" << endl;
cout << str << endl;
cin.get();
return 0;
}
<td> timeout was 2 seconds.</td>
<td>Name: www.a.shifen.com</td>
<td>Address: 220.181.111.147</td>
<td>Aliases: www.baidu.com</td>
EEEEEEEEEEEEEEEE
Test