File I/O using File Pointers: Reading and Writing Files in C, Study notes of Computer Science

An introduction to performing file input/output (i/o) using file pointers in c programming. It covers opening and closing files, character and string input/output, and includes a sample program to compute the average of a list of integers from an input file and write the result to an output file. Related topics include using file as a structure, opening files with different modes, and using fprintf and fscanf functions.

Typology: Study notes

Pre 2010

Uploaded on 03/16/2009

koofers-user-zmv
koofers-user-zmv 🇺🇸

10 documents

1 / 15

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff

Partial preview of the text

Download File I/O using File Pointers: Reading and Writing Files in C and more Study notes Computer Science in PDF only on Docsity!

24-

-^ Perform File I/O using file pointers •^ FILE *^

data-type

•^ Opening and closing files •^ Character Input and Output •^ String Input and Output •^ Related Chapters: FER Chapter 15

24-

1. Problem Definition^ Write a program that computes the average of a list ofintegers. The program should prompt the user for thename of both the input and output files and then readthe values from the input file and print the averagevalue in the output file. 2. Refine, Generalize, Decompose the problem definition^ (i.e., identify sub-problems, I/O, etc.)^ Input = String naming the file of integers to be readand a string naming the output file. Also, the integers inthe file.^ Output = The average value is written to the output file.

/* C Program to compute the average of a list of numbers. / #include <stdio.h> void main(void){^ int value,total = 0,count = 0;^ / fileptrIn and fileptrOut are variables of type “FILE *” /FILE * fileptrIn, * fileptrOut;^ char filenameIn[100],filenameOut[100];^ printf("Please enter an input filename (use path if needed):");^ scanf("%s",filenameIn);^ printf("Please enter an output filename (use path if needed):");^ scanf("%s",filenameOut);^ / open files to read “r” and write “w”

*/

fileptrIn = fopen(filenameIn, "r"); fileptrOut = fopen(filenameOut, "w");

After the above program is compiled execution from theUnix prompt: > more input.dat 1 2 3 4 5 > ./a.out Please enter an input filename (use path if needed):input.dat Please enter an output filename (use path if needed):output.dat > more output.dat Ave of 5 numbers = 3.000000 >

24-

FILE^ (all caps) is a structure defined in

<stdio.h>.

In CS101 we will only use this data type in thedeclaration of pointer variables. Examples:

FILE^ *ptrFileIn; FILE^ *ptrFileOut; declares^ ptrFileIn

and^ ptrFileOut

as pointer

variables. They both “point” to values of data-type FILE. We must have

#include

<stdio.h>

in our

source file to use

FILE^ pointers.

24-

FILE^ *fileOut; fileOut^ =

fopen("output.dat",

"w");

The first argument “output.dat” is a string that names the file to be read. The second argument the mode, “ w ”,stands for “write” mode only. fopen^ returns a pointer to the file(in this case (

output.dat

).

Example: If an already existing file is opened with mode “w”

the

old contents are discarded.

24-

FILE^ *fileOut; fileOut^ =

fopen("out.dat",

"a");

The first argument “out.dat” is a string that names the file the you to which you want to write .The writing takes place at the end of the original file. If the file out.dat already exists it will not be destroyed asin the case for “w” mode. fopen^ returns a pointer to the file(in this case (

out.dat).

Example:

24-

fprintf^ function is in

<stdio.h>

.^ It is the

same as the

printf^ function except that the first argument for

fprintf^

is a file pointer:

fprintf(ptrFileOut,

ctrlString,

expression(s));

where^ ctrlString

contains the format specifiers, and

expression(s)

can contain

one or more variable names and constants. Examples

: fprintf(ptrFileOut,

"%i^ ",^ intVlaue); fprintf(ptrFileOut,

"%f^ ",^ a

+^ 5.0^ *^

b);

24-

fscanf^ function is also in

<stdio.h>

.^ It is the

same as the

scanf^ function except that the first argument for

fscanf^ is a file pointer; i.e., fscanf(ptrFileIn,

ctrlString,address(es)); Examples

  • fscanf(ptrFileIn,

"%lf",^ &dataValue); fscanf(ptrFileIn,

"%c%s%i",

&ch,^ str,

&num);

  • returns an int value equal to number of itemssuccessfully read from file (e.g., 0, 1, 2, 3 in lastexample), or returns the value of EOF if end of file isencountered before any values are read in.

Thus,

fscanf can be used as an input check just like scanf.