Thread: System calls

  1. #1
    Mak
    Guest

    System calls

    Hey, hi, im very new to c programming and im jus learning file system calls. I have these 2 programs but cant seem to get them to run (see below) if you could take a quick look and tell me what ive done wrong. I found these programs on the internet and am just getting my head around it. If you could comment and tell me what each line does it would be helpful and what the program could be used for

    Code:
    /*Program 1*/
    
    #include <fcntl.h>
    main()
    {
    int fd;
    
    fd = open("in1", O_WRONLY | O_CREAT | O_TRUNC, 0644);
    if(fd < 0)
    {
    perror("filename");
    exit(1);
    }
    
    }
    
    /**********************************************/
    
    /*Program 2*/
    
    #include <fcntl.h>
    main()
    {
    char *c;
    int fd, sz;
    
    c = (char *) calloc(100, sizeof(char));
    
    fd = open("in1", O_RDONLY);
    if (fd < 0)
    {
    perror("filename");
    exit(1);
    }
    
    sz = read(fd, c, 10);
    c[sz] = '\0';
    sz = read(fd, c, 99);
    c[sz] = '\0';
    close(fd);
    }
    Code tags added by Hammer

  2. #2
    Registered User
    Join Date
    Feb 2003
    Posts
    2
    ok program one is not going to do much all it will do it open a file.
    program 2 is similar it will not teach you much. if you are new to c and programming you shoudl start with an simpler program. Search the web for tutorials and a helloworld program. If you still want to have a example like below i will try you a better one showing a program that creates and opens a file and writes some stuff to it you then can open the file up in notpad and see what you typed. just post a reply asking and i will type it out when i get a few mins.
    Code:
    /*Program 1*/
    
    
    #include <fcntl.h>
    main()
    {
    int fd;
    
    fd = open("in1", O_WRONLY | O_CREAT | O_TRUNC, 0644);
    if(fd < 0)
    {
    perror("filename");
    exit(1);
    }
    
    }
    
    /**********************************************/
    
    /*Program 2*/
    
    #include <fcntl.h>
    main()
    {
    char *c;
    int fd, sz;
    
    c = (char *) calloc(100, sizeof(char));
    
    fd = open("in1", O_RDONLY);
    if (fd < 0)
    {
    perror("filename");
    exit(1);
    }
    
    sz = read(fd, c, 10);
    c[sz] = '\0';
    sz = read(fd, c, 99);
    c[sz] = '\0';
    close(fd);
    }

    Code tags added by Hammer

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Basic system calls help.
    By AmbliKai in forum C Programming
    Replies: 5
    Last Post: 03-21-2008, 07:18 AM
  2. Opinions on custom system build
    By lightatdawn in forum Tech Board
    Replies: 2
    Last Post: 10-18-2005, 04:15 AM
  3. Problem With My Box
    By HaVoX in forum Tech Board
    Replies: 9
    Last Post: 10-15-2005, 07:38 AM
  4. School Mini-Project on C/C++ (Need Your Help)..
    By EazTerence in forum C++ Programming
    Replies: 4
    Last Post: 09-08-2005, 01:08 AM
  5. System Calls && Variables
    By Okiesmokie in forum C++ Programming
    Replies: 6
    Last Post: 03-06-2002, 09:10 PM