LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-01-2010, 12:42 PM   #1
shlonginator
LQ Newbie
 
Registered: Jul 2009
Posts: 4

Rep: Reputation: 0
Parse /proc filesystem "files" for results


Hi All,

Im new to C so hope you can help.

Im reading in file /proc/cpuinfo into a char array.
Im using strstr to search for a string im interested in (the cpu speed line) and storing it in another char array.
I want to just store the line that contains CPU speed but im getting everything after the CPU speed line in the char array too.
Is there anything better than strstr or is there someway i can parse the the payload data (i.e. the actual cpu speed)

Thanks for the help,
shlonginator
 
Old 12-01-2010, 06:00 PM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192
Well according to this definition:
Quote:
The strstr function locates the first occurrence of the string string1 in the string string2 and returns a pointer to the beginning of the first occurrence.
I am guessing you are searching for something like:
Code:
cpu MHz
As the definition says it will then return everything from 'cpu MHz' to the end of the string. SO maybe if you use strstr again but this time now look for the first
occurrence of a digit you will get what you are looking for.

If I have ballsed up what you are trying to do, then please let me know?
 
Old 12-02-2010, 08:09 AM   #3
shlonginator
LQ Newbie
 
Registered: Jul 2009
Posts: 4

Original Poster
Rep: Reputation: 0
Hi grail.

Thanks for your response.
Yes I was searching for cpu MHz. There are other digits in /proc/cpuinfo so i cannot search with digits...
As you said strstr will return a pointer to the beginning of the first occurence.
However it also returns all the info after the clockspeed entry, e.g.

cpu MHz : 2133
cache size : xxx
physical id : x
etc etc

where I only want to store the cpu Mhz value.
Can you recommend anything better than strstr?

Many thanks,
shlonginator
 
Old 12-02-2010, 08:16 AM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192
Maybe if you supply what code you have so far and we can work from there?
My C is rusty so it helps if I can see what we are trying to do
 
Old 12-02-2010, 08:19 AM   #5
Guttorm
Senior Member
 
Registered: Dec 2003
Location: Trondheim, Norway
Distribution: Debian and Ubuntu
Posts: 1,453

Rep: Reputation: 447Reputation: 447Reputation: 447Reputation: 447Reputation: 447
Hi

After you have found the position of "cpu MHZ" with strstr, you can look for the next newline. You get the length of the line with something like strcspn(line,"\n").
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
what is "FRAG" in "/proc/net/sockstat" ? dileepkk Linux - Networking 1 06-21-2011 11:16 AM
upgrade to kernel 2.6.16.1 : "make bzImage" print "parse error" math_physics Red Hat 2 06-29-2007 11:04 PM
"Successful install" results in "Boot device not found" slackr007 Fedora 2 06-21-2005 04:05 PM
"Successful install" results in "Boot device not found" slackr007 Linux - Newbie 2 05-31-2005 08:02 PM
Writing to "/dev/usb/hiddev*" and to "/proc/bus/usb/00B/00S" throw an arror EI stpg Programming 0 07-14-2004 05:44 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:33 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration