File - C Sharp Programming - Lecture Slides, Slides of C programming

Some concept of C Sharp Programming are Additional Controls, Declaring Arrays, Call-By-Reference Methods, Information Processing Cycle. Main points of this lecture are: File, Information Processing Cycle, File Processing, Memory, Input Devices, Output Devices, Storage Devices, Picture, Input, Temporary

Typology: Slides

2012/2013

Uploaded on 04/27/2013

farooq
farooq 🇮🇳

4.3

(94)

203 documents

1 / 29

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
File I/O
11_file_processing.ppt
Docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d

Partial preview of the text

Download File - C Sharp Programming - Lecture Slides and more Slides C programming in PDF only on Docsity!

File I/O

11_file_processing.ppt

Overview of Topics

• Information Processing Cycle

• File Processing (I/O)

Information Processing Cycle

Input Raw Data

Process (Application)

Output Information

Storage Output from one process can

serve as input to another process.

Storage is referred to as secondary storage, and it is permanent storage. Data is permanently stored in files.

Input and Output (I/O)

  • In the prior assignments, we would enter the test

data and check the results.

  • If it was incorrect, we would change the program,

run it again, and reenter the data.

  • Depending on the application, it may be more

efficient to capture the raw data the first time it is

entered and store in a file.

  • A program or many different programs can then

read the file and process the data in different

ways.

  • We should capture and validate the data at its

points of origination (ie cash register, sales clerk).

coffees.txt

Chocolate Almond

Espresso Roast Jamaican Blue Mtn.

Kona Blend Vanilla Nut

  • This is a simple text file.
  • The file can be created with Notepad or some

other text editor.

  • Just enter values and press return at the of each

record, including after the last one.

  • The file should be saved in the Debug folder

within the bin folder of the project folder.

  • CS11ex > bin > Debug > coffees.txt

Sample Interface

Menu Option: File -> Load List

  • Use an OpenFileDialog box (Windows 7)

Load File Method – Part 1

private void mnuFileLoad_Click(object sender, EventArgs e) { //Not checking if a list has already been loaded string strFileName; string strFlavorName;

//Open the file and load the list box with the data stored in the file try { OpenFileDialog openFileDialog1 = new OpenFileDialog(); openFileDialog1.Filter = "txt files (.txt)|.txt|All files (.)|." ; openFileDialog1.FilterIndex = 1; // At first only show text files. openFileDialog1.InitialDirectory = Application.StartupPath; //default folder openFileDialog1.RestoreDirectory = true; // restore path to default folder

Internal and External File Names

  • coffees.txt is the external file name.
  • flavorsFileIn is the internal file name.
  • FileStream flavorsFileIn = new FileStream("coffees.txt", FileMode.Open);

connects the internal name to the external file.

  • Naming conventions for the external file names vary by

Operating Systems (OS).

  • Internal names conform to variable naming rules.
  • In new FileStream is the only place we see the external name.

Use Try/Catch When Opening Files

  • Some possible errors:
    • File not found
    • Protection violation on network
    • Disk Full

EOF Flag

  • Flag is a term used for variables that can have two

possible values.

  • On or off, 0 or 1
  • Y or N, True or false
  • EOF? – is it at the end of the file or not?
  • In this example the Peek method is used to check

for EOF.

  • Minus 1 (-1) is return when the end of file is reach

while (flavorsStreamReader.Peek( ) != -1)

Close File

flavorsStreamReader.Close( );

• Close releases file to Operating System (OS).

• Other users may get file locked error if file is

not closed.

• Good housekeeping.

Form Closing Event

  • If user’s click on a Close button or Exit menu item, we can capture those events.
  • If the user clicks on the close window icon, an event is not fired.
  • However, when the form is instructed to close (this.Close), the Form Closing event is fired, and we can add a handler for that event.
  • Create a method and add the code to check if the data has been saved in the form closing method.
  • After writing this method to handle the FormClosing event, its needs to be assigned as the form's FormClosing event handler while in Design Mode.
  • After assigning it to the form, the method is automatically executed when the form is instructed to close.
  • In the Close button or Exit menu method, just call this.Close( ) and that will trigger the Form Closing event.

Exit and Closing Methods

private void mnuFileExit_Click( … ) { //cblnIsDataSaved checked in Form Closing event procedure. this.Close( ); }

private void frmCS11ex_FormClosing( … ) { DialogResult dgrResponse;

if (cblnIsDataSaved = = false) { dgrResponse = MessageBox.Show("Do you wish to save the list?", "Save", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question); if (dgrResponse = = DialogResult.Yes) mnuFileSave_Click( mnuFileSave, new EventArgs( )); if (responseDialogResult == DialogResult.Cancel) e.Cancel = true; //cancel close event } }