![]() |
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here. |
More UNIX and Linux Forum Topics You Might Find Helpful
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| how to make a line BLINKING in output and also how to increase font size in output | mail2sant | Shell Programming and Scripting | 3 | 04-14-2008 07:30 AM |
| top output | new2ss | UNIX for Advanced & Expert Users | 4 | 03-11-2008 04:58 AM |
| awk output | useless79 | Shell Programming and Scripting | 3 | 09-03-2007 10:21 AM |
| FTP Output | lindeng | UNIX for Dummies Questions & Answers | 9 | 02-10-2004 10:18 PM |
| ps output | Shobhit | UNIX for Advanced & Expert Users | 2 | 03-10-2002 09:45 PM |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
||||
|
Why I don't get any output?
Hello,
I am very new in writing low level programming in C. I am trying to get an output in Linux 2.6.17.6 gentoo platform, but I don't get any output. I am trying to do the following: I am trying to scan a word and print its content at the standard output by using sscanf and printf. I am trying to read the cpuinfo at the /proc directory, which looks like as following, and then, I am trying to print the highlighted string. cat 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 tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe up cid xtpr bogomips : 5189.98 But, I can only print the model name but cannot print its concent. My C file looks as below. #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <stdio.h> main(int argc,char **argv) { char mybuf[4256]; char vernum[116]; int fd; int errs; int count; if (argc != 2) { printf("Bad command line argument\n"); exit(-1); } if ((fd = open (argv[1],O_RDONLY))< 0){ perror("Bad open"); exit(-1); } while ((count = read(fd, &mybuf[0], sizeof(mybuf))) > 0) { count = read(fd,&mybuf[0],sizeof(mybuf)); sscanf(&mybuf[0],"model name %s",&vernum[0]); printf("The model name is:%s\n", &vernum[0]); } if(count < 0) { perror ("Bad read"); exit (-1); } } Please tell me what I shouls fix at sccaf( ) and at the printf( ) method. I tried a lot by reading the man page to read new line or empty space or \t and by putting %c or *. But I never cannot print anything after model name. If you need, you can see the file "cpuinfo" at /proc directory. Type cd /proc then cat cpuinfo. Please help me; I am here for help as it is a help forum for new and unexperienced like me. Please tell me what to do in the code and what to write....so that I can print the whole string "model name : Intel(R) Celeron(R) CPU 2.60GHz" Thank you. Sharmin ![]() Last edited by Sharmin; 09-16-2006 at 01:52 PM.. Reason: Editing wording |
|
||||
|
Hi Sharmin,
the simple way is Code:
#include <stdio.h>
#include <string.h>
main(int argc, char *argv[])
{
FILE *fp;
char buf[256], *p;
if(argc < 2){
printf("Bad command line\n");
exit(1);
}
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;
}
}
fclose(fp);
return 0;
}
|
|
||||
|
Hello, but I want to know how to use sscanf( ) here
Quote:
thank you. what does "rt" means at fopen line? But, I want to to know to how to use sscanf functinality instead of strstr. Please let me know how to extract the string with sscanf and then print it effectively. But thank you for your time. Hoping to get reply back from you. |
|
||||
|
You are confusing something called a "scanset" with what sscanf is doing in your example. A scanset is a CLASS of characters, for example, all lowercase:
Code:
/* sscanftest.c*/
int main()
{
char mystring[256];
printf("Enter a string in lower case ");
scanf("%[ a-z]",mystring); /* allow space character and all lc letters */
printf("The string was ||%s||\n",mystring);
return 0;
}
Quote:
1. the scanset does not look for a particular string like "model name" scansets filter in (or out with ^ as the first character) desired characters. 2. it stops scanning when it hits a character not in the scanset - like the comma In other words, sscanf does not do what you want it to do - look for a given string. Your syntax should be "%[ <scanset goes here> ]". Bottom line -- the strstr answer you already got works. scansets are not the desired way to go here. HTH |
|
||||
|
Thank you
Quote:
Hello, thank you so much for detaill answer. I cannot believe that we cannot use sscanf( ) here. But, thank you as I know a way now. But if I become able to extract the string with sscanf( ), I will definitely let you know by posting the method here. Thank you. ![]() Last edited by Sharmin; 09-16-2006 at 09:50 PM.. |
|
||||
|
Quote:
to open the file in text mode and not in binary mode default is 't' when it is not specified with fopen default value is set in _fmode in /usr/include/stdio.h |
![]() |
| Bookmarks |
| Tags |
| linux |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|