Simple Read Write problem

This is a discussion on Simple Read Write problem within the Linux Programming forums, part of the Platform Specific Boards category; Sorry guys for the terribly basic question. I need to open, read and write information from and to a standard ...

  1. #1
    Registered User
    Join Date
    Sep 2010
    Posts
    6

    Question Simple Read Write problem

    Sorry guys for the terribly basic question.

    I need to open, read and write information from and to a standard file.
    Atm I am just trying to write the info to stderr to test if its working but to no avail.
    I'm sure there's something very basic I am doing wrong but I havnt been able to find any decent example to show me.

    Here is my code:

    Code:
    	FILE * ffp;
            /* Open Files */
    	ffp = open(firstpath, O_RDONLY);
    	
    	/* check file opened succesfully */
    	if(ffp == -1)
    	{
    		fprintf(stderr, "Error opening first file\n");
    		exit(1);
    	}
    
    	while(read(ffp, input_buffer, BUFFER) != -1)
    	{
    		write(stderr, &input_buffer, sizeof(input_buffer)); 
    	}
    The read command is always returning -1.
    This is running on a Solaris machine just so you know.

    Thanks for any help.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    And errno says what?

  3. #3
    Registered User
    Join Date
    Sep 2010
    Posts
    6
    It's saying bad address

  4. #4
    Registered User
    Join Date
    Sep 2010
    Posts
    6
    Ok I realised I wasnt mallocing any space for input_buffer but now the loop is going forever and printing nothing. Errno says bad file number.

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You mean EFAULT? Then you need to check how you messed up input_buffer.

    EDIT: Okay you fixed that while I was reading. Now: you can't write to stderr. You can printf to stderr, or you can write to 2.

  6. #6
    Registered User
    Join Date
    Sep 2010
    Posts
    6
    Thanks, thats got something printing but its wrong and all over the place. To read line by line into a file how would I go about that?

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Your read is correct. You should probably fix your write statement, if you are not printing what you want.

  8. #8
    Registered User
    Join Date
    Sep 2010
    Posts
    6
    Thank you very much for your help. I have it running now. The problem was the sizeof(input_buffer). I needed to specify exactly the size which is sizeof(char) * strlen(input_buffer)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with asynchronous read/write
    By mikepol in forum Linux Programming
    Replies: 1
    Last Post: 01-06-2011, 07:57 AM
  2. read write to file problem
    By xniinja in forum C Programming
    Replies: 1
    Last Post: 06-09-2010, 12:01 PM
  3. Read write problem
    By zephon in forum C Programming
    Replies: 3
    Last Post: 01-13-2009, 04:04 AM
  4. read() write() problem
    By spank in forum Linux Programming
    Replies: 5
    Last Post: 04-28-2007, 11:35 AM
  5. what should i do when .....(read/write txt file problem)
    By Jasonymk in forum C++ Programming
    Replies: 1
    Last Post: 10-30-2003, 08:55 PM

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