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

File Input and Output - Introduction to C Programming for Engineers | CSE A205, Study notes of Engineering

Material Type: Notes; Class: Introduction to C Programming for Engineers; Subject: Computer Systems Engineering ; University: University of Alaska - Anchorage; Term: Spring 2009;

Typology: Study notes

2009/2010

Uploaded on 03/28/2010

koofers-user-cln
koofers-user-cln 🇺🇸

10 documents

1 / 8

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CSE294A
Introduction to C Programming for Engineers
Lecture #20
Jeffrey Miller, Ph.D.
pf3
pf4
pf5
pf8

Partial preview of the text

Download File Input and Output - Introduction to C Programming for Engineers | CSE A205 and more Study notes Engineering in PDF only on Docsity!

CSE294A

Introduction to C Programming for Engineers

Lecture

Jeffrey Miller, Ph.D.

File Input/Output

Files are used for data persistence

Files are stored on secondary storage devices, suchas hard disks, CDs, flash drives, etc.

When a file is “opened” in a program, an object iscreated and a stream is associated with the object

For file processing in C, the functions are locatedin the stdio.h header file, so no additional headerfiles are needed

File Open Modes

r

read from a file

w

write to a file

a

append to a file

rb

read from a binary file

wb

write to a binary file

ab

append to a binary file

Sequential File Reading

Here is the code to read from a file sequentially

#include <stdio.h> void main()

FILE

*f_ptr; f_ptr = fopen(“input.txt”, “r”); if (f_ptr

NULL)

printf (“File could not be opened.”); return; } int num; fscanf(f_ptr, “%d”, &num); printf (“number from file

%d\n”, num); fclose(f_ptr); }

Program

Write a file copy program. The program will ask the userto enter the input file and the output file, then will copy thecontents of the input file to the output file.

Homework

Homework #8 is posted!