Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Unix System Call from C

Options
  • 29-11-2006 7:35pm
    #1
    Closed Accounts Posts: 97 ✭✭


    Hi folks,

    Looking for some help with calling a unix function (specifically wc) from inside a c program. The C code is below. It should input one file or text, output to another and using unix wc command, I want to count lines, words, chars, etc.

    I've just made the command below "CLS" as I don't have a unix machine here so trying to get it running in DOS and can then port accross.

    Basically, I cannot find out how to actually call the unix wc command. I've put in the line below as:
    system ("CLS"); - and would replace the CLS with WC if I can get it to work. Anyone any ideas? I'm having no luck on the web.

    Cheers lads.


    ===========
    /* System Call to Determine lines, words and characters */

    #include <stdio.h>
    #include <process.h>
    #define LINESIZE 100

    void main(void)
    {

    char one_line[LINESIZE];
    char *command = "dir";
    FILE *fp_in, *fp_out;

    /* Opens file in Read Only, error if file not there */

    if ( (fp_in=fopen("datafile.txt", "r"))==NULL)
    puts("Error opening datafile.txt");
    else if ( (fp_out=fopen("newfile.txt", "w"))!=NULL)
    {

    /* Starts loops and puts input file to output file */

    while (fgets(one_line,LINESIZE,fp_in)!=NULL)
    fputs (one_line,fp_out);


    fclose(fp_in);
    fclose(fp_out);
    puts ("Done");
    }
    else
    puts("Error opening newfile.txt");

    /* System Call to Operating System */

    system( "cls" );
    system( command );

    }


Comments

  • Closed Accounts Posts: 97 ✭✭VroomVroom


    Forgot to say that the rest works, if I comment out the system call function, the rest of the code does what it is supposed to do, take the first file and copy to newfile.

    Cheers.


  • Closed Accounts Posts: 82 ✭✭cyberbob


    i put this together in a coupla secs , seems to work
    writes wc to another file which you need to read in , im sure theres a way of getting the output of wc more directly.....

    #include <stdio.h>
    #include<stdlib.h>
    int main()
    {
    system("wc file > out ");
    }
    


  • Closed Accounts Posts: 97 ✭✭VroomVroom


    That'll do, thanks a million!


Advertisement