C++ FLTK displaying a sysctl value

OK you ignored me long enough I think I finally got what *fp meant.

I think in this case it means "file pointer". Remember that in Unix, everything is a file. So the sub-process you just launched is a file. If you read from it, you get its output to stdout. If you write to it, it gets what you wrote in stdin.
 
I was wondering in my head why all the coders here were making examples that were overly complicated. Strip away all that error handling so I can see whats happening.

I see why from the code above.
IX. DEFENSIVE CODING TECHNIQUE
 
Remember that in Unix, everything is a file. So the sub-process you just launched is a file. If you read from it, you get its output to stdout. If you write to it, it gets what you wrote in stdin.
OK so FILE in this programs context means an IO Stream. Could be either direction. In or Out

That clears up that line for me. Thank You.
 
I really enjoy the professors zeal when he names first homework assignment= homework0

I love the book for that course
Pointers are one of the most feared things in the C language. In fact, they are the one thing that makes this language challenging at all. But why?

Because they, quite honestly, can cause electric shocks to come up through the keyboard and physically weld your arms permanently in place, cursing you to a life at the keyboard in this language from the 70s!

Really? Well, not really. I’m just trying to set you up for success.
 
The above document allowed me to strip the onion layers back some. I have a few more questions.

Code:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#define MAX_BUFFER_SIZE 100
#define COMMAND0 "sysctl -n dev.cpu.0.temperature"
#define COMMAND1 "sysctl -n dev.cpu.1.temperature"
#define COMMAND2 "sysctl -n dev.cpu.2.temperature"
#define COMMAND3 "sysctl -n dev.cpu.3.temperature"

int main(int argc, char ** argv)
{
FILE *fp;
char result_buffer0[MAX_BUFFER_SIZE];
memset(result_buffer0, 0, MAX_BUFFER_SIZE);

fp = popen(COMMAND0, "r");
if (fp == NULL)
    /* Handle error */;

while (fgets(result_buffer0, sizeof(result_buffer0), fp) != NULL)
  printf("System Command Output Stored in String: %s \n", result_buffer0);

pclose(fp);
}
c++ -v -o sc sc.cxx -I/usr/local/include -L/usr/local/lib

While building extensions onto my new gem I discovered I need to stop for a bit. Ponder expansion.
Just adding/suffixing numbers to separate buffers and functions might not be ideal.
 
Well its code review time. Does this skeleton pass muster for coding? I do not like the naming convention but it outputs what I expect.

C++:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#define MAX_BUFFER_SIZE 100
#define COMMAND0 "sysctl -n dev.cpu.0.temperature"
#define COMMAND1 "sysctl -n dev.cpu.1.temperature"
#define COMMAND2 "sysctl -n dev.cpu.2.temperature"
#define COMMAND3 "sysctl -n dev.cpu.3.temperature"

int main(int argc, char ** argv)
{
FILE *fp0;
char result_buffer0[MAX_BUFFER_SIZE];
memset(result_buffer0, 0, MAX_BUFFER_SIZE);

FILE *fp1;
char result_buffer1[MAX_BUFFER_SIZE];
memset(result_buffer1, 0, MAX_BUFFER_SIZE);

FILE *fp2;
char result_buffer2[MAX_BUFFER_SIZE];
memset(result_buffer2, 0, MAX_BUFFER_SIZE);

FILE *fp3;
char result_buffer3[MAX_BUFFER_SIZE];
memset(result_buffer3, 0, MAX_BUFFER_SIZE);



fp0 = popen(COMMAND0, "r");
if (fp0 == NULL)
    /* Handle error */;

while (fgets(result_buffer0, sizeof(result_buffer0), fp0) != NULL)
  printf("System Command Output Stored in String0: %s \n", result_buffer0);

pclose(fp0);


fp1 = popen(COMMAND1, "r");
if (fp1 == NULL)
    /* Handle error */;

while (fgets(result_buffer1, sizeof(result_buffer1), fp1) != NULL)
  printf("System Command Output Stored in String1: %s \n", result_buffer1);

pclose(fp1);


fp2 = popen(COMMAND2, "r");
if (fp2 == NULL)
    /* Handle error */;

while (fgets(result_buffer2, sizeof(result_buffer2), fp2) != NULL)
  printf("System Command Output Stored in String2: %s \n", result_buffer2);

pclose(fp2);


fp3 = popen(COMMAND3, "r");
if (fp3 == NULL)
    /* Handle error */;

while (fgets(result_buffer3, sizeof(result_buffer3), fp3) != NULL)
  printf("System Command Output Stored in String3: %s \n", result_buffer3);

pclose(fp3);

}

From here I should have no problems adding FLTK back and adding new boxes..
 
C:
#include <stdio.h>
#include <stdlib.h>

int main()
{
    char buffer[100];
    char cmd[60];
    int i;
    FILE *fp;

    for (i = 0; i < 4;i ++) {
        sprintf(cmd, "sysctl -n dev.cpu.%d.temperature", i);
        fp = popen(cmd, "r");
        if (!fp) exit(2);
        while (fgets(buffer, sizeof(buffer), fp) != NULL)
            printf("System Command Output Stored in String1: %s \n", buffer);
         fclose(fp);
    }
    return 0;
}
 
I gotta follow thru with the lunkhead mode. Almost got my 4 boxes in position. Then optimization.Thanks so much for the help.
sc.jpg
 
printf("System Command Output Stored in String1:
The problem is as Alains comments suggest. Everything is stored in one string here.

Code:
System Command Output Stored in String1: 41.0C
 
System Command Output Stored in String1: 41.0C
 
System Command Output Stored in String1: 44.0C
 
System Command Output Stored in String1: 40.0C

I need the separate results_buffers right? I like the different boxes for future reuse..

Code:
_SNIP_
pclose(fp3);


result_buffer0[sizeof(result_buffer0)-4]=0;
box0->value(result_buffer0); // THE BOX HAS NOW AS TEXT THE RESULT OF THE SYSCTL COMMAND
box0->labelsize(20);

result_buffer1[sizeof(result_buffer1)-4]=0;
box1->value(result_buffer1); // THE BOX HAS NOW AS TEXT THE RESULT OF THE SYSCTL COMMAND
box1->labelsize(20);

result_buffer2[sizeof(result_buffer2)-4]=0;
box2->value(result_buffer2); // THE BOX HAS NOW AS TEXT THE RESULT OF THE SYSCTL COMMAND
box2->labelsize(20);

result_buffer3[sizeof(result_buffer3)-4]=0;
box3->value(result_buffer3); // THE BOX HAS NOW AS TEXT THE RESULT OF THE SYSCTL COMMAND
box3->labelsize(20);


window->end();
_SNIP_
 
I really value your feedback. Sorry for being such a bonehead. I like learning all different ways. I will get there.
libgpio will require it.

also why these lines ?
result_buffer2[sizeof(result_buffer2)-4]=0;
I think these were to strip out the bogus characters.
I am elated to have gotten this far. They don't bother me.
They don't see to be working either.

I am really getting close to moving to Jose 's sysctlbyname example. I wanted to get a grasp of Alain De Vos example first.

Thanks everyone for the patience. I updated the graphic.
 
Back
Top