c programming help!

c programming help!

Post by MonkeyNut on Thu Mar 05, 2009 8:47 pm
([msg=19386]see c programming help![/msg])

Hi i was just wondering would anyone be able to tell me why i'm getting the following errors?
I'm a novice at c and not sure what to do.

com.c:176: warning: passing argument 1 of ‘sscanf’ makes pointer from integer without a cast
com.c:178: warning: passing argument 1 of ‘sscanf’ makes pointer from integer without a cast
com.c:182: warning: passing argument 2 of ‘send’ makes pointer from integer without a cast

They relate to the while loop situated at the bottom of this code:

Code: Select all

#include <stdio.h>                                 /* for printf() and fprintf() */
#include <sys/socket.h>                            /* for socket(), connect(), send(), and recv() */
#include <arpa/inet.h>                             /* for sockaddr_in and inet_addr() */
#include <stdlib.h>                                /* for atoi() and exit() */
#include <string.h>                                /* for memset() */
#include <unistd.h>
                                 /* for close() */


#define BUFSIZE 4096                                 /* Size of receive buffer */
#define RCVBUFSIZE 32 

void DieWithError(char *errorMessage);                        /* Error handling function */
void HandleTCPServer(int sock, char *argv[]);                       /* Server handling function */

int main(int argc, char *argv[])

