Presentation is loading. Please wait.

Presentation is loading. Please wait.

File ▪ File – Unit of logical storage – Aid in manipulating exact sector of file data ▪ Abstract view of secondary physical storage devices ▪ Without files.

Similar presentations


Presentation on theme: "File ▪ File – Unit of logical storage – Aid in manipulating exact sector of file data ▪ Abstract view of secondary physical storage devices ▪ Without files."— Presentation transcript:

1 File ▪ File – Unit of logical storage – Aid in manipulating exact sector of file data ▪ Abstract view of secondary physical storage devices ▪ Without files – You can’t process codes or other text-related material – You can’t run programs – You can’t run operating systems – Meaning, computers have only hardware

2 File ▪ Files are very important! – From running a command to idle time, files are being used ▪ Other programs need to access other files too – Like the basic algorithms on sorting (last week’s lab)

3 File add #include int main(int argc, char** argv){ FILE *file; file = fopen("haha.txt","w"); if(file == NULL){ fprintf(stderr,"File can't be opened!"); exit(1);} fprintf(file,"Hello World"); fclose(file); return 0; }

4 Constructor Declaration ▪ Declaring variables already allocates memory (like int i;) ▪ The same goes for declaring files (FILE *fp;) – A default zero variable for the constructor is created automatically the moment it is declared – Object is being instantiated at the moment of declaration (only applies in C++) ▪ ONLY IN C++ can you create constructors for your files – C does not allow this (because declaration != assignment)

5 HelloWorld.cpp (C++ version) #include using namespace std; int main( void ){ ofstream fout; fout.open( "HelloWorld.txt" ); if( fout.is_open() ) { fout << "Hello World!\n"; fout.close(); } return 0; }

6 HelloWorld.cpp (C++ version) #include using namespace std; int main( void ){ ofstream fout(“HelloWorld.txt”); //explicit constructor call if( fout.is_open() ) { fout << "Hello World!\n"; fout.close(); } return 0; }

7 Reading of Files #include char line[128]; int main(int argc, char** argv){FILE *file; file = fopen(argv[1],"r"); if(file == NULL){ fprintf(stderr,"File can't be opened!"); exit(1);} while(fgets(line,sizeof(line),file) != NULL){ printf(line); } fclose(file); return 0;}

8 Lab 3: Binary Converter ▪ Create a file binaryconvert.c that converts a text file into an 8-bit binary file and vice versa (following the ASCII convention) ▪ Use C (not C++) to process binary files – In short, don’t use the binary libraries in converting a number to binary ▪ Make sure that the output is printed out in a.bin file. And the content of that is just 1’s and 0’s. ▪ I should be able to run this program, provided that my other argument is a.txt file that will serve as the text to be converted

9 Lab 3: Binary Converter ▪ I should be able to do the reverse, meaning that if the file is a.bin file, then it converts it to a.txt file that converts the binaries to a text – This should still be divided into 8 bits (following the format for ASCII characters) ▪ I can run this using the command:./binaryconvert input.txt (for text to binary)./binaryconvert input.bin (for binary to text)

10 Lab 3: Binary Converter ▪ Of course, I should be able to see your Certificate of Authorship. Failure to do this will null and void your lab!!! ▪ Deadline: Tomorrow, 11:55 pm ▪ Submit it with the following format: CS162B_Lab3_ _ _.tar

11 Next Week… ▪ More C/C++ – Pointers – Multiple Inheritance – Assembly ▪ Take Home Lab 1

12 The End Dreams are a big picture of what you want. PLANS are the specifics of that dream.


Download ppt "File ▪ File – Unit of logical storage – Aid in manipulating exact sector of file data ▪ Abstract view of secondary physical storage devices ▪ Without files."

Similar presentations


Ads by Google