string search in C
hey all.
im trying to search for a string within another, so i looked up strstr(). Code:
void why would this be? thanks in advance. |
Re: string search in C
Quote:
|
Re: string search in C
Quote:
Code:
if ( (cmp = strstr (iw_result, temp)) == NULL) which contains some other variables Code:
system ("/sbin/iwconfig > iwcfgdump 2>/dev/null"); |
first up, thanks you two.
satan : umm, yes, i meant that im checking to see if there is a "test" string within the string variable 'iw_result'. i wanted to try it before searching for the ssid variable (neither works - and iw_result contains both). the system() just outputs the result of /sbin/iwconfig to the file iwcfgdump and does not print any message on std_err. Hko: the file is created. i checked it everytime i run the program, and sure enough, the strrings im searching for are there. Again, my intention is to search for the string for the string in variable 'temp' within the variable 'iw_result'. thanks. |
also, if i print out the 'iw_result' variable, i have the strings im trying to match in it - so thats not a problem.
|
any ideas?
|
I haven't really used the fopen and fread/fwrite stuff (I just go with plain open read and write calls) but from the looks of it, your code declares iwcfgdump as write only
Code:
p = fopen ("iwcfgdump", "w+"); Code:
|
Quote:
You are reading 8x80-byte chunks of the file into iw_result. Each call to fread() the previous contents of iw_result are overwritten with the newly read data. This repeats until there's nothing left to read (in which case nothing is overwritten in iw_result). Because you are reading chunks of 8x80 = 640 bytes, depending how much data iwconfig outputs, you are probably reading just 1 chunk, so nothing gets overwritten. So, merely by luck, the text you are looking for is preserved in iw_result. Though it seems to work, this is not the way to do it. Also, as fread() is for reading binary data, it does not insert a '\0' character. This is more or less ok if your provide for appending a '\0' at the right place yourself, but your program doesn't. Because of this strstr() stops comparing only when it encounters a '\0' by accindent. This doesn't explain for 100% why it would not find the string you search for, but your program does have severe bugs. |
understood. i dont even need to have the data in the file - i only need it in a variable, and do the comparison. very roundabout way to do this - what im doing.
whats the best way to read a syscall output into a variable? on what i was trying - i havent really understood the problem with the while loop. i wanted to keep reading the data from the file till an EOF was reached, which was what the manpage said it would do. again, whats the right way to do this? thanks a lot. :) |
Quote:
However, you can use the popen() syscall to make it more clean, and maybe a little bit faster. popen("<command>", "r") starts the command just like system(), but it connects the standard output (stdout) to the FILE* it returns. This works the same way as "iwconfig | your_prog". After popen(), you can read from the FILE* returned by popen() just like you would read from the temp file. You also don't have to delete/unlink anything afterwards, just close the pipe with pclose(). Quote:
If the file is larger than 640 bytes, the while loop will run a second time executing the same fread() call again, storing the next 640 in the same place: iw_result. Thus overwriting the first 640 bytes. One solution is to have a while loop that reads one line from iwconfig, scans the line for some string, say "test". And then read the next line, and scan again. Until the end-of-file is reached. something like: Code:
while (read next line) { Code:
while (read 640 bytes) { Code:
#include <stdio.h> |
thanks Hko, satan and orgcandman - a lot!
|
i tried the code snippet of ur Hko, it works fine.
but... problem persists. when i give the value of temp = "test", and search using temp, it matches correctly. but if i passed the same string from some other function into this one, and try to compare, it doesnt work. and both the temp, and the parsed string, when i output, look the same to me. Code:
void Code:
if (strstr(one_line, ssid) != NULL) { as i said, when i print out temp, and ssid, they both look the same. i wonder whats the issue. |
i tried to compare the two using strcmp(), and they dont match. and i cant seem to figure out what the problem is, cos if i print them out, theyre the same. could it have anything to do with an \0 character or something?
tia. |
I don't understand what the problem is. This works:
Code:
#include <stdio.h> |
there seems to be a problem with the string im passing - like i said, the strcmp() does not give me a 0, although if i print them out, they look the same.
im sooo lost now. :) |
All times are GMT -5. The time now is 12:52 AM. |