Why am I getting an "Expected Identifier" error?

Error: expected primary-expression before 'read', PLEASE HELP?

  • include <sys/types.h> #include <arpa/inet.h> #include <netdb.h> #include <string> #include <cstdlib> #include <iostream> #include <unistd.h> #include <stdlib.h> #include <string.h> #include <stdio.h> #include <sys/wait.h> #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> void handle_connection(int new_socket_fd); int main (int argc, char **argv){ const char *srvr_addr = NULL; const char *srvr_port = "18080"; const char *filename; struct sockaddr_in serv_addr;/* AF_INET */ struct sockaddr_in cli_addr;/* AF_INET */ int socket_fd; /* Socket */ int new_socket_fd; /* Client socket */ int bread; /* buffer read*/ int britten; /*buffer written*/ int buf_loc; /*buffer location*/ int buf; socklen_t client_length; if ( argc >= 2 ) { /* Addr on cmdline: */ srvr_addr = argv[1]; } else { /* Use default address: */ srvr_addr = "127.0.0.1" ; } if ( argc >= 3 ) srvr_port = argv[2]; socket_fd = socket(PF_INET,SOCK_STREAM,0); memset(&serv_addr,0,sizeof serv_addr); serv_addr.sin_family = AF_INET; serv_addr.sin_port = htons(atoi(srvr_port)); if ( strcmp(srvr_addr,"*") != 0 ) { /* Normal Address */ serv_addr.sin_addr.s_addr = inet_addr(srvr_addr); } else { serv_addr.sin_addr.s_addr = INADDR_ANY; } client_length = sizeof serv_addr; bind (socket_fd, (struct sockaddr*)&serv_addr,sizeof(serv_addr)); listen(socket_fd,5); client_length = sizeof(socket_fd); while (true) { client_length = sizeof (cli_addr); accept(socket_fd,(struct sockaddr*)&cli_addr, &client_length); int new_socket_fd = accept(socket_fd,(struct sockaddr*)&cli_addr, &client_length); int pid = fork(); if (pid == 0);{ char buf [8192]; read(socket_fd, buf ,450); char str[] = "GET /path/to/file somethingelse"; char* ptr; ptr = strtok(str," "); ptr = strtok(NULL, " "); printf("%s",ptr); filename = ptr; } while (1) { int bread = ssize_t read(filename,void *buf,size_t sizeof(buf)); if (bread == 0) break; void *buf_loc = buf; while (bread > 0) { int britten = ssize_t write(new_socket_fd,void *buf,size_t bread); if (britten <= 0) { } bread -= britten; buf_loc += britten; } } } } Im Getting these errors been stuck for hours cant I just do read (filename, buffer, size) do you have to specify ssize_t read and write in the beginning: : In function 'int main(int, char**)': 86: error: expected primary-expression before 'read' 86: error: expected ',' or ';' before 'read' 91: error: invalid conversion from 'int' to 'void*' 93: error: expected primary-expression before 'write' 93: error: expected ',' or ';' before 'write' 98: warning: pointer of type 'void *' used in arithmetic

  • Answer:

    Check this line int bread = ssize_t read(filename,void *buf,size_t sizeof(buf)); Here ssize_t, are you typecasing? I think you have simply copied the prototype of the function read here. Send correct arguments filename string is not initialized. Also, read function first argument is file descriptor (integer) while you are sending a string See thefollowing line also. It is simply a prototype. You have to call it by sending arguments. int britten = ssize_t write(new_socket_fd,void *buf,size_t bread);

DVJ29 at Yahoo! Answers Visit the source

Was this solution helpful to you?

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.