2

I am trying to write a C code under UNIX to read the third word from each line of a text, and store it to a string by using POPEN. However my code is giving me an error (Modifiable lvalue required for assignment operator) at the line inside my while loop. Here is my code:

    int main() {

int license = 0;
char number[100];


FILE *file = popen("grep User results_today.TXT_05012013 > filename", "r");
if ( file != NULL)
{
    char line [128];
    while (fgets(line, sizeof line, file) != NULL)
    {

        number = popen("cut -f3 -d' '", "r");

    }
    fclose (file);

    printf("Hello %s\n", number);
}

I know there are a few errors on here as i am still kinda new to C. But please help me correct them, thanks!

3 Answers 3

2
FILE *file = popen("grep User results_today.TXT_05012013 > filename", "r");

This will run a grep command looking for User and redirect the output to the file filename. It will return a FILE * that allows you to read the output of this command, but as that output has been redirected, you won't get anything.

popen("cut -f3 -d' '", "r");

This will run the cut command which, as it has no file arguments, will read from stdin and write to stdout which can be read by the FILE * that popen returns, but which you aren't doing anything with.

You probably want something more like:

char line[128];
int number;
FILE *file = popen("grep User results_today.TXT_05012013 | cut -f3 -d' '", "r");
if (file) {
    while (fgets(line, sizeof line, file)) {
        if (sscanf(line, "%d", &number) == 1) {
            printf("It's a number: %d\n", number);
        }
    }
    pclose(file);
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for your help. So it looks like your code will run the grep User and cut command on the text file and output to FILE *file, then use the while loop to output to number? How come when i run your code it doesn't give me anything? Not even the "It's a number" part... am i missing something?
1

You assign the result of popen to a fixed size char array. This is not possible.

number = popen("cut -f3 -d' '", "r");

Do it like the first popen -> assign it to FILE *file2

4 Comments

he should figure that out by himself ;)
By the way, getdelim is a handy way to read the entire input.
@MircoEllmann Thanks, I kinda guessed that would be the problem. So now that i have a file called "filename" which i previously used grep User command to extract the info i wanted, is there a different way in using that while loop from my code to extract let's say the 3rd word from each line of the file "filename"? Thanks.
You might try to combine the commands with | grep User results_today.TXT_05012013 | cut -f3 -d' ' You could save the second popen that way. give it a shot...
1

First of I'm not a C programmer This is my implementation (with a lot of borrowed lines of course ;) ). I was just fed up of

popen
while fgets
 printf // I want to store in char* got it?

So here's the code. It may not be perfect but does the job :)

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

#include <string.h>

char* concatenate(char * dest, char * source) {
    char * out = (char *)malloc(strlen(source) + strlen(dest) + 1);

    if (out != NULL) {
            strcat(out, dest);
            strcat(out, source);
    }

    return out;
}

char * executeCmd(char * cmd) {
    FILE *fp;

    int BUFF_SIZE = 1024;

    int size_line; 
    char line[BUFF_SIZE];

    char* results = (char*) malloc(BUFF_SIZE * sizeof(char));

    if (cmd != NULL) {
            /* Open the command for reading. */
            fp = popen(cmd, "r");
            if (fp != NULL) {

            /* Read the output a line at a time - output it. */
            while (fgets(line, size_line = sizeof(line), fp) != NULL) {
                    results = concatenate(results, line);
            }
            }
            /* close */
            pclose(fp);
    } // END if cmd ! null

    return results;
}


int main( int argc, char *argv[] ) {
    char * out = executeCmd("ls -l");
    printf("%s\n", out);

    return 0;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.