Popen question

This is a discussion on Popen question within the Linux Programming forums, part of the Platform Specific Boards category; Hi, I have a question regarding the popen() function in C. I would like to open a text file and ...

  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
    21,397
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    21,397
    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");
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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

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