data from terminal to a local file.

This is a discussion on data from terminal to a local file. within the Linux Programming forums, part of the Platform Specific Boards category; Guys, what I am trying to accomplish is to catch output from keyboard and direct it to a file: Code: ...

  1. #1
    Dragon Rider jas_atwal's Avatar
    Join Date
    Nov 2007
    Location
    India
    Posts
    54

    data from terminal to a local file.

    Guys,

    what I am trying to accomplish is to catch output from keyboard and direct it to a file:

    Code:
    int main(){
    
      char buffer[128];
      int out, len;
    
      out = open("file.out", O_CREAT, S_IWUSR|S_IRUSR);
      len = read(0, &buffer, 128);
      do {
            write(out, &buffer, len);
      } while((len = read(0, &buffer, 128)) >= 128 );
    
      exit(0);
    }
    I am able to compile this perfectly fine. When I execute the binary it asks for input from terminal. file.out is created on local disk but... it is always empty.
    please advice!!

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,701
    Are you sure the first read() isn't returning 0?

    gg

  3. #3
    Dragon Rider jas_atwal's Avatar
    Join Date
    Nov 2007
    Location
    India
    Posts
    54
    Quote Originally Posted by Codeplug View Post
    Are you sure the first read() isn't returning 0?

    gg
    I made the following modifications to the above code.
    Code:
      out = open("file.out", O_CREAT, S_IWUSR|S_IRUSR);
      len = read(0, &buffer, 128);
    
      write(1, " value for len is "  , 64);
      write(1, &len, 2);
    
      do {
            write(out, &buffer, len);
      } while((len = read(0, &buffer, 128)) >= 128 );
    after compiling the new code I get the following output which is definatly ODD... please advice...

    Code:
    Hi how are you>?
     value for len is $@■  @°■  xX   

  4. #4
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,701
    write() always writes in binary, so if you want to see the text representation of 'len', you have to convert it to a string yourself.

    write() will also write the number of bytes that you tell it. That static string is not 64 bytes long. You can hard code 18 or make a "write_string" function that calls strlen().

    gg

  5. #5
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,273
    You are opening the output with mode O_CREAT. This does not allow writing to the file. Your calls to write() are doing nothing. You want to use O_CREAT | O_WRONLY.

    Also, you are processing data in blocks of 128 bytes. One bug I see is that if the last block does not contain exactly 128 bytes, it won't get copied. Why are you checking if len >= 128? You ought to check just that it's greater than zero, meaning you got SOMETHING.

    As it stands now, it will only do the right thing if the input is either less than 128 bytes, or, if greater than 128 bytes, is an exactly multiple of 128 bytes.

    So you have two things to fix.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Unknown memory leak with linked lists...
    By RaDeuX in forum C Programming
    Replies: 6
    Last Post: 12-07-2008, 04:09 AM
  2. Replies: 48
    Last Post: 09-26-2008, 04:45 AM
  3. Bitmasking Problem
    By mike_g in forum C++ Programming
    Replies: 13
    Last Post: 11-08-2007, 12:24 AM
  4. File Database & Data Structure :: C++
    By kuphryn in forum C++ Programming
    Replies: 0
    Last Post: 02-24-2002, 11:47 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21