{
int sock;                                                 /* Socket descriptor */
struct sockaddr_in echoServAddr;                           /* Echo server address */
unsigned short echoServPort;                               /* Echo server port */
char *servIP;                                       /* Server IP address (dotted quad) */
char *echoString;
char echoBuffer[RCVBUFSIZE];
unsigned int echoStringLen;
int bytesRcvd, totalBytesRcvd;

unsigned int nrows=0;                                            /* number of data rows in the file */
unsigned int comment=0;                                       /* number of comment lines in file */
unsigned int i=0;                                                   /* counters */
const unsigned int linelen=700;                              /* length of buffer line */
FILE *fp;                                                              /* pointer to file to read data */
char userfile[40];                                                   /* name of file to read */
char **buff;                                                           /* buffer line */
char templine[700];                                                /* temporary line for initial reading of the file */

unsigned int nrows1=0;                                          /* number of data rows in the file */
unsigned int comment1=0;                                     /* number of comment lines in file */
unsigned int j=0;                                                    /* counters */
const unsigned int linelen1=700;                             /* length of buffer line */
FILE *fp1;                                                             /* pointer to file to read data */
char passfile[40];                                                   /* name of file to read */
char **buff1;                                                          /* buffer line */
char templine1[700];                                               /* temporary line for initial reading of the file */
char sendBuffer[BUFSIZE];


    if ((argc < 3) || (argc > 4))                              /* Test for correct number of arguments */
    {
       fprintf(stderr, "Usage: %s <Server IP> [<Port>]\n",argv[0]);
       exit(1);
    }

    servIP = argv[1];                                        /* First arg: server IP address (dotted quad) */
    echoString = argv[2];

    if (argc == 4)
        echoServPort = atoi(argv[3]);                      /* Use entered port */

    else

        echoServPort = 21;                                  /* 21 for default port */


    /* Create a reliable, stream socket using TCP */
    if ((sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
        DieWithError("socket() failed");


    /* Construct the server address structure */
    memset(&echoServAddr, 0, sizeof(echoServAddr));               /* Zero out structure */
    echoServAddr.sin_family      = AF_INET;                       /* Internet address family */
    echoServAddr.sin_addr.s_addr = inet_addr(servIP);             /* Server IP address */
    echoServAddr.sin_port        = htons(echoServPort);           /* Server port */


    /* Establish the connection to the echo server */
    if (connect(sock, (struct sockaddr *) &echoServAddr, sizeof(echoServAddr)) < 0)
        DieWithError("connect() failed");

echoStringLen = strlen(echoString);

if (send(sock, echoString, echoStringLen, 0) != echoStringLen)
DieWithError("send() sent a differnet number of bytes then expected");

totalBytesRcvd = 0;
printf("Received: ");
while (totalBytesRcvd < echoStringLen)
{
if ((bytesRcvd = recv(sock, echoBuffer, RCVBUFSIZE -1, 0)) <=0 )
DieWithError("recv() failed or connection closed prematurely");
totalBytesRcvd += bytesRcvd;
echoBuffer[bytesRcvd] = '\0';
printf(echoBuffer);
}

printf("\n");

    HandleTCPServer(sock, argv);

    close(sock);
  printf("Enter the name of the username file you want to read data from: ");
        scanf("%s", userfile);
        if (!(fp=fopen(userfile,"r"))) {
                perror(userfile);
                exit(1);
        }

        /* count the number of comment lines, and data lines */
        while (fgets(templine,sizeof(templine),fp)!=NULL) {
                if (templine[0]=='#') {
                        comment++;
                } else {
                        nrows++;
                }
        }
        printf("\nThere are %u comments and %u rows", comment, nrows);

        /* allocate string array to write all the lines from the file */
        buff=malloc((comment+nrows)*sizeof(*buff));
        if (buff==NULL) {
                printf("\nCould not allocate  pointer array");
        }
        for (i=0; i<(comment+nrows); i++) {
                buff[i]=malloc(linelen*sizeof(*buff[i]));
                if (buff[i]==NULL) {
                        printf("\nCould not allocate array %u", i);
                }
        }

        /* read all lines into buffer, and print to screen */
        rewind(fp);
        for (i=0; i<(comment+nrows); i++) {
                fgets(buff[i],linelen*sizeof(buff[i]),fp);
                printf("%s", buff[i]);
        }

        /* clean up memory */
        fclose(fp);
        for (i=0; i<(comment+nrows); i++) {
                free(buff[i]);
        }
        free(buff);


/* open a file to read */
        printf("Enter the name of the password file you want to read data from: ");
        scanf("%s", passfile);
        if (!(fp1=fopen(passfile,"r"))) {
                perror(passfile);
                exit(1);
        }

        /* count the number of comment lines, and data lines */
        while (fgets(templine1,sizeof(templine1),fp1)!=NULL) {
                if (templine1[0]=='#') {
                        comment1++;
                } else {
                        nrows1++;
                }
        }
        printf("\nThere are %u comments and %u rows", comment1, nrows1);

        /* allocate string array to write all the lines from the file */
        buff1=malloc((comment1+nrows1)*sizeof(*buff1));
        if (buff1==NULL) {
                printf("\nCould not allocate  pointer array");
        }
        for (j=0; j<(comment1+nrows1); j++) {
                buff1[j]=malloc(linelen1*sizeof(*buff1[j]));
                if (buff1[j]==NULL) {
                        printf("\nCould not allocate array %u", j);
                }
        }

        /* read all lines into buffer, and print to screen */
        rewind(fp1);
        for (j=0; j<(comment1+nrows1); j++) {
                fgets(buff1[j],linelen1*sizeof(buff1[j]),fp1);
                printf("%s", buff1[j]);
        }

   

while(sscanf(BUFSIZE, "%s\n", userfile))
{
     while(sscanf(BUFSIZE, "%s\n", passfile))
     {
          sprintf(sendBuffer, "USER %s\r\nPASS %s\r\n", userfile, passfile);
         
          send(sock, strlen(sendBuffer), 0, 0);

          memset(sendBuffer, 0, 256);
          // Perhaps memset pass buffer here
     }

     // And user buffer here, but I'm not sure it's necessary.

}


        /* clean up memory */
        fclose(fp1);
        for (j=0; j<(comment1+nrows1); j++) {
                free(buff1[j]);
        }
        free(buff1);

        return 0;

exit(0);

}


MonkeyNut
New User
New User
 
Posts: 7
Joined: Mon Mar 02, 2009 4:40 pm
Blog: View Blog (0)


Re: c programming help!

Post by aNewHobby4me on Thu Mar 05, 2009 9:03 pm
([msg=19387]see Re: c programming help![/msg])

from :
http://docs.online.bg/PROGRAMMING/the-c ... pter7.html
There is also a function sscanf that reads from a string instead of the standard input:

int sscanf(char *string, char *format, arg1, arg2, ...)

It scans the string according to the format in format and stores the resulting values through arg1, arg2, etc. These arguments must be pointers.

#define BUFSIZE 4096 /* Size of receive buffer */

You have BUFSIZE as the first argument, and sscanf was expecting a string char pointer.

[edit]
Last edited by aNewHobby4me on Thu Mar 05, 2009 9:55 pm, edited 1 time in total.
"To understand recursion you must first understand recursion."
aNewHobby4me
Poster
Poster
 
Posts: 185
Joined: Thu Jan 08, 2009 5:44 pm
Blog: View Blog (0)


Re: c programming help!

Post by MonkeyNut on Thu Mar 05, 2009 9:23 pm
([msg=19389]see Re: c programming help![/msg])

aNewHobby4me wrote:from :
http://docs.online.bg/PROGRAMMING/the-c ... pter7.html
There is also a function sscanf that reads from a string instead of the standard input:

int sscanf(char *string, char *format, arg1, arg2, ...)

It scans the string according to the format in format and stores the resulting values through arg1, arg2, etc. These arguments must be pointers.

#define BUFSIZE 4096 /* Size of receive buffer */

You have BUFSIZE as the first argument, and sscanf was expecting a string pointer.


I see so do i need to define another buffer for sending the data? for instance #define sbuffer 4096 and use that instead?
sorry if that seems like a stupid question but still trying to get my head around c!
MonkeyNut
New User
New User
 
Posts: 7
Joined: Mon Mar 02, 2009 4:40 pm
Blog: View Blog (0)


Re: c programming help!

Post by aNewHobby4me on Thu Mar 05, 2009 9:53 pm
([msg=19390]see Re: c programming help![/msg])

from : http://docs.online.bg/PROGRAMMING/the-c ... pter5.html
As formal parameters in a function definition,

char s[];

and

char *s;

are equivalent; we prefer the latter because it says more explicitly that the variable is a pointer.


char sendBuffer[BUFSIZE];

BUFSIZE is an integer. You would use the array name for the first argument of sscanf(). Also, I misspoke when I said it was a string pointer--it is a char pointer. Sorry.
"To understand recursion you must first understand recursion."
aNewHobby4me
Poster
Poster
 
Posts: 185
Joined: Thu Jan 08, 2009 5:44 pm
Blog: View Blog (0)


Re: c programming help!

Post by MonkeyNut on Thu Mar 05, 2009 10:20 pm
([msg=19393]see Re: c programming help![/msg])

Right think i fixed that however i seem to be getting one last warning for the following line:

send(sock, strlen(sendBuffer),0 , 0);

the warning is com.c:182: warning: passing argument 2 of ‘send’ makes pointer from integer without a cast

sorry to be a nuisance!
MonkeyNut
New User
New User
 
Posts: 7
Joined: Mon Mar 02, 2009 4:40 pm
Blog: View Blog (0)


Re: c programming help!

Post by mischief on Fri Mar 06, 2009 6:51 am
([msg=19409]see Re: c programming help![/msg])

MonkeyNut wrote:Right think i fixed that however i seem to be getting one last warning for the following line:<br><br>send(sock, strlen(sendBuffer),0 , 0);<br><br>the warning is com.c:182: warning: passing argument 2 of ‘send’ makes pointer from integer without a cast<br><br>sorry to be a nuisance!


no offense monkeynut, but i think you need to read some man pages. you are trying to pass a size_t, which is the result of strlen, to send(), and it expects a char *. check the man pages on send and strlen if you don't know what i am talking about.
The whole secret of existence is to have no fear. Never fear what will become of you, depend on no one. Only the moment you reject all help are you freed.
--Buddha
User avatar
mischief
Poster
Poster
 
Posts: 355
Joined: Wed Jan 07, 2009 4:16 pm
Blog: View Blog (0)


Re: c programming help!

Post by MonkeyNut on Fri Mar 06, 2009 7:40 am
([msg=19411]see Re: c programming help![/msg])

mischief wrote:
MonkeyNut wrote:Right think i fixed that however i seem to be getting one last warning for the following line:<br><br>send(sock, strlen(sendBuffer),0 , 0);<br><br>the warning is com.c:182: warning: passing argument 2 of ‘send’ makes pointer from integer without a cast<br><br>sorry to be a nuisance!


no offense monkeynut, but i think you need to read some man pages. you are trying to pass a size_t, which is the result of strlen, to send(), and it expects a char *. check the man pages on send and strlen if you don't know what i am talking about.


Hey mischief thanks for the reply. yeah i have a pretty basic knowledge of c. thanks for the link. i'll check it out
MonkeyNut
New User
New User
 
Posts: 7
Joined: Mon Mar 02, 2009 4:40 pm
Blog: View Blog (0)



Return to C and C++

Who is online

Users browsing this forum: No registered users and 0 guests