The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > High Level Programming
.
google unix.com



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

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 09-16-2006
Sharmin Sharmin is offline
Registered User
  
 

Join Date: Sep 2006
Posts: 6
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
  #2 (permalink)  
Old 09-16-2006
wwwdev wwwdev is offline
Registered User
  
 

Join Date: Aug 2006
Posts: 6
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;
}
  #3 (permalink)  
Old 09-16-2006
Sharmin Sharmin is offline
Registered User
  
 

Join Date: Sep 2006
Posts: 6
Hello, but I want to know how to use sscanf( ) here

Quote:
Originally Posted by wwwdev
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,

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.
  #4 (permalink)  
Old 09-16-2006
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
  
 

Join Date: Feb 2004
Location: NM
Posts: 5,717
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;
}
Here is what you get:
Quote:
$ sscanftest
Enter a string in lower case hello world
The string was ||hello world||
$ sscanftest
Enter a string in lower case hello, unix world
The string was ||hello||
$ sscanftest
Enter a string in lower case abcd1234
The string was ||abcd||
$
Notice:
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
  #5 (permalink)  
Old 09-16-2006
Sharmin Sharmin is offline
Registered User
  
 

Join Date: Sep 2006
Posts: 6
Thank you

Quote:
Originally Posted by jim mcnamara
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;
}
Here is what you get:


Notice:
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

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..
  #6 (permalink)  
Old 09-17-2006
matrixmadhan matrixmadhan is offline Forum Advisor  
Technorati Master
  
 

Join Date: Mar 2005
Location: leaf node in B+ tree
Posts: 2,944
Quote:
Hello,

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.
't' - mode specifier
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
Closed Thread

Bookmarks

Tags
linux

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 07:13 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0