Thread: file contents as a parameter to a program

  1. #1
    Registered User
    Join Date
    Dec 2004
    Posts
    7

    file contents as a parameter to a program

    How do you pump the contents of a file as an parameter to a program in Windows?

    I have a program that will count the number of words in a string. The program accepts this string as a parameter like this:

    WordCount "this is a string"

    If I wanted to put the string in a file and have the file contents dumped to the program as that string parameter, what do I have to do?

    Thanks.

  2. #2
    Registered User xxxrugby's Avatar
    Join Date
    Jan 2005
    Posts
    178
    Can you explain what you want for me without words like dumped and pump!
    I am not very good with English! But I just can't understand what you want.
    I take dictionary but I just can't get the meaning of the sentence!

    example
    fprintf /* write into file
    Code:
    /* fprintf example */
    #include <stdio.h>
    
    int main ()
    {
       FILE * pFile;
       int n;
       char name [100];
    
       pFile = fopen ("myfile.txt","w");
       for (n=0 ; n<3 ; n++)
       {
         puts ("please, enter a name: ");
         gets (name);
         fprintf (pFile, "Name %d [%-10.10s]\n",n,name);
       }
       fclose (pFile);
    
       return 0;
    }
    Sorry for spelling errors, not English!
    xxxrugby: "All Human Race Will Die From My Hand!"
    xxxrugby: "We are all philosophers, when question is about politics!"

  3. #3
    Registered User
    Join Date
    Dec 2004
    Posts
    7
    On a Linux machine, you can do something like this:

    WordCount 'cat filename.txt'

    and it will read the contents of filename.txt into your program as a string.

    I'm looking for the Windows equivalent.

    I tried: WordCount 'more filename.txt' but it gets read in as a string, meaning that the program reads 'more filename.txt' instead of the contents of the file.

    Does that make more sense?

    Thanks.

  4. #4
    Handy Andy andyhunter's Avatar
    Join Date
    Dec 2004
    Posts
    540
    Basically, you would pass the filename to your program as a command line parameter. This is the FAQ Command Line Parameters .

    Then you would open the file in your program designated by the command line parameter and count the number of char present. You could implement this with a while loop.
    i don't think most standard compilers support programmers with more than 4 red boxes - Misplaced

    It is my sacred duity to stand in the path of the flood of ignorance and blatant stupidity... - quzah

    Such pointless tricks ceased to be interesting or useful when we came down from the trees and started using higher level languages. - Salem

  5. #5
    Registered User Scribbler's Avatar
    Join Date
    Sep 2004
    Location
    Aurora CO
    Posts
    266
    Actually in linux, WordCount 'cat filename.txt' would execute a program named WordCount and pass the string 'cat filename.txt' as an argument to the program WordCount to process internally.

    I think what you're thinking about is cat filename.txt | wc which takes the output of cat (which in turn is outputting the contents of filename.txt) and instead of sending it to the normal output device, connects the output of cat to the input of wc (which is the wordcount program). The program itself doesn't really care whether the input is coming from the keyboard or from another source... all it knows is it's receiving input when it expects it.

    Unless I'm mistaken (not at a windows machine at the moment) Windows/MSDos also supports piping in the same manner as *nix.
    Last edited by Scribbler; 02-13-2005 at 09:26 PM.

  6. #6
    Registered User Scribbler's Avatar
    Join Date
    Sep 2004
    Location
    Aurora CO
    Posts
    266
    Verified it on a windows machine...

    Piping does work in windows as well, you just have the syntax wrong. You connect output of one process to the input of the next process with the pipe '|' symbol.

    So try type filename.txt | WordCount.

    As an example you can take the 2 following progs...

    prog1.c
    Code:
    #include <stdio.h>
    
    int main()
    {
        printf( "This is a test string" );
        
        return 0;
    }
    prog2.c
    Code:
    #include <stdio.h>
    
    int main()
    {
        char inputString[80];
        int count = 0;
        
        do{
            scanf ("%s", inputString);
            printf ("String %d\n%s\n" , ++count, inputString );
        } while ( getchar() != EOF );
        
        return 0;
    }
    compile them into the same directory, then execute prog1 | prog2. You'll see that the printf() in prog1 doesn't go to the screen. Instead it's connected to the input of prog2, which then processes the string from prog1, reformats it, and then outputs it to the screen.

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Command line parameters + fopen + fread/fgets/fscanf/fetcetcetc functions = possible solution.
    Command line parameters + pipes + do it yourself inside the program = another possible solution.
    OS handled piping + reading from stdin inside the program = yet another possible solution.

    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    .
    Join Date
    Nov 2003
    Posts
    307
    One of the accepted standards for command line tools in unix - a 'decent' program will either:

    read from stdin or
    open a file passed in as a parameter and read from it.

    Windows isn't a command line environment. The above used to be true in DOS.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. opening empty file causes access violation
    By trevordunstan in forum C Programming
    Replies: 10
    Last Post: 10-21-2008, 11:19 PM
  2. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 04:54 PM
  3. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 12:39 PM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM