YUYU
asked on
pr
Problem with my c# application :
I have file 202.txt I need to extract ip address from it and save the all ip in another file ip.txt
problem that occur :
Object reference not set to an instance of an object.
in line : string str = "http://" + currLine.Substring(currLin e.IndexOf( "2"), (currLine.IndexOf(":")) - 2) + "/";
i guess the problem is the null in the end of file how i can solve the problem.
I have file 202.txt I need to extract ip address from it and save the all ip in another file ip.txt
problem that occur :
Object reference not set to an instance of an object.
in line : string str = "http://" + currLine.Substring(currLin
i guess the problem is the null in the end of file how i can solve the problem.
StreamReader srFile = new StreamReader("e:\\202.txt");
StreamWriter ip = new StreamWriter("e:\\ip.txt");
string currLine = srFile.ReadLine();
int f = currLine.IndexOf("2");
int l = currLine.IndexOf(":");
while (currLine != null)
{
if(currLine.Contains(".")){
currLine = srFile.ReadLine();
string str = "http://" + currLine.Substring(currLine.IndexOf("2"), (currLine.IndexOf(":")) - 2) + "/";
ip.WriteLine(str);
}
}
ASKER
i use notepad++ to open it . 202.txt
hi,
try:
but this is valid if all ip starts with "2"
if not:
try:
StreamReader srFile = new StreamReader("e:\\202.txt");
StreamWriter ip = new StreamWriter("e:\\ip.txt");
string currLine = srFile.ReadLine();
while (currLine != null)
{
int f = currLine.IndexOf("2");
int l = currLine.IndexOf(":");
if(f!=-1 && l!=-1){
string str = "http://" + currLine.Substring(f, (l) - 2) + "/";
ip.WriteLine(str);
}
currLine = srFile.ReadLine();
}
Toggle HighlightingOpen in New WindowSelect All
Capture1.PNG (54 KB) (File Type Details)
capture
capture
hope it helps,but this is valid if all ip starts with "2"
if not:
StreamReader srFile = new StreamReader("e:\\202.txt");
StreamWriter ip = new StreamWriter("e:\\ip.txt");
string currLine = srFile.ReadLine();
while (currLine != null)
{
int f = 2;//index of first letter of ip
int l = currLine.IndexOf(":");
if(l!=-1){
string str = "http://" + currLine.Substring(f, (l) - 2) + "/";
ip.WriteLine(str);
}
currLine = srFile.ReadLine();
}
Toggle HighlightingOpen in New WindowSelect All
Capture1.PNG (54 KB) (File Type Details)
capture
capture
this would help
StreamReader srFile = new StreamReader("e:\\202.txt" );
StreamWriter ip = new StreamWriter("e:\\ip.txt") ;
string currLine = srFile.ReadLine();
int f = currLine.IndexOf("2");
int l = currLine.IndexOf(":");
while (currLine != null)
{
if(currLine.Contains(".")) {
string str = "http://" + currLine.Substring(currLin e.IndexOf( "2"), (currLine.IndexOf(":")) - 2) + "/";
ip.WriteLine(str);
currLine = srFile.ReadLine();
}
}
StreamWriter ip = new StreamWriter("e:\\ip.txt")
string currLine = srFile.ReadLine();
int f = currLine.IndexOf("2");
int l = currLine.IndexOf(":");
while (currLine != null)
{
if(currLine.Contains("."))
string str = "http://" + currLine.Substring(currLin
ip.WriteLine(str);
currLine = srFile.ReadLine();
}
}
ASKER
magadesign_sviluppo :
sorry your code not work .
sorry your code not work .
I don't get any error with the file you provided ...
StreamReader srFile = new StreamReader("e:\\202.txt");
StreamWriter ip = new StreamWriter("e:\\ip.txt");
string currLine = srFile.ReadLine();
int f = currLine.IndexOf("2");
int l = currLine.IndexOf(":");
while (currLine != null)
{
if(currLine.Contains("."))
{
string str = "http://" + currLine.Substring(currLine.IndexOf("2"), (currLine.IndexOf(":")) - 2) + "/";
ip.WriteLine(str);
}
currLine = srFile.ReadLine();
}
ASKER
pratima_mcs code work without problem but doesn't write to ip.txt file .
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks to all .
magadesign_sviluppo the best solution .
magadesign_sviluppo the best solution .
ASKER
thanks
experts-exchange is the best :)
When the error happens, what is the value of currLine ?
I also think that you'll never leave the loop if the line does not contain "." because in that case you don't read any other line ... --> infinite loop
Open in new window