LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   problem with a function defination (https://www.linuxquestions.org/questions/programming-9/problem-with-a-function-defination-819744/)

aa.bb.cc 07-14-2010 04:55 AM

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.......

dwhitney67 07-14-2010 05:48 AM

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)
}


zirias 07-15-2010 12:34 AM

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.

aa.bb.cc 07-16-2010 07:28 AM

thanks a lot both of you!!


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