1 / 12

File

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

evania
Télécharger la présentation

File

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

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 <stdio.h> int main(intargc, 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 inti;) • 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 <iostream> #include <fstream> using namespace std; int main( void ){ ofstreamfout; fout.open( "HelloWorld.txt" ); if( fout.is_open() ) { fout << "Hello World!\n"; fout.close(); } return 0; }

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

  7. Reading of Files #include <stdio.h> char line[128]; int main(intargc, 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) ./binaryconvertinput.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_<Section>_<Surname>_<ID Number>.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.

More Related