Thread: Low-Level I/O | read()

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    2

    Low-Level I/O | read()

    I'm trying to implement low-level I/O using open, write, and read. The above code works fine but I can't seem to use read() to get the data back out. I'm using read() with the read only flag and using a char as a buffer. I'm trying to read the contents of the file into a character array and then print that to the screen.


    Any ideas?

    Code:
    #include <fcntl.h>
    #include <stdio.h>
    
    char str[80];
    
    int i,f;
    
    main()
    
    { f = open("abc",O_WRONLY|O_CREAT,0700);
    
      printf("Enter a string: ");
      fgets(str, 10, stdin);
    
      i = strlen(str)-1;
      if( str[ i ] == '\n')
          str[i] = '\0';
    
      write(f,str,80);
      close(f);
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    I'm guessing that's because you never call read() anywhere in your program.

    On a somewhat more serious note, if you write then read, be sure to rewind your file pointer before reading. Or if WRONLY means write only, then you need to close and reopen the file.

  3. #3
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Yeah I think you did a perfect job of writing something that dumps stdin into a file. But perhaps you meant to do the exact opposite.... Just out of curiosity, are you using linux?

  4. #4
    Registered User
    Join Date
    Apr 2008
    Posts
    2
    Yes I am using Linux & I am having trouble implementing read()

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by js0n22 View Post
    Yes I am using Linux & I am having trouble implementing read()
    Why would you want to implement it? It's already there, isn't it?

    Have you done "man read" yet?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Low Level Keyboard Hook
    By sumone4life in forum C# Programming
    Replies: 3
    Last Post: 06-22-2009, 05:10 PM
  2. low level programming book
    By CChakra in forum C Programming
    Replies: 5
    Last Post: 09-08-2008, 01:23 PM
  3. why page based I/O can improve performance?
    By George2 in forum C Programming
    Replies: 1
    Last Post: 06-12-2006, 07:42 AM
  4. File I/O Read and Check
    By rabbit in forum C++ Programming
    Replies: 3
    Last Post: 02-06-2006, 09:02 PM
  5. How should I read this file (i/o question)
    By mmattax in forum C Programming
    Replies: 13
    Last Post: 01-17-2006, 11:16 AM