Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Lab 9 - Fundamentals of Computer Programming | THTR 101, Lab Reports of Acting

Material Type: Lab; Class: Introduction to Acting; Subject: Theatre; University: University of Southern California; Term: Fall 2005;

Typology: Lab Reports

Pre 2010

Uploaded on 11/08/2009

koofers-user-qtw
koofers-user-qtw 🇺🇸

10 documents

1 / 3

Toggle sidebar

Related documents


Partial preview of the text

Download Lab 9 - Fundamentals of Computer Programming | THTR 101 and more Lab Reports Acting in PDF only on Docsity! CSCI 101: Fundamentals of Computer Programming Lab 9: File I/O Fall 2005 Introduction Inputs and outputs are an integral part of most programs. As the number of input grow, so does the need for an automated mechanism for data entry. Hence, the introduction of file I/O – the methods associated for reading and writing to a file. In this lab session, we will primarily be focusing on the former which is reading a file and manipulating the data retrieved. There are generally two flavors of files that reside in our computers, binary and text files. A binary file like its namesake consists of ones and zeroes, while text files consist of ASCII characters. ASCII characters are generally the characters that users can type on the keyboard. Or, one can also look at it from another angle – that text files are files that are used by human users, while binary files are used by machines. An example of a text file would be a simple C file that consist one or multiple C programs, while the binary file would be the a.out file that is a result of compiling the C file. Because of this, the compiler is also sometimes viewed as the translator between a text and a binary file. To learn file I/O, a new variable type needs to be introduced; FILE. The FILE data type is simple a file pointer that points to the file that is to be accessed. Once the file pointer is initialized, it is linked to the file by an fopen command. fopen takes in the filename and the access method as arguments. After the file is opened, it is time for data collection. This is done using the fscanf function. fscanf is almost identical to its regular scanf counterpart, except that it also takes in the file pointer as an argument, and returns an integer type. The return value will be the number of successful reads performed using the scanf function, or the EOF control character. The EOF (End of file) control character will be automatically generated at the end of the file by the system when it is saved. Because of this unique property, a loop is often used in association with this to allow the program to read the entire file before quitting. After the file is read, fclose is called to close the file. Example We shall illustrate the file reading mechanism with a program example. This program will open a data file called lab9example.dat. This data file will only contain a list of integers. It will then read in the data and outputs it to the computer screen in the order it is read. Download this program at http://scf.usc.edu/~csci101/labs/lab9file.c #include <stdio.h> int main(){ // initializing the file pointer and other variables FILE *fp; int flag, num; // opening the file to read fp = fopen("lab9example.dat", "r"); // check if the file is valid. if (fp == NULL) printf("Error opening file\n"); else{ // starts reading in the numbers flag = fscanf(fp, "%d", &num); // continue reading until the end of file is reached. while(flag != EOF){ counter ++; // output the numbers read printf("Number scanned: %d\n", num); flag = fscanf(fp, "%d", &num); } printf("EOF reached. \n"); // closing the file when done reading fclose(fp); } return 0; } Notice that no additional libraries are needed to be included as all the file I/O functions are in the stdio library. Also note that the second argument in the fopen function is the string r which stands for read. Other alternatives that we will learn later on include w for write, and a for append. If you try running the file now, you should encounter an error and a printout stating Error opening file on the screen. This is because the file example.dat hasn’t been created yet. Hence, it cannot be opened. Copy the lab10example.dat file from http://scf.usc.edu/~csci101/labs/lab9example.dat for this exercise, and store it in the same directory as this program in your UNIX account. You will have to use an FTP program for this.
Docsity logo



Copyright © 2024 Ladybird Srl - Via Leonardo da Vinci 16, 10126, Torino, Italy - VAT 10816460017 - All rights reserved