To import the output of another program as input when programming

Asked 2 years ago, Updated 2 years ago, 25 views

It's a program that throws a number at the bottom. The file name is getHits.sh.

#!/bin/bash
# # Print 32 bit random number
UPPERBIT=$RANDOM
LOWERBIT=$RANDOM
UPPERBIT=$(($UPPERBIT<<16))
SLEEPTIME=$(($RANDOM % 10))
SLEEPTIME=$(echo $SLEEPTIME/10 | bc -l)
sleep $SLEEPTIME
echo tsf0 : 0x1080 : $(($UPPERBIT+$LOWERBIT))

You can run this command by typing it on the terminal.

chmod +x getHits.sh
./getHits.sh

Then the result comes out as below.

tsf0 : 0x1080 : 181882363

I'm going to program this number as an input, and I have to create a function that takes a constant number of inputs per hour and averages them. From the start, I'm stuck with a difficulty with that result. I don't mind writing it, but I'm thinking of doing it in cpp.

c++

2022-09-21 21:35

3 Answers

When I googled, I think I can use popen function. Unix can transfer output from a program to a standard output device as input from another program, which is called a pipe. (Source: falinux form)

Using the example from the link above,

#include <stdio.h>

#define BUFF_SIZE 10

int main(void) {

    char buff[BUFF_SIZE];

    FILE *filepipe;

    filepipe = _popen( "./getHits.sh", "r");

    if (NULL == filepipe) {

error ("popen failed");

        return -1;

    }

    while( fgets(buff, BUFF_SIZE, filepipe) )

        printf("%s", buff);

    _pclose(filepipe);

    return 0;

}

I think you can do it with that. Click the links in the function/macro/variable to navigate to the appropriate topics in the Microsoft Visual Studio C++ documentation.

For your information, Windows says that popen and pclose should be replaced with _popen and _pclose, and there may be an exception that the pipe does not close properly, so you may need to use an additional try-catch statement. (Source: StackOverflow)


2022-09-21 21:35

http://tldp.org/LDP/lpg/node7.html

Connect the link to learn.


2022-09-21 21:35

I'm a bit harsh, but what about this? First of all, getHits.Modify the last line of sh as follows.

echo $(($UPPERBIT+$LOWERBIT))
# # echo tsf0 : 0x1080 : $(($UPPERBIT+$LOWERBIT))

Using the system function and the '>' command on the terminal, Save the output of the shell script as a txt file Using the file input/output function of language c, we reread the contents stored in the txt file. Here's the code we've made.

#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>

#define ITERATION 3

int main(int argc, char **argv)
{
    int intinput;
    FILE *fp;
    char input[32];

    for(int i = 0; i<ITERATION;i++)
    {
        if(system("/home/hansle/getHits.sh > /home/hansle/getHits.txt") != 0)
            return -1;
        fp=fopen("/home/hansle/getHits.txt","r");
        fscanf(fp,"%d",&intinput);
        printf("%d: %d \n",i+1,intinput);
        fclose(fp);
    }
    return 0;
}

I don't know; I don't have to write the output hard and read what I wrote on the hard again;; Even if the performance is poor, I think it will be able to perform the function.

This is the added-output result

1: 1671002997 
2: 1552024643 
3: 1917608459 


2022-09-21 21:35

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.