PDA

View Full Version : not working: if( (fd = open("data", 0_RDONLY)) < 0)



Viktorija Rubyt
04-15-2014, 12:14 PM
okey, this is code:


#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>

#include <fcntl.h>

void printpos(char *msg, int fd);

main()

{
int fd; /*file descriptor*/

int pid; /*process-id*/

char buf[10]; /*buffer to hold file data*/

if( (fd = open("date", 0_RDONLY)) < 0)

{
fatal ("open failed");

read(fd, buf, 10); /*advance file pointer*/

printpos("Before fork", fd); }

if( (pid = fork()) < 0) {

fatal("fork failed"); }

else if (pid == 0){ /*child*/

printpos("Child before read", fd);

read(fd, buf, 10);

printpos("Child after read", fd);

}else{ /*parent*/

/*wait until child finished*/
wait( (int *)0);
printpos("Parent after wait", fd);
}

int printpos(const char *str, int fd) {

off_t pos;
if (( pos = lseek(fd, 0, SEEK_CUR)) == -1) {
fatal(“lseek failed”);
}
printf(“%s:%ld\n”, str, pos);
}


void printpos( char *msg, int fd )
/* Print position in file */
{
long int pos;

if( (pos = lseek( fd, 0L, SEEK_CUR) ) < 0L )
perror(“lseek”);

printf( “%s: %ld\n”, msg, pos );
}
return 0;
}




Problem is that bold line says that:
13494

CTRL + Q to Enable/Disable GoPhoto.it

laserlight
04-15-2014, 12:27 PM
It looks like the code that you posted is not the code that you compiled. According to the error message, the code that you compiled contains a line in which a variable named open is declared. This declaration hides the declaration of the POSIX standard function named open.

anduril462
04-15-2014, 12:34 PM
Post as plain text with proper indentation/formatting next time.

Also, compile with errors/warnings turned all the way up (-Wall flag for gcc):


gcc -Wall -ggdb3 -pedantic -std=gnu99 -O0 -o foo foo.c -lm -lpthread -lrt
foo.c:10:1: warning: return type defaults to ‘int’ [enabled by default]
foo.c: In function ‘main’:
foo.c:19:28: error: invalid suffix "_RDONLY" on integer constant
foo.c:22:9: warning: implicit declaration of function ‘fatal’ [-Wimplicit-function-declaration]
foo.c:43:9: warning: implicit declaration of function ‘wait’ [-Wimplicit-function-declaration]
foo.c:47:5: warning: ISO C forbids nested functions [-pedantic]
foo.c: In function ‘printpos’:
foo.c:51:13: error: stray ‘\342’ in program
foo.c:51:13: error: stray ‘\200’ in program
foo.c:51:13: error: stray ‘\234’ in program
foo.c:51:28: error: expected ‘)’ before ‘failed’
foo.c:51:28: error: stray ‘\342’ in program
foo.c:51:28: error: stray ‘\200’ in program
foo.c:51:28: error: stray ‘\235’ in program
foo.c:53:9: error: stray ‘\342’ in program
foo.c:53:9: error: stray ‘\200’ in program
foo.c:53:9: error: stray ‘\234’ in program
foo.c:53:19: error: expected expression before ‘%’ token
foo.c:53:19: error: stray ‘\’ in program
foo.c:53:19: error: stray ‘\342’ in program
foo.c:53:19: error: stray ‘\200’ in program
foo.c:53:19: error: stray ‘\235’ in program
foo.c:54:5: warning: no return statement in function returning non-void [-Wreturn-type]
foo.c: In function ‘main’:
foo.c:57:5: warning: ISO C forbids nested functions [-pedantic]
foo.c:57:10: error: conflicting types for ‘printpos’
foo.c:47:9: note: previous definition of ‘printpos’ was here
foo.c: In function ‘printpos’:
foo.c:63:13: error: stray ‘\342’ in program
foo.c:63:13: error: stray ‘\200’ in program
foo.c:63:13: error: stray ‘\234’ in program
foo.c:63:13: error: stray ‘\342’ in program
foo.c:63:13: error: stray ‘\200’ in program
foo.c:63:13: error: stray ‘\235’ in program
foo.c:63:13: warning: passing argument 1 of ‘perror’ from incompatible pointer type [enabled by default]
/usr/include/stdio.h:843:13: note: expected ‘const char *’ but argument is of type ‘__off_t (*)(int, __off_t, int)’
foo.c:65:9: error: stray ‘\342’ in program
foo.c:65:9: error: stray ‘\200’ in program
foo.c:65:9: error: stray ‘\234’ in program
foo.c:65:20: error: expected expression before ‘%’ token
foo.c:65:20: error: stray ‘\’ in program
foo.c:65:20: error: stray ‘\342’ in program
foo.c:65:20: error: stray ‘\200’ in program
foo.c:65:20: error: stray ‘\235’ in program
make: *** [foo] Error 1

1. The "error: stray" are because you somehow got "smart quotes" in there. Perhaps using MS Word or some other non-plain-text editor for your code.
2. You can't nest functions in C (i.e. no functions inside other functions).
3. You can't overload functions. One name for one function. No overloading const-ness or anything else.
4. Your prototype, definition and all calls to a function must match in return type, parameter count, type and order. A few types auto-promote as needed (e.g. char * to const char *, but not the other way around).
5. fatal is not a C function I'm aware of and you forgot to include it.
6. You need the right headers for wait. Check the man page (type "man wait" on the command line) to find out which headers it needs.
7. You should explicitly declare main to return an int: int main().
8. Use an editor that supports a good programming font. Specifically one that can distinguish between 0 (zero) and O (upper case "oh"). Also, between 1 (one), l (lower case "ell") and I (upper case "eye").