Thread: not working: if( (fd = open("data", 0_RDONLY)) < 0)

  1. #1
    Registered User
    Join Date
    Apr 2014
    Posts
    1

    Exclamation not working: if( (fd = open("data", 0_RDONLY)) < 0)

    okey, this is code:
    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:
    not working: if( (fd = open(&quot;data&quot;, 0_RDONLY)) &lt; 0)-progc-jpg

    CTRL + Q to Enable/Disable GoPhoto.it

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Post as plain text with proper indentation/formatting next time.

    Also, compile with errors/warnings turned all the way up (-Wall flag for gcc):
    Code:
    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").

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. LINK : fatal error LNK1104: cannot open file "nafxcwd.lib"
    By kandas.soft in forum C++ Programming
    Replies: 5
    Last Post: 07-26-2011, 03:15 AM
  2. "Data Fork"/"Alternate Data Streams"
    By phantomotap in forum Tech Board
    Replies: 3
    Last Post: 08-06-2010, 11:01 AM
  3. fatal error LNK1104: cannot open file "Files\Microsoft.obj"
    By chintugavali in forum C Programming
    Replies: 2
    Last Post: 03-14-2008, 07:33 AM
  4. "itoa"-"_itoa" , "inp"-"_inp", Why some functions have "
    By L.O.K. in forum Windows Programming
    Replies: 5
    Last Post: 12-08-2002, 08:25 AM
  5. "CWnd"-"HWnd","CBitmap"-"HBitmap"...., What is mean by "
    By L.O.K. in forum Windows Programming
    Replies: 2
    Last Post: 12-04-2002, 07:59 AM

Tags for this Thread