简体   繁体   中英

How to execute 2 sucessive system command in c using system() like connecting to ftp and logging in using program?

I want to use the ftp command using system function in c. I need to execute 3 commands.

1 FTP
2 USERNAME
3 PASSWORD

how do i perform the above 3 using system().... i am stucked up here
system(ftp);

You might want to check if your version of ftp allows you to specify the user name and password on the command line (some do). Of course, you could also implement ftp either on your own (using sockets directly), or via a library (there are several around).

Otherwise, if you really want to run the system's ftp command, you probably want to use popen (or, on Windows, _popen ) instead.

FILE *cmd = popen("cmd", "w");

if (NULL != cmd)
    fprintf(cmd, "%s\n%s", user_name, password);

What you write to the FILE * goes to the standard input of the child process (and if you open with "r" instead of "w" , you can read from the child's standard output). Many (but not all) recent versions of popen also support "rw" , so you can write to the child's standard input and read from its standard output.

Note, however, that this will only work for a child process that reads from its standard input. Some use other methods to read directly from the console/keyboard, and it won't (necessarily) work with those.

I recommend you using some other API instead of system(). System() is considered as non standard way for supplying commands. I would suggest use only FTP command for opening that terminal and then use ftp apis overs winsock api's.

These are few library I know. please use if,

libftp 
ftplib 

enough documentation can be found in web for this. Please let me know if this answers your question.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM