#include #include #include #include #include #include #include //Jason Kim //CSE 660 //Lab 1 //Due 4/15/05 using namespace std; int main() { //variables int file1,file2,file3,file4; char buf[BUFSIZ]; int bsize,bsize2; char step2[10] = "Jason Kim"; char step3[17] = "ABCDEFGHabcdefgh"; char step4[17] = "ijklmnopIJKLMNOP"; //Step 1 // Create ABC1 and ABC2 files file1 = creat("abc1.txt",S_IRWXU); close(file1); //creates abc1 file with read,write,and execute permission file2 = creat("abc2.txt",S_IRWXU); close(file2); //creates abc2 file with read,write,and execute permission file4 = creat("abc3.txt",S_IRWXU);//create abc3 close(file4); //note this creates abc files in my home directory //for another user, simply change the path to /home/youruser/abc1 if(file1 == -1) { cout << endl; cout << "The file was not created, this may because you did not change the file path to your specified directory, or you dont have permission to write files" << endl; } //opens x.x file file1 = open("/usr/class/cis660/x.x",O_RDONLY); if(file1 == -1)//check if file could not open { cout << "could not open file" << endl; exit(1); } file2 = open("abc1.txt",O_RDWR); file3 = open("abc2.txt",O_RDWR); file4 = open("abc3.txt",O_RDWR); //pulls of the first 10 characters and writes them to abc1 bsize = read(file1,buf,10); write(file2,buf,10); //pulls off the next 20 characters and writes them to abc2 bsize2 = read(file1,buf,20); write(file3,buf,20); //continues doing the above until end of file while( (bsize>=10) || (bsize2>=20) ) { bsize = read(file1,buf,10); write(file2,buf,10); bsize2 = read(file1,buf,20); write(file3,buf,20); } //step 2 Append "Jason Kim" (My Name) into abc1 at the end of the file lseek(file2,0,SEEK_END); write(file2,step2,14); //step 3 Write ABCDEFGHabcdefgh starting from location 100 int ABC1 (increase file size by 16) lseek(file2,100,SEEK_SET); write(file2,step3,17); //step 4 inserts into abc2 ijklmnopIJKLMNOP into location 160 lseek(file3,160,SEEK_SET); write(file3,step4,17); //step 5 create abc3, and copies abc1 into abc3 100 characters at a time lseek(file2,0,SEEK_SET);//reset buffer pointer of file abc1 back to start of the file abc1 bsize = read(file2,buf,100); //read 100 characters from abc1 write(file4,buf,100); //write 100 characters into abc3 while(bsize>=100) { bsize = read(file2,buf,100); write(file4,buf,100); } //step 6 appends abc3 with abc2 in reverse order //first read all of abc2 lseek(file3,0,SEEK_SET); bsize = read(file3,buf,1000); while(bsize>=1000) { bsize = read(file3,buf,1000); } char temp[BUFSIZ]; int tsize=BUFSIZ; while(tsize > 0)// now reverse the order { temp[tsize] = buf[tsize]; if(tsize != 0) { tsize--; } } lseek(file4,0,SEEK_END); write(file4,buf,BUFSIZ); //append reverse abc2 into abc3 //step 7 reads abc3 80 characters at a time and prints 80 characters to the screen. then deletes abc1,abc2,abc3 lseek(file4,0,SEEK_SET); read(file4,buf,80); int count=0; while(count < 80) { cout << buf[count]; count++; } file1 = unlink("abc1.txt"); //deletes files unlink("abc2.txt"); unlink("abc3.txt"); if(file1 == -1) { cout << endl; cout << "Files were not deleted, this may because you do not have permission to delete it" << endl; } return 0; }