12
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

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

Embed Size (px)

Citation preview

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

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

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

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)

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

File add

#include <stdio.h>

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;

}

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

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)

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

HelloWorld.cpp (C++ version)

#include <iostream>

#include <fstream>

using namespace std;

int main( void ){

ofstream fout;

fout.open( "HelloWorld.txt" );

if( fout.is_open() ) {

fout << "Hello World!\n";

fout.close();

}

return 0;

}

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

HelloWorld.cpp (C++ version)

#include <iostream>

#include <fstream>

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;

}

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

Reading of Files

#include <stdio.h>

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;}

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

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

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

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)

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

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

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

Next Week…

▪ More C/C++– Pointers– Multiple Inheritance– Assembly

▪ Take Home Lab 1

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

The End

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