Quote:
|
Originally Posted by matrixmadhan
what is the post that you are referring to ?
is that from some other thread ??? 
|
Hello,
thank you. You all ready gave me answer about the extracting string and then printing it out. I thought that noone saw it.
At future, I will wait for a long time, before asking again. My question was how to extract a specific string from a file with sscanf( ) and then printing it out.
I got the answer. But now I wonder whether it is possible to extract more than one string from more than one line from a file. Say, we have a file,
cat /proc/cpuinfo
processor : 0
vendor_id : GenuineIntel
cpu family : 15
model : 2
model name : Intel(R) Celeron(R) CPU 2.60GHz
stepping : 9
cpu MHz : 2591.654
cache size : 128 KB
fdiv_bug : no
hlt_bug : no
f00f_bug : no
coma_bug : no
fpu : yes
fpu_exception : yes
cpuid level : 2
wp : yes
flags : fpu vme de pse
And I want to extract
vendor_id : GenuineIntel
model : 2
model name : Intel(R) Celeron(R) CPU 2.60GHz
I know that it is possible by wrting the following code three times in this case as we are extracting three strings here.
if(NULL == (fp = fopen(argv[1], "rt"))){
printf("Can't open %s\n", argv[1]);
exit(1);
}
for(;fgets(buf, 255, fp)
{
if(buf == strstr(buf, "model name")){
if(NULL != (p = strchr(buf, ':')))
printf("%s\n", p + 1);
break;
}
But is there a way, for which I can write the code once but can extract some specific strings and print them out at once one after the other.
And also, is there a way, so that in one C file I can open mutiple files by command line argument, one after the other. Say, after I am done with printing vendor_id, model type, and model name from
cpuinfo file from /proc directory, I open the
stat file from the /proc directory and start extracting some specific strings and printing them again. Is that a very compilcated way or much simple. Please let me know how to do that if you know.
And also, when we extract a string, say, cpu family : 15, 15 is a type string. Say, I want to convert string 15 to an integer; so, I use atoi(), but to print it, I have to do the following right?
int number = atoi(p);
printf("The integer value is: %d\n", number);
But well I cannot print the integer value. So, I think I am missing something.
Whatever help I can get, I will be very thankful.
Thank you.
