Thread: system calls read and write

  1. #1
    Registered User
    Join Date
    Nov 2020
    Posts
    14

    system calls read and write

    Hey,
    Is it even possible to write to a binary file a number as an int and not as a string? If yes how do i write it and how to i read it?

  2. #2
    Registered User
    Join Date
    Sep 2020
    Posts
    223
    To write an integer to a file

    Code:
       int i;
      fwrite(&i,sizeof(i),1,f);
    To read an integer from a file

    Code:
       int i;
      fread(&i,sizeof(i),1,f);
    (Where 'f' is a file pointer)

    You should also check for errors and act on them...

  3. #3
    Registered User
    Join Date
    Nov 2020
    Posts
    14
    Quote Originally Posted by hamster_nz View Post
    To write an integer to a file

    Code:
       int i;
      fwrite(&i,sizeof(i),1,f);
    To read an integer from a file

    Code:
       int i;
      fread(&i,sizeof(i),1,f);
    (Where 'f' is a file pointer)

    You should also check for errors and act on them...
    I am supposed to use read and write system calls

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,309
    The *nix read and write operate on bytes, so the bytes could be the bytes of the int value in memory, or they could be the bytes of a string representation of the number; it's up to you.
    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

  5. #5
    Registered User
    Join Date
    Sep 2020
    Posts
    223
    Quote Originally Posted by giorgosmarga View Post
    I am supposed to use read and write system calls
    Here is using the "read" and "write" system calls directly.

    Note that this program needs no header files, as it is accessing the Linux system calls directly rather than using any library functions

    Code:
    // SYSTEM CALL NUMBERS
    #define SYSCALL_READ  0
    #define SYSCALL_WRITE 1
    
    
    // STANDARD FILE DESCRIPTORS
    #define STDIN  0
    #define STDOUT 1
    
    
    long syscall(long number, ...);
    
    
    const char msg[] = "Hello world! Press Enter\n";
    int main(void) {
       char buffer[1];
       syscall(SYSCALL_WRITE, STDOUT, &msg, sizeof(msg)-1);
       syscall(SYSCALL_READ, STDIN, &buffer, 1);
       return 1;
    }
    Here's the output:
    Code:
    $ ./main
    Hello world! Press Enter
    
    
    $

  6. #6
    Registered User
    Join Date
    Feb 2019
    Posts
    851
    Or, using the wrapper functions and worrying about endianess:
    Code:
    #include <unistd.h>
    #include <arpa/inet.h>
    
    int n = 1;
    
    // Write all bytes of n in BIG ENDIAN format.
    n = htonl( n );
    write( STDOUT_FILENO, &n, sizeof n );

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Lot of WRITE calls in parallel in C WRITE test code
    By Bobby1995 in forum C Programming
    Replies: 2
    Last Post: 04-28-2020, 08:47 AM
  2. Replies: 3
    Last Post: 10-28-2015, 02:07 PM
  3. System Calls - write() & read () int
    By easly89 in forum C Programming
    Replies: 2
    Last Post: 12-18-2011, 05:12 AM
  4. System calls (open, close, write)
    By ollie88r in forum C Programming
    Replies: 7
    Last Post: 11-09-2009, 06:16 PM
  5. write and read system calls
    By nacho4d in forum C Programming
    Replies: 4
    Last Post: 01-28-2008, 10:59 AM

Tags for this Thread