Thread: Popen question

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    3

    Smile Popen question

    Hi, I have a question regarding the popen() function in C.

    I would like to open a text file and count the "the" words, and this would do the job:
    Code:
    FILE* fin;
    fin=popen("cat myfile.txt | grep -o -w 'the' |wc -w ","r");
    
    char nr[5];
    fgets(nr,sizeof nr,fin);
    puts (nr);
    
    pclose(fin);
    Now i would like to read the file name,"myfile.txt", from the standard input as a string and pass it to the popen() function.
    For example i would like to have something like:

    Code:
    char filename[20];
    gets(filename);
    FILE* fin;
    
    fin=popen("cat filename | grep -o -w 'the' |wc -w ","w"))==NULL);
    ...
    Is there any way i could do this?I know this doesn't work because Shell tries to open the file named 'filename'.

    Thanks

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    You could use sprintf to first create the string to pass as an argument to popen, but I wonder why you don't just write a shell script.

    By the way, you should avoid gets as it is vulnerable to buffer overflow. You could use fgets instead.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Nov 2010
    Posts
    3
    Thanks for the reply

    Well, I have to do a project using POSIX threads in C, each thread has to count the number of occurrences of the "the" word in a given file. I chose to do this with popen because it's easier and you don't have to code that much )

    Anyway, i tried using sprintf and it's basically the same deal:

    Code:
     char filename[20];
    ...
    //read filename
    ...
     char file_to_open[20];
     sprintf(file_to_open, "/home/me/Documents/ %s", filename);
    
     FILE *fin;
     fin=popen("cat file_to_open | grep -o -w 'the'|wc -w ","w")
    And then, when I execute it:
    cat: file_to_open: No such file or directory

    Am I missing something?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    I am thinking more of something like this:
    Code:
    char filename[FILENAME_MAX];
    ...
    //read filename
    ...
    
    char count_command[FILENAME_MAX + 50];
    sprintf(count_command, "cat /home/me/Documents/%s | grep -o -w 'the' |wc -w", filename);
    
    FILE *fin;
    fin = popen(count_command, "w");
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Nov 2010
    Posts
    3
    That does make sense! How could I have missed that!? :-))
    Thanks a lot *hug*

  6. #6
    Registered User
    Join Date
    Oct 2007
    Posts
    32
    I am use grep very intensive, but somehow never noted -w flag before.
    Cool.

    What never fail to astonish me, why so often used
    Code:
    "cat file | grep ...
    Instead of plain
    Code:
    grep ... file

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question bout my work
    By SirTalksAlots in forum C Programming
    Replies: 4
    Last Post: 07-18-2010, 03:23 PM
  2. A question about a question
    By hausburn in forum C++ Programming
    Replies: 3
    Last Post: 04-25-2010, 05:24 AM
  3. popen()
    By Cactus_Hugger in forum Windows Programming
    Replies: 2
    Last Post: 10-22-2005, 03:16 PM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM