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

Slides:



Advertisements
Similar presentations
Chapter 7 Process Environment Chien-Chung Shen CIS, UD
Advertisements

Strings and Dynamic Memory Allocation CS-2301, B-Term Programming Assignment #6 Strings and Dynamic Memory Allocation CS-2301, System Programming.
Memory Image of Running Programs Executable file on disk, running program in memory, activation record, C-style and Pascal-style parameter passing.
CS211 Data Structures Sami Rollins Fall 2004.
CS 101 Problem Solving and Structured Programming in C Sami Rollins Spring 2003.
Classes and Objects Instructor: 小黑. Files and Streams 1 #include int main( void ) { 5 char input[ 5 ]; 6 7 FILE *cfPtr; 8 9 if (( cfPtr = fopen(
Arrays and Pointers in C Alan L. Cox
CSE 332: C++ program structure and development environment C++ Program Structure (and tools) Today we’ll talk generally about C++ development (plus a few.
Command line arguments. – main can take two arguments conventionally called argc and argv. – Information regarding command line arguments are passed to.
An Introduction to C Programming (assuming that you already know Java; this is not an introduction to C++)
Intro. to Game Programming Want to program a game?
CS162B: Assembly and C Jacob T. Chan. Objectives ▪ System calls ▪ Relation of System calls to Assembly and C ▪ Special System Calls (exit, write, print,
Review C++ exception handling mechanism Try-throw-catch block How does it work What is exception specification? What if a exception is not caught?
University of Calgary – CPSC 441. C PROGRAM  Collection of functions  One function “main()” is called by the operating system as the starting function.
F28PL1 Programming Languages Lecture 7 Programming in C - 2.
Input & Output: Console
Variables, Functions & Parameter Passing CSci 588 Fall 2013 All material not from online sources copyright © Travis Desell, 2011.
 200 Total Points ◦ 74 Points Writing Programs ◦ 60 Points Tracing Algorithms and determining results ◦ 36 Points Short Answer ◦ 30 Points Multiple Choice.
CS 114 – Class 02 Topics  Computer programs  Using the compiler Assignments  Read pages for Thursday.  We will go to the lab on Thursday.
“In mathematics and computer programming, Index notation is used to specify the elements of an array of numbers”
Week 6 - Wednesday.  What did we talk about last time?  Exam 1!  And before that…  Review!  And before that…  Arrays and strings.
C Programming in Linux Jacob Chan. C/C++ and Java  Portable  Code written in one system and works in another  But in C, there are some libraries that.
1 File Handling. 2 Storage seen so far All variables stored in memory Problem: the contents of memory are wiped out when the computer is powered off Example:
Weeks 5-6 Pointers and Arrays Basic pointer type Pointers and Arrays Address arithmetic Pointer Arrays User-defined data types Structures Unions Pointers.
C++ / G4MICE Course Session 2 Basic C++ types. Control and Looping Functions in C Function/method signatures and scope.
 140 Total Points ◦ 100 Points Writing Programs ◦ 24 Points Tracing Algorithms and determining results ◦ 16 Points Short Answer  Similar to quizzes.
Topic 3: C Basics CSE 30: Computer Organization and Systems Programming Winter 2011 Prof. Ryan Kastner Dept. of Computer Science and Engineering University.
Objective: Students will be able to: Declare and use variables Input integers.
Strings Programming Applications. Strings in C C stores a string in a block of memory. The string is terminated by the \0 character:
 200 Total Points ◦ 75 Points Writing Programs ◦ 60 Points Tracing Algorithms and determining results ◦ 35 Points Short Answer ◦ 30 Points Multiple Choice.
C++ / G4MICE Course Session 1 - Introduction Edit text files in a UNIX environment. Use the g++ compiler to compile a single C++ file. Understand the C++
CS415 C++ Programming Takamitsu Kawai x4212 G11 CERC building WV Virtual Environments Lab West Virginia University.
General Computer Science for Engineers CISC 106 Lecture 12 James Atlas Computer and Information Sciences 08/03/2009.
A Quick Look at C for C++ Programmers Noah Mendelsohn Tufts University Web: COMP.
CSE 332: C++ pointers, arrays, and references Overview of Pointers and References Often need to refer to another object –Without making a copy of the object.
Gramming An Introduction to C Programming (assuming that you already know Java; this is not an introduction to C++)
C Primer Session – 1/25/01 Outline Hello World Command Line Arguments Bit-wise Operators Dynamic Memory / Pointers Function Parameters Structures.
C Part 1 Computer Organization I 1 August 2009 © McQuain, Feng & Ribbens A History Lesson Development of language by Dennis Ritchie at Bell.
File I/O in C++. Using Input/Output Files A computer file  is stored on a secondary storage device (e.g., disk);  is permanent;  can be used to provide.
Files A collection of related data treated as a unit. Two types Text
C++ Namespaces, Exceptions CSci 588: Data Structures, Algorithms and Software Design All material not from online sources copyright © Travis Desell, 2011.
CSIS 123A Lecture 7 Static variables, destructors, & namespaces.
C is a high level language (HLL)
File I/O in C++ I. Using Input/Output Files A computer file is stored on a secondary storage device (e.g., disk); is permanent; can be used to provide.
C Programming Day 2. 2 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003 Version No. 1.0 Union –mechanism to create user defined data types.
CSE 232: Moving Data Within a C++ Program Moving Data Within a C++ Program Input –Getting data from the command line (we’ve looked at this) –Getting data.
Ms N Nashandi Dr SH Nggada 2016/01/03Ms N Nashandi and Dr SH Nggada1 Week 6 -File input and output in C++
INTRODUCTION TO PROGRAMING System Development Mansoura October 2015.
Chapter 7 Process Environment Chien-Chung Shen CIS/UD
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
The Machine Model Memory
A bit of C programming Lecture 3 Uli Raich.
Command Line Arguments
Command Line Arguments
File Input/Output.
CSC 253 Lecture 8.
CS 2308 Final Exam Review.
File I/O We are used to reading from and writing to the terminal:
CS 1430: Programming in C++ Turn in your Quiz1-2 No time to cover HiC.
CSC 253 Lecture 8.
CS 2308 Exam I Review.
Basic File I/O and Stream Objects
File Handling.
File I/O in C++ I.
Functions Reasons Concepts Passing arguments to a function
EE 312 Exam I Review.
File I/O in C++ I.
CS 2308 Final Exam Review.
File I/O We are used to reading from and writing to the terminal:
Advanced OS COMP 755.
Presentation transcript:

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 ▪ 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)

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

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)

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

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

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

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

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)

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

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

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