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

Reading Data from File, Outputting Data to a File - Notes | ENGR 0012, Study notes of Engineering

Material Type: Notes; Class: INTRO TO ENGINEERING COMPUTING; Subject: Engineering; University: University of Pittsburgh; Term: Spring 2007;

Typology: Study notes

Pre 2010

Uploaded on 09/02/2009

koofers-user-ksv
koofers-user-ksv 🇺🇸

10 documents

1 / 2

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1
ENGR 0012 – Dr. Lund’s 10am Section
March 20, 2007
Section 5.5
Reading data from a file
(See Ex1.cpp, Ex2.cpp, Ex3.cpp)
Inputting data from a file - Summary
Inputting data from a file requires 4 primary
statements:
1. Declaration of file pointer variable:
FILE *file_ptr
2. Opening file:
file_ptr = fopen(“filename”, “r”);
3. Reading file data:
fscanf(file_ptr , “formatting”, &variable)
4. Closing file (Clears buffer of file info):
fclose(file_ptr);
#include <stdio.h>
main()
{double col2;
int r1c1, r2c1;
FILE *data1;
data1=fopen("stats.txt","r");
fscanf(data1, "%d", &r1c1);
fscanf(data1, "%d; %lf", &r2c1, &col2);
printf("r1c1 = %d\nr2c1 = %d\ncol2 = \
%lf\n\n", r1c1, r2c1, col2);
fclose(data1);
}
Note that the formatting string for fscanf
requires knowledge of the formatting of
the data in the file, i.e. what separates
the values, what kind of values, etc.
pf2

Partial preview of the text

Download Reading Data from File, Outputting Data to a File - Notes | ENGR 0012 and more Study notes Engineering in PDF only on Docsity!

ENGR 0012 – Dr. Lund’s 10am Section

March 20, 2007

  • Section 5.

Reading data from a file

(See Ex1.cpp, Ex2.cpp, Ex3.cpp)

Inputting data from a file - Summary

Inputting data from a file requires 4 primary

statements:

1. Declaration of file pointer variable:

FILE *file_ptr

2. Opening file:

file_ptr = fopen(“filename”, “r”);

3. Reading file data:

fscanf(file_ptr , “formatting”, &variable)

4. Closing file (Clears buffer of file info):

fclose(file_ptr);

#include <stdio.h> main() { double col2; int r1c1, r2c1; *FILE data1;

data1= fopen ("stats.txt","r");

fscanf (data1, "%d", & r1c1); fscanf (data1, "%d; %lf", & r2c1, & col2);

printf("r1c1 = %d\nr2c1 = %d\ncol2 =
%lf\n\n", r1c1, r2c1, col2);

fclose (data1); }

Note that the formatting string for fscanf requires knowledge of the formatting of the data in the file, i.e. what separates the values, what kind of values, etc.

Outputting data to a file

#include <stdio.h> main() { int value = 10; *FILE data1;

data1= fopen (“output.txt",“ w ");

fprintf(data1, “Integer value = %d”, value);

fclose (data1); }

Like inputting data from a file, outputting data

to a file also requires 4 primary statements:

1. Declaration of file pointer variable:

FILE *file_ptr

2. Opening file:

file_ptr = fopen(“filename”, “w”);

3. Writing data to file:

fprintf(file_ptr, “formatting”, variables)

4. Closing file (Clears buffer of file info):

fclose(file_ptr);

When writing data to a file, the access mode in the fopen statement is “w”.

Reading Data From File

What if you don’t know the size of the data file?

FILE *indat;

double x[1000], y[1000];

int i = 0, size;

indat = fopen (“filename”, “r”);

while(fscanf(indat, "%lf %lf", &x[i], &y[i]) == 2) i++;

size = i;

The function fscanf can return an integer which represents how many

numbers it successfully read. When it gets to the end of the file, it won’t

have any numbers to read and will return EOF = -