class string problem


#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;
}


//Below is the content of the file "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




I think it should be output<td>DNS request timed out.</td>.....but it always output </td>NSDNS request timed out. I can't slov this problem.Everybody, please! Copy this program and the file content, compile and run it and post the output here, so i can know is it just I encouter the problem. Everybody know how the problem benn, please help.


I'm Chinese. My english is bad. If you can't understand my words, please send email to me. My email: linjielig@hotmail.com.
Thank you everybody.
I tried your code. Its output:

<td>DNS request timed out.</td
<td>timeout was 2 seconds.</td
<td>Server: google-public-dns-b.google.com</td
<td>Address: 8.8.4.4</td
<td></td
<td>DNS request timed out.</td
<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

What is the exact output you get?
Last edited on
Thank you. Your result is right. But I got not the same as you. When I run the program I got
</tdDNS request timed out.
</tdtimeout was 2 seconds.
</tdServer: google-public-dns-b.google.com
</tdAddress: 8.8.4.4
</td
</tdDNS request timed out.
</tdtimeout was 2 seconds.
</tdName: www.a.shifen.com
</tdAddress: 220.181.111.147
</tdAliases: www.baidu.com
EEEEEEEEEEEEEEEE
Test



I use windows xp and the compiler is cygwin's g++, what about you? Thank you very much, your result prove my program is right. But something wrong with the compiler or the system! You are a kind man.
You forgot to add ">" symbol "end" tag, means "</td>" not "</td".
Last edited on
Thank you Jackson Marie. But your reply is not the problem I ask. The "</td" should appear the end of the sentence, but now it appear the begin, that's the key. Do you know why?
I used the Visual C++-Compiler of the Visual Studio 2010 IDE.
First try to cout << the lines without the td's, so remove the line
//s[i]="<td>"+s[i]+"</td";
This should get you an idea of whether your strings are loaded correctly and no extra endl appears before the table.
Then try what the output is with:
s[i]="<td>"+s[i];
Maybe the "+" operator is not working properly.
I never used cygwin so I can't tell you what the problem is here. It just seems that the problem is in this particular line. As an alternative you could use a concatenate function that adds string together.
Last edited on
I have know what cause this. Thank you everybody. The file "n" generate on windows's dos command "tracert www.baidu.com" and the compiler is cygwin's G++,it seems they are don't compatible very well. I copy the "n" file to Centos, the result is same. Then I use the command "tracert www.baidu.com > n" on Centos, I run the program again, the output is exact as my expect. That prove the program is no problem. The problem is the operate system and compilers don't compatible very well.


Thank you everyone.
Topic archived. No new replies allowed.