How to take input from cmd line into file


 
Thread Tools Search this Thread
Top Forums Programming How to take input from cmd line into file
# 1  
Old 02-11-2010
How to take input from cmd line into file

Hi, I want to be able to write a simple program that takes in input from the command line. I;m am at the level of getchar and putchar. Any examples would be a great help thanks.


I intend/prefer also to use the pipe command | eg: input | file1

---------- Post updated at 04:08 PM ---------- Previous update was at 04:01 PM ----------

C is the programming language I am using
# 2  
Old 02-11-2010
man popen

simple example:

Code:
 
FILE *pfp;
if((pfp = popen("ls", "r")) != NULL)
{
    while(fgets(bufffer, sizeof(buffer)-1, pfp))
    {
        printf("I read: %s\n", buffer);
    }
    pclose(pfp);
}

# 3  
Old 02-19-2010
Hope that this is the simplest input output program in C , using getchar and putchar

Code:
#include<stdio.h>
main()
{
        int c ;
        while((c=getchar())!=EOF)
                putchar(c);
}

# 4  
Old 02-19-2010
For what its worth, check out the readline library. It is somewhat more work than getc/putc, but in many many cases its very much worth the effort: you get command line editing, command line history, key bindings, etc etc, and the API is not that difficult to use:

Code:
  char line[MAX_LINE_LEN];

  line = readline ("prompt > ");
 
  /* If the line has any text in it, save it on the history. */
  if (line && *line)
      add_history (line);

The GNU Readline Library has all the details you want, and then some Smilie
# 5  
Old 02-26-2010
Bug

Hi,

Using following program you can get the input from the command line
and print that line in stdout.

Code:
#include<stdio.h>
#define BUFSIZE 1024

main()
{
        char string[BUFSIZE];
        scanf(" %[^\n]",string);
        printf("STRING:%s\n",string);

}

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. How to Post in the The UNIX and Linux Forums

Read a json file passed as cmd line argument

usage: myscript.sh config.json config.json: { "HOST":"abc", "DB_NM":"xyz", "USR_NM":"asd", "PWD":"xxx", ......... ......... ......... ........ } myscript.sh: (2 Replies)
Discussion started by: RGRT
2 Replies

2. Programming

C++ Input File line by line

Hi there, I need to read some data from a file with string and number that is similar to this: word1 0.0 1.0 0.0 word3 word4 0.0 0.0 -1.0 word1 0.0 0.0 1.0 word5With this code: #include<iostream> #include<fstream> #include<string> using namespace std; int main() ... (5 Replies)
Discussion started by: Giordano Bruno
5 Replies

3. Shell Programming and Scripting

Curl - input line by line from text file

Hi, I've got a text file with hundreds of lines I need to upload to an API via curl, one by one. The text file is like: 2012-08-01 10:45,124 2012-08-02 10:45,132 2012-08-03 10:45,114 I want to get curl to go through the text file sending a post for each line. like: curl --request... (0 Replies)
Discussion started by: emdeex
0 Replies

4. UNIX for Dummies Questions & Answers

passing input into a cmd line that's waiting for a response

Hi, I am writing a little script to update a parameters on JMQ. however the JMQ requires a "y" confirmation to be input as part of the cmd I am running. However I want run this script to offline with no input from a user. it works if a I create a file with with just y in it and pass that in... (3 Replies)
Discussion started by: shropshirehobbi
3 Replies

5. Shell Programming and Scripting

run cmd based on input

Hi, This is what I have so far but it seems like a lot more than is necessary because....for example...user presses 2 or 3 ..... the script does the *same* thing it just depends on the directory it has to access....how can I improve this so that the code from 2 and 3 is only put once...? ... (4 Replies)
Discussion started by: holyearth
4 Replies

6. UNIX for Dummies Questions & Answers

input URL into cmd with a alias

I want to run wget "URL" -SO /dev/null 2>&1 | grep "HTTP/\\|Age:\\|Last-Modified:" but I want a alias so I can just type mywget and the URL and it will put the url in the right place and give me the output that I want without having to type that over and over again.:wall: I am newbie to all... (2 Replies)
Discussion started by: splitradius
2 Replies

7. Shell Programming and Scripting

How to input the return value (1 or 0) ping cmd to a variable

Hi I would like to ask about my plan script I have this several workstation want to monitor and execute a command without logging it we use "rsh $host "<command>" i create csh script using foreach to loop my several workstation, my problem with the rsh command is if it encounter a... (3 Replies)
Discussion started by: jao_madn
3 Replies

8. Shell Programming and Scripting

sed command works from cmd line to standard output but will not write to file

Hi all .... vexing problem here ... I am using sed to replace some special characters in a .txt file: sed -e 's/_<ED>_/_355_/g;s/_<F3>_/_363_/g;s/_<E1>_/_341_/g' filename.txt This command replaces <ED> with í , <F3> with ó and <E1> with á. When I run the command to standard output, it works... (1 Reply)
Discussion started by: crumplecrap
1 Replies

9. Shell Programming and Scripting

sed to read line by line and input into another file

I have two files. Fileone contains text string one text string two text string three Filetwo contains Name: Address: Summary: Name: Address: Summary: Name: Address: Summary: I would like to use sed to read each line of file one and put it at the end of the summary line of file... (3 Replies)
Discussion started by: dolacap
3 Replies

10. Shell Programming and Scripting

ksh: cmd output to input of another script

I want to write a script in KSH that takes the output of one command and redisplays it. Something like: while true do read inpt date +"%I:%M:%S %p <-> $inpt" done and then some how get the output of the ping command to redirect to the input of this script. does that make sense? (2 Replies)
Discussion started by: IMTheNachoMan
2 Replies
Login or Register to Ask a Question