Thread: Unable to open filedescriptor

  1. #1
    Registered User
    Join Date
    Jan 2013
    Posts
    114

    Unable to open filedescriptor

    Here is my code of trying to open a file-descriptor and write data to it.
    Code:
    #include <stdio.h>
    #include <stdlib.h> 
    #include <fcntl.h>
    #include <string.h>
    #include <sys/stat.h>
    #include <unistd.h>
    void usage(char *prog_name,char *filename)
    {
        printf("Usage: %s <data to add to %s>\n", prog_name, filename);
        exit(0);
    
    }
    void fatal(char *);
    void *ec_malloc(unsigned int);
    
    int main(int argc,char *argv[])
    {
        int fd;
        char *buffer,*datafile;
        buffer =   (char*)ec_malloc(100);
        datafile = (char*)ec_malloc(20);
        strcpy(datafile,"/home/note");
        if(argc<2)
            usage(argv[0],datafile);
    
        strcpy(buffer,argv[1]);
        printf("[DEBUG] buffer@ %p: \'%s\'\n", buffer, buffer);
        printf("[DEBUG] datafile @ %p: \'%s\'\n", datafile, datafile);
        strncat(buffer,"\n",1);
        fd = open(datafile,O_WRONLY|O_CREAT|O_APPEND,S_IRUSR|S_IWUSR);
        if(fd = -1)
            fatal("In main() while opening file\n");
        printf("[DEBUG] file descriptor is %d\n", fd);
        if(write(fd, buffer, strlen(buffer)) == -1)
            fatal("in main() while writing buffer to file");
        if(close(fd) == -1)
           fatal("in main() while closing file");
        printf("Note has been saved.\n");
        free(buffer);
        free(datafile);
    }
    
    void fatal(char *message){
        char error_message[100];
        strcpy(error_message,"[!!] Fatal Error ");
        strncat(error_message,message,strlen(message));
        perror(error_message);
        exit(-1);
    }
    
    void *ec_malloc(unsigned int x){
        void *ptr = malloc(x);
        if(ptr == NULL)
            fatal("in ec_malloc() on memory allocation");
        return ptr;
    
    }
    Now If I don't give any CL argument, then the response is
    Code:
    Usage: ./simplenote <data to add to /tmp/note>
    But if I give command line such as, `./simplenote "This is a test"` than no filedescriptor is returned. Here is the output of what I get,

    Code:
        [DEBUG] buffer@ 0x1edf010: 'this is a test note'
        [DEBUG] datafile @ 0x1edf080: '/tmp/note'
        [!!] Fatal Error In main() while opening file
        : Success
    So, What is the problem? Why can't it open the file?

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Remember that the operator= is the assignment operator, not the comparison operator==.

    Code:
        if(fd = -1)
            fatal("In main() while opening file\n");

    Jim

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Unable to open/read the .dat file
    By pinvpn in forum C++ Programming
    Replies: 4
    Last Post: 11-09-2013, 05:42 AM
  2. Unable to open file using fopen()
    By sanddune008 in forum C Programming
    Replies: 7
    Last Post: 01-13-2012, 10:46 AM
  3. Why am I unable to open the files?
    By Kat007 in forum C++ Programming
    Replies: 24
    Last Post: 09-26-2010, 08:01 PM
  4. Unable to open included file
    By jaisha in forum C Programming
    Replies: 12
    Last Post: 04-22-2010, 12:23 AM
  5. Unable to open USB drive
    By abc123 in forum Tech Board
    Replies: 1
    Last Post: 11-09-2006, 03:50 PM