LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   c code to login to ftp server while reading all login info from a file (https://www.linuxquestions.org/questions/programming-9/c-code-to-login-to-ftp-server-while-reading-all-login-info-from-a-file-4175412185/)

yahoosam 06-19-2012 01:34 AM

c code to login to ftp server while reading all login info from a file
 
i am urgently in need of a c code for my B.Tech. last sem project, while will read ftp server login info like IP,user name,password from a file and log in to ftp server...
gotta urgent

pan64 06-19-2012 01:59 AM

do you have a test code or something to start with?









_____________________________________
If someone helps you, or you approve of what's posted, click the "Add to Reputation" button, on the left of the post.
Happy with solution ... mark as SOLVED
(located in the "thread tools")

yahoosam 06-19-2012 04:58 AM

i just wrote a program to just read a file as the file will store all the necessary inputs for login like IP,User,Pass.
and stored in structure but dont know to which should i pass these arguments for login.i already have created an FTP server by installing "VSFTPD package"..can u assist on this.

pan64 06-19-2012 06:05 AM

can you show the code you have written?

yahoosam 06-19-2012 07:23 AM

its just reading program from the file nothing much..
structure wise
#include /*Header files*/
typedef struct
{
char ip;
char user;
char pass;

}info;

main()
{
FILE *fp;/*file to read details*/
info *data=NULL;
a=data->ip;
b=data->user;
c=data->pass;
/*Required method to pass these arguments*/

methodLogin(a,b,c);/*At this point i need assistance for my semester project as i thought to do*/

return 0;
}

pan64 06-19-2012 10:43 AM

yes, you need to open file and parse data. here is a little example on how to open and read a file using fopen and fscanf. Can you fill up the ip, user and pass variables?

yahoosam 06-20-2012 02:14 AM

i did writing & modification for the parse program as here..
so what further in case the login to FTP commented below!!


#include <stdio.h>
//#include <io.h>
#include <stdlib.h>
#include<string.h>
typedef struct
{
char ip[16];
char user[16];
char pass[16];
}info;


int main(void)
{
FILE *fp;
/*
char ip[16];
char user[16];
char pass[16];
int t;
*/
info *value=NULL;
value=(info *)malloc(sizeof(info));
memset(value,0,sizeof(info));
if((fp=fopen("test", "w")) == NULL) {
printf("Cannot open file.\n");
exit(1);
}

printf("Enter IP,USER,PASS: ");
fscanf(stdin, "%s %s %s",value->ip,value->user,value->pass); /* read from keyboard */

fprintf(fp, "%s %s %s", value->ip,value->user,value->pass); /* write to file */
fclose(fp);

if((fp=fopen("test","r")) == NULL) {
printf("Cannot open file.\n");
exit(1);
}

fscanf(fp, "%s %s %s", value->ip,value->user,value->pass); /* read from file */
fprintf(stdout, "IP:%s\nUSER:%s\nPASS:%s\n\n", value->ip,value->user,value->pass); /* print on screen */


/*FTP LOGIN*/
FtpLogin(value->ip,value->user,value->pass);

return 0;
}

pan64 06-20-2012 02:36 AM

There are some problems with your code, but first: please use [code][/code] to keep formatting your code.

1. probably 16 bytes are not enough to store ip/user/pass. In case of overflow your app may die with a segmentation fault or similar, or will destroy some data inside.
2. you did not check the result of fscanf.
3. here is a simple ftp client: http://enggedu.com/lab_exercise/netw...FTP_client.php. With this you can connect to a server.
4. you can try to log in, send commands and receive response.




_____________________________________
If someone helps you, or you approve of what's posted, click the "Add to Reputation" button, on the left of the post.
Happy with solution ... mark as SOLVED
(located in the "thread tools")

yahoosam 06-20-2012 06:32 AM

pan64:Actually i m in need of some FTP API functions which will logon with my attributes,send files(PUT),recieve files{GET},and then exits.thats my idea of project..
now can u assist me on that as at this level m not vrt good at Googling..
--
Respond

pan64 06-20-2012 07:41 AM

here is an api, but you need to compile it. I assume it will not cause any problem.
Also a collection: http://curl.haxx.se/libcurl/competitors.html. Libcurl has a few example also: http://curl.haxx.se/libcurl/c/ftpupload.html





_____________________________________
If someone helps you, or you approve of what's posted, click the "Add to Reputation" button, on the left of the post.
Happy with solution ... mark as SOLVED
(located in the "thread tools")

yahoosam 06-22-2012 12:50 AM

Thanx vry much!
i m going through all the APIs n got stuck in one argument.i m giving u the url of function and argument i stuck with.

function URL:
http://nbpfaus.net/~pfau/ftplib/FtpConnect.html

Problem:
int FtpConnect(const char *host, netbuf **nControl);
what to pass here for "netbuf **nControl"
--

pan64 06-22-2012 01:21 AM

that is written on the page: nControl is the return value, you will get a handle which can be used to initiate data transfers.

yahoosam 06-22-2012 01:41 AM

i got that point.

but if i call like this,giving only one argument,it gives error like
Code:

/*
if(!FtpConnect(value->ip))
{
printf("Error in connection");

}

Error:
parse.c: In function ‘main’:
parse.c:52: error: too few arguments to function ‘FtpConnect’
*/

so wat structure ref should i pass there..
will u please write that function with its passing arguments.with that argument declaration.
coz for last 2hrs i am searching for "Arguments for FtpConnect" in google and result is ZERO
--

pan64 06-22-2012 01:50 AM

that is written, you do not understand: you must give a second argument, which is a pointer:
Code:

netbuf *nControl;
if(!FtpConnect(value->ip, &nControl))
{
    printf("Error in connection");
}
// this nControl will identify your connection and will be used as a handle in the subsequent calls you will make...


yahoosam 06-22-2012 06:25 AM

just perfectly fine and running successfully..
jst got stuck in sending & receiving files..Actually program halts at some point after entering to the function FtpGet(...)
Putting Rough structure in here,have a look on FtpGet() function plz
giving you details..
Code:

if(FtpConnect(...))
{
  if(FtpLogin(...))
  {
  if(FtpGet("abc.c","/home/amit/ftp/llllll.c",FTPLIB_ASCII,nControl))/*Take an Eye in here*/
  /*About FtpGet()
    http://nbpfaus.net/~pfau/ftplib/FtpGet.html
  */
    {
    printf("Sent successfully");
    }else{ printf("can't send"); exit(0);}
  }else{ printf("can't Login"); exit(0);}
}else{ printf("can't Connect"); exit(0);}



All times are GMT -5. The time now is 10:02 AM.