Computing.Net > Forums > Programming > Please Help - Adding Print statements an

Please Help - Adding Print statements an

Reply to Message Icon

Original Message
Name: Ian
Date: April 7, 2002 at 09:36:21 Pacific
Subject: Please Help - Adding Print statements an
Comment:

I have produced the code for copying a file safely and the code for getting file attributes
How do I get the code to show the commands (i.e. File to Copy - file Not Found - File Exists - File copied etc...)
Also I have the code which shows the file attributes of a file - How do I add this code into my cade for the safecopy file
The code is listed below:

/* safecp.c file for copying files safely */

#include
#include
#include
#include
#include
#include

#define BUFFERSIZE 512

void Error(char* msg)
{
perror(msg);
exit(0);
}

main(int argc, char** argv)
{
int fileDescriptor1, fileDescriptor2;
ssize_t bytesRead,bytesWritten;
char buffer[BUFFERSIZE]; /* 512 byte buffer for transferring data */
int i;

/* check the command line syntax */
if (argc!=3) {Error("syntax: copy fromFile toFile");}

/* open input file read only */
fileDescriptor1=open(argv[1],O_RDONLY);
if (fileDescriptor1==-1) {Error("Open Read:");}

/* create output file (must not exist) & set permissions to user only */
fileDescriptor2=open(argv[2],O_WRONLY|O_CREAT|O_EXCL,S_IRWXU);
if (fileDescriptor2==-1) {Error("Open Write: ");}

/* while not End Of File, read the next block*/
while ((bytesRead=read(fileDescriptor1,buffer,BUFFERSIZE))!=0)
{
if (bytesRead==-1) {Error("Reading: ");}
/* write the same number of bytes back to the output file */
bytesWritten=write(fileDescriptor2,buffer,bytesRead);
if (bytesWritten==-1) {Error("Writing: "); }
}

/* close both files */
close(fileDescriptor1);
close(fileDescriptor2);
}


/* attr.c - display the file attributes from the inode */


#include
#include
#include
#include
#include

void Error(char* msg)
{
perror(msg);
exit(0);
}

main(int argc, char** argv)
{
struct stat inode; /* create inode struct for OS to copy into */
int status;

if (argc!=2) {Error("syntax: attr fileName"); }

status=stat(argv[1],&inode);
if (status==-1) {Error("Getting inode: "); }

printf("filename: %s\n",argv[1]);

/* print selected attribute fields from the inode structure */
/* these fields are described on the man page for stat */

printf("\tdevice: %#x \tinodeNo: %d\n",inode.st_dev,inode.st_ino);
printf("\tmode: %#x \towner: %d\n",inode.st_mode,inode.st_uid);
printf("\tsize: %d \tmodified: %s \n",inode.st_size,
ctime(&inode.st_mtime));
}

Any help would be greatly appreciated
Thanks in advance


Report Offensive Message For Removal








Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: Please Help - Adding Print statements an

Comments:

 


  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 
Data Recovery Software




How often do you use Computing.Net?

Every Day
Once a Week
Once a Month
This Is My First Time!


View Results

Poll Finishes In 3 Days.
Discuss in The Lounge