GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 11-Oct-2004, 14:05
nkhambal nkhambal is offline
Regular Member
 
Join Date: Jul 2004
Location: CA USA
Posts: 313
nkhambal is a jewel in the roughnkhambal is a jewel in the rough

How to detect end of file with read() function call?


Hi,

I am writing this small program to copy a file into another using low level I/O functions read() and write().

Program works fine,in the sense that, it can copy one file to another, however, there are cuple of problems as listed below

1. I am defining a buffer of 512 bytes to read and write from file. However, if my source file size less than 512 bytes, the program does not stop after reading last character from source file but continues to add blanks to make its size equal to 512. It also adds junks at the end if the source file is bigger than 512 bytes but not exactly in size which is multiple of buffer length. (remainder of last fetched data using read()). How can I make it stop doing this ?
2. The destination file has executable permission only for others i.e it "ls -la" shows following for destination file

CPP / C++ / C Code:
---------x    1 root     root          512 Oct 11 12:59 dst.txt

How can I copy the same attributes as source file to destination file?

Following is my program.

CPP / C++ / C Code:
#include <stdio.h>
#include <fcntl.h>

int main(int argc, char *argv[])
{
	if (argc!=3)
	{
		printf("\nUSAGE: %s <source file name> <destination file name>\n",argv[0]);
		exit(1);
	}
	
	int src_handle,dst_handle;
	char buffer[512];
	const int bytes_read=512;
	int rd,wr;

	if ((src_handle=open(argv[1],O_RDONLY))==-1)
	{
		perror("Source File open read");
		exit(1);
	} 

	if ((dst_handle=open(argv[2],O_CREAT,O_WRONLY))==-1)
	{
		perror("Destination File create");
		exit(1);
	} 
	if ((dst_handle=open(argv[2],O_WRONLY))==-1)
	{
		perror("Destination File open write");
		exit(1);
	} 
	
	memset(buffer,'\0',512);
	while ((rd=read(src_handle,buffer,bytes_read*(sizeof(char))))!=0)
	{
		//printf("\nrd:= %d\n",rd);
		if (rd==-1)
		{
			printf("\nError reading source file\n");
			exit(1);
		}
		wr=write(dst_handle,buffer,bytes_read*(sizeof(char)));
		//printf("\nwr:= %d\n",wr);
		if (wr==-1)
		{
			printf("\nError writing to destination file\n");
			exit(1);
		}
	}
	
 return 0;
}

Thanks,
  #2  
Old 11-Oct-2004, 19:23
dsmith's Avatar
dsmith dsmith is offline
Senior Member
 
Join Date: Jan 2004
Location: Utah, USA
Posts: 1,351
dsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of light
Hi nkhambal.

Read actually will return the actual number of bytes read. So you should pass the actual number read to write. Also you should just check the rd value to make sure it is not less than the passed buffer size. I would also probably use a do while
CPP / C++ / C Code:
do{
  rd=read(src_handle,buffer,bytes_read*(sizeof(char)));
  if(rd>0)
    wr=write(dst_handle,buffer,rd);
}while(rd == bytes_read);

I took out some of your error checking, but that should give you the idea..

As for permissions, use should probably try to use a stat on the original file and then use the chown and chmod calls to change the new file to have the same owner/permissions.

I know that the stat structure has a st_uid & st_gid entry. I also believe that the posix stat has a field called st_mode.

HTH.
  #3  
Old 11-Oct-2004, 19:27
rikor rikor is offline
New Member
 
Join Date: Oct 2004
Location: Leicestershire
Posts: 1
rikor is on a distinguished road
I believe you can use the feof subroutine to check for the EOF indicator.

Quote:
Originally Posted by man page
The function feof() tests the end-of-file indicator for the stream point-
ed to by stream, returning non-zero if it is set. The end-of-file indi-
cator can only be cleared by the function clearerr().
  #4  
Old 11-Oct-2004, 19:36
dsmith's Avatar
dsmith dsmith is offline
Senior Member
 
Join Date: Jan 2004
Location: Utah, USA
Posts: 1,351
dsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of light
Quote:
Originally Posted by rikor
I believe you can use the feof subroutine to check for the EOF indicator.

Hi rikor. Welcome to GIDForums first of all. Glad to have you around and your response is very welcome here.

The feof function is used for the higher level IO functions using the FILE stream. nkhambal is using the low level functions of write/read which use a file handle.

This does bring up an interesting point though. I have had absolutely no luck using the feof in a binary file (using the stream io). Whenever I use stream io for a binary file, I always use stat to find the file size because feof doesn't work right for me.

Again, thanks for the response. We always appreciate new members who offer assistance.
  #5  
Old 11-Oct-2004, 21:11
nkhambal nkhambal is offline
Regular Member
 
Join Date: Jul 2004
Location: CA USA
Posts: 313
nkhambal is a jewel in the roughnkhambal is a jewel in the rough
Thanks dsmith,

I changed write function call's size paremeter to the number of bytes read by read() function call. It works fine now.

about using stat,do you have some code snippet to show how to do this ?

Thanks,
  #6  
Old 11-Oct-2004, 21:52
dsmith's Avatar
dsmith dsmith is offline
Senior Member
 
Join Date: Jan 2004
Location: Utah, USA
Posts: 1,351
dsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of light
Quote:
Originally Posted by nkhambal
Thanks dsmith,

I changed write function call's size paremeter to the number of bytes read by read() function call. It works fine now.

about using stat,do you have some code snippet to show how to do this ?

Thanks,

Here is a small program that will read the permissions/owner of the first file and impose them on the second file:
CPP / C++ / C Code:
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdio.h>

int main(int argv, char* argc[])
{
	struct stat buffer;
	
	if(argv!=3)
		printf("Program usage:\n\tfilestat <filename1> <filename2>\n\tfilename1 permissions/owner will be imposed on filename2.\n");
	else{
		if(!stat(argc[1],&buffer)){
			printf("Filename: %s\n",argc[1]);
			printf("------------------------------------------\n");
			printf("File owner is %d\n",buffer.st_uid);
			printf("File group is %d\n",buffer.st_gid);
			printf("File permisions are set to %o\n",buffer.st_mode);
			if(chmod(argc[2],buffer.st_mode))
				printf("Sorry, could not change the permissions of %s.\n",argc[2]);
			if(chown(argc[2],buffer.st_uid,buffer.st_gid))
				printf("Sorry. Could not change the owner of %s\n",argc[2]);
		}
		else
			printf("Sorry, %s is not a valid file.\n",argc[1]);
	}
	return 0;
}

Hope that helps.

Good luck!
  #7  
Old 12-Oct-2004, 01:08
nkhambal nkhambal is offline
Regular Member
 
Join Date: Jul 2004
Location: CA USA
Posts: 313
nkhambal is a jewel in the roughnkhambal is a jewel in the rough
Thanks dsmith,

That was helpful.
 
 

Recent GIDBlogDeveloping GUIs with wxPython (Part 3) by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Airport Log program using 3D linked List : problem reading from file batrsau C Programming Language 11 29-Feb-2008 07:44
how to call function from another to my file rameshs C Programming Language 1 09-Nov-2006 07:51
[Include] Doubly-linked List dsmith C Programming Language 6 14-Apr-2006 13:12
[GIM] gim.h dsmith C Programming Language 0 18-Jan-2005 08:48

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 20:38.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.