C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 06-23-2009, 10:53 AM   #1
Registered User
 
Join Date: Jun 2009
Posts: 31
C: array string

Hi

My goal is to obtain something like that:

Pid: value_of pid
Name: process_name
State: process_state

where the left side is static and i get the right side dinamically.
I see on:
intializing an array of strings
this help but isn't exactly what i'm looking for.
I've tried but obviously is wrong.
Code:
.....
char buf [ ] [MAX]={
{"PID"},
{"NAME"},
{"STATE"},
};
........

while (!feof(file_in) ){

fscanf(file_in,"%s", buf [ i ]);
printf("%s\n", buf [ i ] );
i++;
}......
Can anyone suggest the right way or a tutorial as well?

Thanks

D
Dedalus is offline   Reply With Quote
Old 06-23-2009, 11:01 AM   #2
Registered User
 
Join Date: Oct 2008
Location: TX
Posts: 1,262
provide more info as it's not possible (at least for me) to figure out what you're talking about
itCbitC is offline   Reply With Quote
Old 06-23-2009, 11:18 AM   #3
Registered User
 
Join Date: Jun 2009
Posts: 31
ok.

I've written a program that read the text file /proc/given_pid/stat that contain some stuff like:

stat:
1212 my_prog S ....

each entry in each position is a know parameter. I mean the first entry 1212 is the PID, the 2nd is the process name the 3d the state and so on.

the problem is that i can't understand how can i read the stat file and write on stdout something like:

PID: 1212
NAME: my_prog
STATE: S
.....

I was thinking to create something like: array [ PID, NAME, STATE ] [ 1212, my_prog, S ].
But still i don't know.

Thanks
Dedalus is offline   Reply With Quote
Old 06-23-2009, 11:25 AM   #4
Kernel hacker
 
Join Date: Jul 2007
Location: Farncombe, Surrey, England
Posts: 15,686
So, do you want to REPLACE the current "PID", "NAME", "STATE" with the actual value from /proc/... ? Or do you want to store the values ALONGSIDE the names of the fields?

If it is the former you want, then I think your code is about right.

If it is the latter, you certainly aren't doing it right.

Of course, we don't see what values i has for example, so it may be that you've got it wrong for BOTH things.

--
Mats
__________________
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
matsp is offline   Reply With Quote
Old 06-23-2009, 11:29 AM   #5
and the Hat of Guessing
 
tabstop's Avatar
 
Join Date: Nov 2007
Posts: 8,740
I'm thinking you want to use strcat, I guess, on the assumption that MAX is large enough to hold whatever it is. I guess.

Although I don't know why you need to store PID NAME and STATE; can't you just put those as literals in your printf statements?
tabstop is offline   Reply With Quote
Old 06-23-2009, 11:39 AM   #6
Registered User
 
Join Date: Jun 2009
Posts: 31
Quote:
Originally Posted by matsp View Post
So, do you want to REPLACE the current "PID", "NAME", "STATE" with the actual value from /proc/... ? Or do you want to store the values ALONGSIDE the names of the fields?
Sorry for my bad explain.
As tabstop suggest I'd like to do:

printf("%s\n", " PID:", array[0]);------>where array[0]= 1212
printf("%s\n","NAME", array[1]);------>where array[1] =my_prog
......

so should i use an array that contain strings?
Dedalus is offline   Reply With Quote
Old 06-23-2009, 11:42 AM   #7
Kernel hacker
 
Join Date: Jul 2007
Location: Farncombe, Surrey, England
Posts: 15,686
You can not (without breaking lots of rules) store integers in a string as a long long [by the way, I think a PID will never be more than at most 32 bits, so I'm not sure why you feel you need to print it using %lld which is a 64-bit integer format - but that is beside the point].

So as tabstop says, you need to use an integer for pid, and strings for name and state.

Of course, if you do not need to use the PID for anything that requires it to be an integer (in other words, you simply want to report it back again), then there is little reason to actually convert the input to a integer - just use the string.

--
Mats
__________________
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
matsp is offline   Reply With Quote
Old 06-23-2009, 11:43 AM   #8
Registered User
 
Join Date: Oct 2008
Location: TX
Posts: 1,262
ITA with tabstop because if it's just for display then you simply pick each of the values with fscanf() and printf() them later.
If you want to process these values apart from printing them, then you need to think of a 2D array for storing them too.
itCbitC is offline   Reply With Quote
Old 06-23-2009, 11:47 AM   #9
Registered User
 
Join Date: Jun 2009
Posts: 31
ok.

I'll try and I will report the result.

thanks for answ.
D.
Dedalus is offline   Reply With Quote
Reply

Tags
array, string

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Pointer to array of string and Array of Pointer to String vb.bajpai C Programming 2 06-15-2007 06:04 AM
String issues The_professor C++ Programming 7 06-12-2007 09:11 AM
Program using classes - keeps crashing webren C++ Programming 4 09-16-2005 03:58 PM
Calculator + LinkedList maro009 C++ Programming 20 05-17-2005 12:56 PM
Quick question about SIGSEGV Cikotic C Programming 30 07-01-2004 07:48 PM


All times are GMT -6. The time now is 10:20 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22