Are the line lengths the same?
Is capitalization, spaces and punctuation the same?

Have you called stricmp() to see if that resolves the problem ?

Will you zip up the file and attach it to your post so I can view it on my computer. Don't post it as *.txt file so that DaniWeb doesn't change it

Member Avatar for iamthwee

You can test it yourself Ancient Dragon. (assuming you're on windows)

Just go to:

Start > run > cmd <ok>

Then type in the command prompt: ipconfig>output.txt

Which will create a text file in that directory. The strange thing is when you add an extra letter to the end of that file, save it, close it, then open it again, all the newlines seems to be eaten up.

Looks like the problem is unitialized buffer. Change this and it works ok char str[500] = {0}; . Now then why its considered an error to find that string is another matter.

Member Avatar for iamthwee

>Change this and it works ok char str[500] = {0};.

No, I don't think that will make any difference.

It worked for me (famous expression) -- VC++ 2008 Express. Before the change the program was iterating beyond the end of the string and displaying junk characters. That stopped after I made that change. Instead of just saying you don't think it will work you should try it to find out for yourself.

It worked for me (famous expression) -- VC++ 2008 Express. Before the change the program was iterating beyond the end of the string and displaying junk characters. That stopped after I made that change. Instead of just saying you don't think it will work you should try it to find out for yourself.

i am also using VC++ 2008 and it did not work for me , something strange i have noticed
here is a new code i used for the same file (or system() output)

char str[500];int i=0;
	system("ipconfig>output.txt");
	fstream file("output.txt",ios::in);	
	while( file.getline(str,500) )
	{
		cout<<endl;
	   while(str[i])
	   {
		 cout<<"["<<str[i]<<"]";i++;
	   }
	   i=0;
	   cin.ignore();
	}
          file.close();
		  cin.ignore();
          return 0;

check the first 2 lines of the output result:

]

]W][i][n][d][o][w][s][ ][I][P][ ][C][o][n][f][i][g][u][r][a][t][i][o][n][

]

in the first line where its an empty space you can notice only one bracket is printed out..
and in the second line, one of the brackets surrounding the "w" is reversed and there is an extra bracket at the end of the line which i believe belongs to the previous line.

>>and it did not work for me
The code you posted is not what I used. Line #1 is not the same, see my post #33 for the correction.

>>and it did not work for me
The code you posted is not what I used. Line #1 is not the same, see my post #33 for the correction.

i did try it with the code before the last one i posted , it did not work

Here is what I used. What you posted isn't a complete program -- this one is.

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main()
{
    char str[500] =  {0};


    char error[]="Windows IP Configuration"; //error string to find
	system("ipconfig>output.txt");
    fstream file("output.txt",ios::in);
	
    int i=0,l=1,counter=0;
	
    while( file.getline(str,500) )
    {
        cout<<"\nLine #"<<l<<" (length:" << strlen(str) << "):\n\n";

        while(str[i]==error[i])
        {
            cout<<"[ "<<str[i]<<" ]"<<" = "<<"[ "<<error[i]<<" ]\n";	
            i++;
            counter++;				
        }

        if(strcmp(error,str)==0 || counter==24)
        {
            cout<<"\nerror found!\n";cin.ignore();
        }

        i=0;l++;counter=0;
    }
    file.close();
    return 0;
}

And here's the output, which I think is what you want.


Line #1 (length:0):


Line #2 (length:24):

[ W ] = [ W ]
[ i ] = [ i ]
[ n ] = [ n ]
[ d ] = [ d ]
[ o ] = [ o ]
[ w ] = [ w ]
[ s ] = [ s ]
[ ] = [ ]
[ I ] = [ I ]
[ P ] = [ P ]
[ ] = [ ]
[ C ] = [ C ]
[ o ] = [ o ]
[ n ] = [ n ]
[ f ] = [ f ]
[ i ] = [ i ]
[ g ] = [ g ]
[ u ] = [ u ]
[ r ] = [ r ]
[ a ] = [ a ]
[ t ] = [ t ]
[ i ] = [ i ]
[ o ] = [ o ]
[ n ] = [ n ]
[ ] = [ ]

error found!

ok dragon change this statement if(strcmp(error,str)==0 || counter==24) to this if(strcmp(error,str)==0 && counter==24) the reasen you got that output is because at least the counter condition was true

ok check my post#36 , some character is added by system() to each line , the first line is supposed to be an empty line, but it contains that character, maybe if we strip that character out of each line before comparing the line that would fix the problem , i don't know how to do it though

Member Avatar for iamthwee

No Ancient Dragon, I'm afraid you are still wrong. I don't mean to be rude, but you do not understand the nature of the problem. I admit I didn't at first either until I actually opened the file created by ipconfig for further inspection.

Here is a crappy solution using substrings, it might not even be want you want but at least it finds the line:-

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main()
{
  string str;

  string err = "Windows IP Configuration"; //error string to find
  system ( "ipconfig>output.txt" );
  ifstream file ( "output.txt" ); 

  while ( getline ( file, str, '\n' ) )
  {
    cout << str << endl;

    int pos = str.find ( err );
    //cout << pos << endl;
    if ( pos > -1 )
    {
      cout << "\nerror found!\n";
      cin.ignore();
    }
  }
  file.close();
  system ( "Pause" );
  return 0;
}

Notice the problem if you just try to compare the line (it doesn't match) even though when you open up the text file it looks like it should:-

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main()
{
    string line;

    string err ="Windows IP Configuration"; //error string to find
    system("ipconfig>output.txt");
    ifstream read("output.txt");
	
   	
    while( getline(read,line,'\n') )
    {
        cout <<line<<endl;

        //int pos = str.find(err);  
        //cout<<pos<<endl;    
        if(line==err)
        {
            cout<<"\nerror found!\n";
            cin.ignore();
        }
  
    }
    read.close();
    system("Pause");
    return 0;
}
commented: thanks +1

thanks iamthwee , your solution is not crappy at all its good , it does find the error line and that is exactly what i want it to do :)

thank you so much :)

i have a question iamthewee,

why use if ( pos > -1 ) and not if ( pos != 0 ) ?

Member Avatar for iamthwee

i have a question iamthewee,

why use if ( pos > -1 ) and not if ( pos != 0 ) ?

You can try it yourself and see what happens. But the short answer is it won't work.

Basically:-
If there isn't a match pos will be -1.
If there is a match it will return the character offset in that string.

i see, let me will check it out

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.