LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-14-2010, 04:55 AM   #1
aa.bb.cc
LQ Newbie
 
Registered: Jul 2010
Posts: 10

Rep: Reputation: 0
problem with a function defination


hey
this is my main.c...
Code:
#include "allheader.h"
#include "ftp.h"



int main(int argc, char* argv[])
{	
	string buf = "STRING TO BE UPLOADED INTO THE SERVER";
	bool answer;
	string user = "ftpu";
	string pass = "ftppwd";
	string server = "172.32.100.160";
	string filename = "myfile.txt";
	answer = ftp_write(buf, server, user, pass, filename);
	return -1;

}
this is my ftp.h ......


Code:
#include "ftplib.h"

bool ftp_write(std:: string  data, std:: string server, std:: string user, std:: string pass, std:: string filename)
{		
		int m_con;	
		int m_log;	
		int m_acs;				
		int m_wrt;
		int m_cls;
		netbuf *p_buf;
		netbuf *p_nhandler;
		
		
		//connecting to an ftp server.
		//returns 1 if the connection is successful
		m_con = FtpConnect(server.c_str(), &p_buf);	
		if (m_con != 1) {
		cout<<"cant connect to the server. not a valid ftp address"<<endl;
		return false;
		}
		//loging in to the server with the credentials.
		//returns 1 if the login is successful
		m_log = FtpLogin(user.c_str(), pass.c_str(), p_buf);
		if (m_log != 1) {												
		cout<<"incorrect username or password. access denied"<<endl;
		return false;
		}
		//opens the remote file passed as the command line argument										
		//returns 1 if file is successfully opened
		m_acs = FtpAccess(filename.c_str(), FTPLIB_FILE_WRITE, 
	     				  FTPLIB_IMAGE, p_buf, &p_nhandler);		
		if (m_acs != 1) {												
		cout<<"not accessible"<<endl;
		return false;
		m_wrt = FtpWrite((void*)data.c_str(),data.size(), p_nhandler);  
		if (m_wrt == -1) {								
		cout<<"error occured on writting"<<endl;
		return false;
		}
		return true;

		m_cls = FtpClose(p_nhandler);
		if (m_cls != 1) {		
		cout<<"error in closing"<<endl;
		return 0;
		}
	FtpQuit(p_buf);	
}

after compiling i am getting the following error.......

main.c: In function 'bool ftp_write(std::string, std::string, std::string, std::string, std::string)':
main.c:7: error: a function-definition is not allowed here before '{' token
main.c:17: error: expected `}' at end of input
any help appreaciated.......
 
Old 07-14-2010, 05:48 AM   #2
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
You are missing a } bracket in this area:
Code:
...
		if (m_acs != 1) {												
		cout<<"not accessible"<<endl;
		return false;
		m_wrt = FtpWrite((void*)data.c_str(),data.size(), p_nhandler);  
		if (m_wrt == -1) {								
		cout<<"error occured on writting"<<endl;
		return false;
		}
		return true;

		m_cls = FtpClose(p_nhandler);
		if (m_cls != 1) {		
		cout<<"error in closing"<<endl;
		return 0;
		}
If you would indent better, it would be easier to see your mistake. Consider writing code similar to the following:
Code:
if (condition) {
    indent statement(s)
}

// or (and this is my preference)

if (condition)
{
    indent statement(s)
}
 
Old 07-15-2010, 12:34 AM   #3
zirias
Member
 
Registered: Jun 2010
Posts: 361

Rep: Reputation: 59
Another thing to consider:

This is plain procedural code, using only an ISO C library, in files called *.c, and you STILL write C++, using e.g. the std::string class for nothing else than accessing the char* inside.

Try writing real C instead, use C++ for projects that actually USE its features. Reasons to do so:
- portability
- no symbol mangling (binary compatibility with libs)
- not confusing others who may work on the code later
- not carrying unneeded stuff in the final binary

I'm NOT AT ALL advocating against C++ here, I just hate to see C-code in C++

Oh and: Don't do implementation in header files. In your example.h file, just give the prototype like
Code:
int whatever(char *foo, int baz);
implement it in example.c, and put both, main.c and example.c in your gcc commandline.

Last edited by zirias; 07-15-2010 at 12:37 AM.
 
Old 07-16-2010, 07:28 AM   #4
aa.bb.cc
LQ Newbie
 
Registered: Jul 2010
Posts: 10

Original Poster
Rep: Reputation: 0
thanks a lot both of you!!
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Passing data from interrupt handler function to tasklet function in kernel programmin double-out Programming 2 05-18-2010 10:10 PM
[SOLVED] Threaded function cannot call a function with extern "C" but nonthreaded function can morty346 Programming 16 01-12-2010 05:00 PM
how to print function names & parmaters each time control enters the function? tanniru Linux - Networking 1 09-11-2008 01:21 AM
sigmatel high defination audio not working on RHEL 4.0 sachin_malik Linux - Hardware 0 08-03-2007 11:50 AM
how to install Realtek high defination audio driver in redhat linux 9.0 graphic mode blpradeep Linux - Hardware 2 07-15-2006 04:26 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:05 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration