Like Tree1Likes
  • 1 Post By grumpy

read() always returning 0 (not reading)

This is a discussion on read() always returning 0 (not reading) within the Linux Programming forums, part of the Platform Specific Boards category; I am playing around with the read(), write(), and open() system calls. I wrote a function to open/create 3 files, ...

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    19

    read() always returning 0 (not reading)

    I am playing around with the read(), write(), and open() system calls. I wrote a function to open/create 3 files, a function to write a test message to the files, and a file to read and print to the terminal what is in the files.

    The open and write system calls work fine, if I cat the files they display exactly what they should. When I try to read from the files though my reads are always returning 0, and so not displaying anything from the file.

    I tried running the program once, then commenting out the write function. When I do this the read function will read from the files, but the output is unfortunately garbage. I seem to have this sort of trouble when using the read system call and would very much appreciate any help so I can use them properly.

    Here are the open and read functions, I have code in the read function to check the values of the fd that the pointers hold and they are correct.

    Code:
    
    int openfiles(int *fdp1, int *fdp2, int *fdp3){
    
    	/*Open file for appending */
    	if((*fdp1=open("file1-append", O_RDWR|O_CREAT|O_APPEND,S_IRUSR |S_IWUSR))==-1){
    		perror("File1 Open Error");
    	}
    	
    	/* open file and truncate */
    	if((*fdp2=open("file2-trunc", O_RDWR|O_CREAT|O_TRUNC, S_IRUSR|S_IWUSR))==-1){
    		perror("File2 Open Error");	
    	}
    	
    	/* Open file only if it does not already exist */
    	if((*fdp3=open("file3-wronce", O_RDWR|O_CREAT|O_EXCL, S_IRUSR|S_IWUSR))==-1){
    		perror("File3 Open Error");
    	}
    
    return 0;
    
    }
    
    int readfiles(int *fdp1, int *fdp2, int *fdp3){
    	printf("\nfdp1:%d,fdp2:%d,fdp3:%d\n",*fdp1,*fdp2,*fdp3);
    	ssize_t readstatus;	
    	char mybuffer[BUFF_SIZE];
    	
    	
    	
    	puts("File 1:");
    	readstatus = read(*fdp1,&mybuffer,BUFF_SIZE);
    	printf("\nreadstatus:%d\n",readstatus);
    	while(readstatus > 0){
    		puts(mybuffer);
    		printf("\nreadstatus:%d\n",readstatus);
    		readstatus = read(*fdp1,&mybuffer,BUFF_SIZE);
    	}
    	printf("\nreadstatus:%d\n",readstatus);
    	if(readstatus ==-1){
    		perror("Error reading File 1:");
    		exit(1);
    	}
    	
    
    	
    	if((readstatus = read(*fdp2,&mybuffer,BUFF_SIZE))==-1){
    		perror("Error reading File 2:");
    		exit(1);
    	}
    	printf("\nreadstatus:%d\n",readstatus);
    	puts("File 2:");
    	puts(mybuffer);
    	if((readstatus = read(*fdp3,&mybuffer,BUFF_SIZE))==-1){
    		perror("Error reading File 3:");
    		puts("File probably already exists");	
    	}
    	puts("File 3:");
    	puts(mybuffer);
    
    	return 0;
    
    }

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,295
    Check the modes you're using to open your input files. All three files are being opened O_RDWR (which means you can both read and write them). O_APPEND (which you're using for the first input file) positions the file pointer at the end of the file, so read() will always return zero on the first call. O_TRUNC, which you're using for the second, truncates the file to length zero - which also causes read() to return zero on the first call.
    dtow1 likes this.
    Right 98% of the time, and don't care about the other 3%.

  3. #3
    Registered User
    Join Date
    Sep 2011
    Posts
    19
    Ah! Thank you grumpy. That makes sense. So I'll give lseek a shot.

    Thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 11-27-2012, 07:04 AM
  2. Function reading/converting/returning
    By clag in forum C Programming
    Replies: 0
    Last Post: 10-06-2009, 01:09 AM
  3. read() is not reading the whole data
    By maven in forum C Programming
    Replies: 6
    Last Post: 02-18-2006, 06:15 AM
  4. Reading a File and returning single words
    By kapri in forum C++ Programming
    Replies: 5
    Last Post: 05-11-2005, 05:13 AM
  5. reading in array and returning length
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 04-21-2002, 09:25 PM

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