Why I don't get any output?


 
Thread Tools Search this Thread
Top Forums Programming Why I don't get any output?
# 1  
Old 09-16-2006
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
Smilie

Last edited by Sharmin; 09-16-2006 at 02:52 PM.. Reason: Editing wording
# 2  
Old 09-16-2006
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  
Old 09-16-2006
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  
Old 09-16-2006
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  
Old 09-16-2006
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. Smilie

Last edited by Sharmin; 09-16-2006 at 10:50 PM..
# 6  
Old 09-17-2006
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
# 7  
Old 09-17-2006
Thank you

Quote:
Originally Posted by matrixmadhan
'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

Thank you a lot. Just to let you know, as I am learning Ansi C now, I will be coming here with questions. I hope that it is okay. I know that there are lots of manuels, but I hope that I could get a website with manpage details and also with more practical coding examples.
Login or Register to Ask a Question

Previous Thread | Next Thread

5 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

A shell script to run a script which don't get terminated and send a pattern from the output by mail

Hi Guys, I am very new to shell script and I need your help here to write a script. Actually, I have a script abc.sh which don't get terminated itself. So I need to design a script to run this script, save the output to a file, search for a given string in the output and if it exists send those... (11 Replies)
Discussion started by: Sambit Sahu
11 Replies

2. Shell Programming and Scripting

Find command don't output anything, but file is there

Please if You can help me debug why nothing is found by this command? # echo "Zeus Robot" >> /home/vps/190/test # cat /home/vps/190/test Zeus Robot # find /home/vps -type f -mtime 2 -size -1000k -exec grep -l "Zeus Robot" {} \; >> out # cat out # cat /home/vps/190/test Zeus Robot Why... (6 Replies)
Discussion started by: postcd
6 Replies

3. Shell Programming and Scripting

AIX -/usr/bin/expect shows output Don't know why.

Hi, I have been programming with the expect program for a while now and have create a series of menu driven checks for the operations team. One thing I have noticed is that I call a remote script and pass parameters and this is display on the screen....for example. Within the script ... (0 Replies)
Discussion started by: yakky
0 Replies

4. Programming

std::cout and gfortran print*, don't output to the screen

I am not sure where to post this other than here. I am trying to figure out why an app gives different output when compiled under Ubuntu 10.10 and CentOS 5.5. I am pretty sure that the issue is that the Cent version has gcc 4.1 installed, while Ubuntu has gcc 4.4. I am trying to print from some... (20 Replies)
Discussion started by: LMHmedchem
20 Replies

5. UNIX for Dummies Questions & Answers

Don't allow to editing !!!

Hi All, Assume that there is file1 file,userA and userB have got read,write and execute permission for file1. when userA open file1, userB cant change(write,editing file etc...) content of file1. after userA exit of file1 , userB can change(write, editing file etc..). Shortly i want... (1 Reply)
Discussion started by: temhem
1 Replies
Login or Register to Ask a Question