Thread: using C read() into buffer issues

  1. #1
    Registered User
    Join Date
    Feb 2017
    Posts
    7

    using C read() into buffer issues

    I'm trying to read a file using read() - not fread() - and read it in byte by byte (each one being a char) into a buffer with a maximum of 100 chars. When I attempt to print out the byte/char read in each time from the buffer, it outputs garbage or nothing at all. Why does it not output the character from the text file? I have a test file with 67 chars and it reads 1 byte 67 times, however outputs garbage. P.S. I'm new to C, and the posts/examples I've worked from seem to match what I'm doing (although I'm sure I'm doing something obvious wrong) Any help is much appreciated.
    Code:
    #include <stdio.h>#include <stdlib.h>
    #include <fcntl.h>
    #include <sys/types.h>
    #include <sys/stat.h>
    #include <unistd.h>
    
    
    int main(int argc, char *argv[]){
        if(argc != 2){
            fprintf(stderr,"Please only enter a single file name (.txt)\n");
        }
        else{
                char buff[1000];
            int fd = open(argv[1],O_RDONLY);
            int count = 0;
            int bytes;
            while ( (bytes = read(fd , &buff, sizeof(char))) > 0 ){
                printf("%d : %c\n",count,buff[count]);
                count++;
            }
        }
    }

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Are you sure the argument should be &buff? That's different from both buff and &buff[0].
    Pretty sure you mean read(fd, buff, sizeof buff)

    You can call for a smaller read() if you want, but there's no real point.

    For example:
    Code:
    #include <stdio.h>
     
    int main(void)
    {
    	char buff[1000];
    	int bytes = read(0, buff, sizeof buff); /* 0 is stdin's file descriptor */
    	if (bytes > 0)
    	{
    		int i;
    		for (i = 0; i < bytes; ++i)
    		{
    			printf("%d : %c\n", i, buff[i]);
    		}
    	}
    	return 0;
    }
    This works for me.

  3. #3
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    Quote Originally Posted by whiteflags View Post
    Are you sure the argument should be &buff? That's different from both buff and &buff[0].
    I think &buff is actually the same as buff here (what else could it mean?). Still, it's weird.

    OP, if you look more closely you'll see that it's printing the first char correctly. However, you are asking it to put every character into the first byte of buff, but are printing out the successive bytes of buff (buff[count], which will contain arbitrary "garbage"). Try:
    Code:
    read(fd, &buff[count], 1)  // sizeof(char) is always 1
    Of course, as whiteflags said, there's not much point in reading a byte at a time.

    And if it's outputing "nothing at all", that may be a space or newline character. You can use isprint to see if a character is printable.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <ctype.h>
    #include <fcntl.h>
    #include <sys/stat.h>
    #include <unistd.h>
     
    int main(int argc, char *argv[]){
        if (argc != 2){
            fprintf(stderr, "Error: Enter a single text file name.\n");
            exit(EXIT_FAILURE);
        }
    
        char buff[1000];
        int count = 0;
    
        int fd = open(argv[1], O_RDONLY);
    
        for ( ; read(fd, &buff[count], 1) > 0; count++)
            printf("%4d: %c\n", count,
                   isprint(buff[count]) ? buff[count] : '.');
    
        close(fd);
    
        return 0;
    }
    Last edited by algorism; 02-10-2017 at 07:09 PM.

  4. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Quote Originally Posted by algorism View Post
    I think &buff is actually the same as buff here (what else could it mean?). Still, it's weird.
    buff was declared as an array of char, so taking it's address in main() means the resulting type is char (*)[1000], or in English, pointer-to-array. Not really the same.

  5. #5
    Registered User
    Join Date
    Feb 2017
    Posts
    7
    This was the problem, I wasn't indexing buff[] so I was only writing to the first location. Thanks for the help!

  6. #6
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    Quote Originally Posted by whiteflags View Post
    buff was declared as an array of char, so taking it's address in main() means the resulting type is char (*)[1000], or in English, pointer-to-array. Not really the same.
    Good point. I guess I was thinking that it was similar to how a function pointer can be designated as either func or &func, which are clearly "different". The point being that &func has no other reasonable interpretation than to be considered the same as func. Same with &array and array.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Read buffer as matrix using X and Y values in C
    By TyrantUT in forum C Programming
    Replies: 2
    Last Post: 04-13-2016, 06:08 PM
  2. dump the buffer read from binary file
    By r00t in forum C Programming
    Replies: 23
    Last Post: 11-23-2009, 11:51 PM
  3. Question abt read(fd, buffer, 1024)
    By tesla in forum Networking/Device Communication
    Replies: 1
    Last Post: 09-12-2009, 03:52 AM
  4. C++ DirectX 9 3D z-buffer issues?
    By jrbarr in forum Game Programming
    Replies: 3
    Last Post: 02-14-2008, 01:23 AM
  5. Weird Z-buffer issues
    By VirtualAce in forum Game Programming
    Replies: 16
    Last Post: 08-04-2006, 05:21 PM

Tags for this Thread