Your SlideShare is downloading. ×
File Organization & processing Mid term summer 2014 - modelanswer
File Organization & processing Mid term summer 2014 - modelanswer
File Organization & processing Mid term summer 2014 - modelanswer
File Organization & processing Mid term summer 2014 - modelanswer
File Organization & processing Mid term summer 2014 - modelanswer
File Organization & processing Mid term summer 2014 - modelanswer
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

File Organization & processing Mid term summer 2014 - modelanswer

325

Published on

File Organization & processing MidTerm exam: tests knowledge of string manipulations and stream operations using C++

File Organization & processing MidTerm exam: tests knowledge of string manipulations and stream operations using C++

Published in: Education
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
325
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
7
Comments
0
Likes
1
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. Cairo University Faculty of Computers and Information Mid-Term Exam Summer2014 Page 1 of 1 Department: CS Course Name: File Organization & processing Date: 4-July-2014 Course Code: CS215 Duration: 2 hours Instructor(s): Dr. Hussien Sharaf Total marks: 24 scaled down to 8 marks Comments made by Eng. Nora Abdelhameed Mohamed Question1: [6 marks] Each of the following code snippets has logical errors. Correct and rewrite the incorrect lines. You don’t need to rewrite the whole code snippet. You must number the lines in your answer sheet: a. //Insert “fci” before @ [0.5 marks] 1. string email = "myMail@h.com"; 2. email.append( “_fci”); Solution 2. email.insert( “_fci”,5); b. //replace “h.” with “domain.edu.eg.” [0.5 marks] 1. string email = "myMail@h.com"; 2. email.replace(5,14, " domain.edu.eg."); Solution 2. email.replace(7,2, " domain.edu.eg."); c. //use find and substr to display all characters after the “=” sign [0.5 marks] 1. string tmp= "var1=var2* var3;"; 2. cout<< tmp.find("=")+1; Solution 2. cout<< tmp.substr( tmp.find("=")+1); d. //extract a field that ends with comma “,” from an opened stream [1 marks] 1. string tmp; 2. getline (instream,tmp); Solution 2. getline (instream,tmp,’,’); e. //write fields separated by “|” into a stream, save to file then read them back. [1 marks] 1. int ID; string Course,Email; 2. ID=811;Course="CS215";Email="myEmail@masr.com"; 3. ofstream Writestream ("example.txt",ios::trunc); 4. Writestream<<ID<<Course<<Email ; 5. Course.clear();Email.clear(); 6. //missing line 7. ifstream readstream ("example.txt"); 8. readstream>>ID>>Course>>Email ; Comment [NM1]: -Correcting the wrong line with multiple lines is accepted. -Function's name and it's parameters each get 0.25. -No grade for mentioning the wrong line only. -Order of parameters is ignored. Comment [NM2]: Comment [NM3]: Each parameter worth 0.25.
  • 2. Cairo University Faculty of Computers and Information Mid-Term Exam Summer2014 Page 2 of 2 9. readstream.close(); Solution 4. Writestream<<ID<<"|"<<Course<<"|"<<Email ; 6. Writestream.close(); OR Writestream.flush(); f. //write and save then read fields with length indicator [2.5 marks] 1. int ID, sSize; string Email; char buf[1000]; 2. ID=811;Email="myEmail@masr.com"; 3. Writestream.open ("example.txt",ios::trunc|ios::binary); 4. sSize=sizeof(int); 5. Writestream<<sSize; 6. Writestream<<ID; 7. sSize=Email.length(); 8. Writestream<<sSize; 9. Writestream<< Email.c_str(); 10. //missing line 11. ID=-1;Email.clear(); 12. readstream.open ("example.txt",ios::binary); 13. readstream.read((char*)&sSize,sizeof(int)); 14. readstream.read((char*)&ID,sizeof(int)); 15. readstream.read((char*)&sSize,sizeof(int)); 16. readstream.read(buf,sSize); buf[sSize]='0'; 17. Email=buf; 18. readstream.close(); Solution 5. Writestream.write((char*)&sSize, sizeof(int)); 6. Writestream.write((char*)&ID, sizeof(int)); 8. Writestream.write((char*)&sSize, sizeof(int)); 9. Writestream.write(Email.c_str(), sSize); 10. Writestream.close() or flush() Solve only one of the following questions Question2: [2 marks] a. What is the limitation of using byte as a data-type for length indicators? Solution: It can only be used with values that never exceed 255. b. Write an algorithm that can append a given char to a given string until it reaches the required length. Solution: string ExtendstringLength(string In, int requiredSize, string appendchar) { Comment [NM4]: -Algorithm with English description only and no code or even pseudo code got 50% -Loop and append function each got 50% of the grade.
  • 3. Cairo University Faculty of Computers and Information Mid-Term Exam Summer2014 Page 3 of 3 while (In.length()<requiredSize ) In=In.append(appendchar); return In; } Question3: [16 marks] Study the following code with no assistance and answer the following questions. rightExtract and leftExtract methods can be helpful for sections a-e: a. Extract the extension only. b. Extract the fileNameWithExtension and without the parent folderPath. c. Extract the fileNameWithoutExtension from the fileNameWithExtension. d. Get parentFolder path only from the fullPathFileNameWithExtension. e. Get root Drive only from the fullPathFileNameWithExtension. f. Continue missing lines in the getFileSize method. g. In the operator <<; for each of the private variables write <fieldName= fieldValue> into the outstream //i.e. <extension=pptx> h. In the operator >>; for each of the private variables read the fieldValue from the outstream //where each field was written in form of <fieldName= fieldValue> //.h file class cls_FileInfo{ private: //private variables string fullPathFileNameWithExtension, parentFolder,drive,fileNameWithoutExtension, extension; long fileSize; /*******private methods*******/ //default constructor made private to prevent calling it cls_FileInfo(); //Given a string it searches for a "searchFor" string from the right side //and returns all characters from right side until the index of the "searchFor" string string rightExtract(const string& In, const string& searchFor, int startPosition=-1) {/* startPosition: Position of the last character in the string to be considered in the search. Any value greater or equal than the string length (including string::npos) means that the entire string is searched. Note: The first character is denoted by a value of 0 (not 1). */ int index =-1; index= In.find_last_of(searchFor,startPosition); if (index>0) return In.substr(index+searchFor.length()); Comment [NM5]: Solving any part of this Q. without using the defined functions in class is accepted. Comment [NM6]: Writing and reading >> and << got 1 mark ... as it ignores the delimiter issue.
  • 4. Cairo University Faculty of Computers and Information Mid-Term Exam Summer2014 Page 4 of 4 return ""; } //Given a string it searches for a "searchFor" string from the left side //and returns all characters from left side until the index of the "searchFor" string string leftExtract(const string& In, const string& searchFor, int startPosition=0) {/* startPosition: Position of the first character in the string to be considered in the search. Any value greater or equal than the string length (including string::npos) means that the entire string is searched. Note: The first character is denoted by a value of 0 (not 1). */ int index =-1; index= In.find(searchFor,startPosition); if (index>0) return In.substr(startPosition,index); return ""; } public: cls_FileInfo(const string& ); ~cls_FileInfo(){}; //overload extraction operator for reading from files friend istream& operator >>( istream& instream,cls_FileInfo& p); //overload insertion operator for writing on files/screen friend ostream& operator <<( ostream& outstream,const cls_FileInfo& p); //calculate the fileSize using tellg static long getFileSize(const string& pfullPathFileNameWithExtension); }; //.CPP file cls_FileInfo::cls_FileInfo(const string& pfullPathFileNameWithExtension) { string fileNameWithExtension; fullPathFileNameWithExtension = pfullPathFileNameWithExtension; //call get file size fileSize=getFileSize(fullPathFileNameWithExtension); //a. Extract extension only extension = rightExtract(pfullPathFileNameWithExtension, ".") ; //b. Extract fileNameWithExtension and without the parent folderPath fileNameWithExtension = rightExtract(fullPathFileNameWithExtension, "") ; //c. extract the fileNameWithoutExtension from the fileNameWithExtension fileNameWithoutExtension = leftExtract(fileNameWithExtension, ".") ; //d. Get parentFolder path only from the fullPathFileNameWithExtension parentFolder =leftExtract(fullPathFileNameWithExtension, fileNameWithoutExtension+"."+extension) ; //e. Get root Drive only from the fullPathFileNameWithExtension drive = leftExtract(fullPathFileNameWithExtension, ":") ;
  • 5. Cairo University Faculty of Computers and Information Mid-Term Exam Summer2014 Page 5 of 5 } long cls_FileInfo::getFileSize(const string& pfullPathFileNameWithExtension){ //open a stream called readStream ifstream readStream(pfullPathFileNameWithExtension.c_str()); //if the stream is not opened successfully then return 0 if (!readStream.is_open()) return 0; //f. continue missing lines //go to the end of the stream readStream.seekg(0,ios::end); //get the current position int rFileSize = readStream.tellg(); //close the stream readStream.close(); //return the number of bytes in the stream return rFileSize; } ostream& operator <<( ostream& outstream,const cls_FileInfo& p) { //string drive,parentFolder,fileNameWithoutExtension, extension; //long fileSize; //g. for each of the above fields write <fieldName= fieldValue> into the outstream //i.e. <extension=pptx> //drive outstream<<"<drive="<<p.drive<<">"; //parentFolder outstream<<"<parentFolder="<<p.parentFolder<<">"; //fileNameWithoutExtension outstream<<"<fileNameWithoutExtension="<<p.fileNameWithoutExtension<<">"; //extension outstream<<"<extension="<<p.extension<<">"; //fileSize outstream<<"<fileSize="<<(float)p.fileSize/1024<<" KB."<<">"; return outstream; } istream& operator >>( istream& instream,cls_FileInfo& p) { //use getline (istream,string_Line,char) more than one time with different //delimiters to extract each value //string drive,parentFolder,fileNameWithoutExtension, extension; //long fileSize; //h. for each of the above fields read the fieldValue from the outstream
  • 6. Cairo University Faculty of Computers and Information Mid-Term Exam Summer2014 Page 6 of 6 //where each field was written in form of <fieldName= fieldValue> //i.e. <extension=pptx> string tmp; //drive getline (instream,tmp,'<'); getline (instream,tmp,'='); getline (instream,tmp,'>'); p.drive = tmp; //parentFolder getline (instream,tmp,'<'); getline (instream,tmp,'>'); p.parentFolder = tmp.substr( tmp.find("=")+1); //fileNameWithoutExtension getline (instream,tmp,'<'); getline (instream,tmp,'>'); p.fileNameWithoutExtension = tmp.substr( tmp.find("=")+1); //extension getline (instream,tmp,'<'); getline (instream,tmp,'>'); p.extension = tmp.substr( tmp.find("=")+1); //fileSize getline (instream,tmp,'<'); getline (instream,tmp,'>'); tmp = tmp.substr( tmp.find("=")+1); p.fileSize=atoi(tmp.c_str()); return instream; }

×