

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
Material Type: Notes; Class: INTRO TO ENGINEERING COMPUTING; Subject: Engineering; University: University of Pittsburgh; Term: Spring 2007;
Typology: Study notes
1 / 2
This page cannot be seen from the preview
Don't miss anything!
ENGR 0012 – Dr. Lund’s 10am Section
March 20, 2007
Reading data from a file
Inputting data from a file - Summary
#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.
#include <stdio.h> main() { int value = 10; *FILE data1;
data1= fopen (“output.txt",“ w ");
fprintf(data1, “Integer value = %d”, value);
fclose (data1); }
When writing data to a file, the access mode in the fopen statement is “w”.