Get user commands from text file.

This is a discussion on Get user commands from text file. within the C Programming forums, part of the General Programming Boards category; I have a program that reads and executes commands from user. To test it well i was planning to write ...

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    35

    Get user commands from text file.

    I have a program that reads and executes commands from user.

    To test it well i was planning to write several commands in a text file and them send those commands one by one to the program.

    i've tried :
    Code:
    main(int argc, char *argv[]){
    char line[100];
    FILE * fp;
    sleep(10);
    fp = fopen("script.txt", "r");
    	if (fp == NULL){
    		perror("No file");
    	}else{
    		while(fgets(line, 100, fp)!=NULL){
    			usleep((atoi(argv[1]))*1000);
    		    fprintf(stdin,"%s", line);
    		}
    		fclose(fp);
    	}
    printf("End\n");
    }
    there is also a delay between each command to allow time for it's completion.
    Is this possible?to write to the stdin?
    i make it sleep for 10 seconds so i can run it in the backgound (&), and them run the main program in the same window, but nothing happens.
    It just prints out the End after the time as elapsed...

  2. #2
    Registered User
    Join Date
    Oct 2008
    Posts
    35
    i have also tried writer | reader ,(writing to stdout), but the behavior is strange .

    The messages are not delivered at the same speed as when the program writer is runned alone.

    Some are delivered a lot faster then the delay, and others are slower...
    Last edited by Ironic; 12-08-2008 at 08:23 PM.

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    You can just write commands to stdout, and then pipe the output of your program into the other program.
    Code:
    $ ./generate_commands | ./process_commands
    As for the delay -- you can't expect usleep to always delay the same amount. It will try to, but other factors might result in delays being longer than you counted for (for example, if some other program was hogging the CPU). It's a bit strange that there's a noticable delay, however.

    Oh, I think I know why -- perhaps your commands (written to stdout) aren't being flushed for some reason. Try this code just after you print each command:
    Code:
    fflush(stdout);
    Just remember: you can't print anything to stdin. That's an input stream. In this case (commands | processor), the stdout of your command-generating program actually becomes the same stream as the stdin of the program that processes the commands. Bah, that was a bad explanation, read some tutorials on your shell if you're interested in a better one. (Try googling "bash tutorial", for example.)
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #4
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,058
    Code:
    /* can't write to standard input */
    fprintf(stdin, "%s", line);
    and inserting a newline in the fprintf() ought to take care of the delay seen in the messages ie
    Code:
    fprintf(stdout,"%s\n", line);
    Last edited by itCbitC; 12-08-2008 at 10:56 PM.

  5. #5
    Registered User
    Join Date
    Oct 2008
    Posts
    35
    dwks's suggestion worked fine.

    tks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. disposing error
    By dropper166 in forum C# Programming
    Replies: 2
    Last Post: 03-31-2009, 12:53 AM
  2. Inventory records
    By jsbeckton in forum C Programming
    Replies: 23
    Last Post: 06-28-2007, 05:14 AM
  3. Batch file programming
    By year2038bug in forum Tech Board
    Replies: 10
    Last Post: 09-05-2005, 04:30 PM
  4. Need to report some things in a text file to the user...
    By Josh7289 in forum C++ Programming
    Replies: 2
    Last Post: 01-18-2005, 06:24 PM
  5. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 10:54 AM

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