Download presentation
Presentation is loading. Please wait.
Published byMitchell Williamson Modified over 9 years ago
1
© Janice Regan, CMPT 128, Jan 2007 0 CMPT 128: Introduction to Computing Science for Engineering Students C File Input and Output Checking input for errors
2
C file input and output Most of this set of notes shows only the differences from C++ file input and output. A few slides in this set of notes discusses the pitfalls of using the end of file test to determine when to stop reading data. These slides are labelled to indicate they apply to both C and C++ © Janice Regan, CMPT 128 2007-2013 1
3
2 Reading/Writing data (1) Use scanf to read data from the keyboard scanf(“value of num is %lf \n”,num); Waits on-screen for keyboard entry Value entered at keyboard is placed in the memory location reserved for variable num, that is the entered value is "assigned" to num
4
© Janice Regan, CMPT 128 2007-2013 3 Reading/Writing data (2) printf( “Enter the value of num: %d“, num); Value from the memory location reserved for variable num, is displayed on the screen “Enter the value of num: %d“ is called the format statement
5
© Janice Regan, CMPT 128 2007-2013 4 C File I/O Libraries To access the objects and functions needed for file input and output You need the library stdio.h #include
6
© Janice Regan, CMPT 128 2007-2013 5 Connect to your file First declare a pointer to the file identifier, You will refer to the file using the file identifier in the remainder of your code. This process of connecting to the file is called “opening the file”, To connect you make the pointer point at the file FILE *myInFileId = NULL; myInFileId = fopen(“infile.txt”, “r”) FILE *myOutFileId = NULL: myOutFileId = fopen(“outfile.txt”, “w”);
7
© Janice Regan, CMPT 128 2007-2013 6 Checking if the file opened Need to check that the file was actually opened FILE *myInFileId = NULL; fopen(“infile.txt“, “r”); if ( myInFIleId == NULL ) { printf("File open failed.\n“); exit(1); }
8
© Janice Regan, CMPT 128 2007-2013 7 Using files for input / output Within C programs information is read from files or written to files The file name (path) of the file saved on the computer’s disk for example infile.txt or outfile.txt is used only to connect to the file in your program All access to the file once it has been opened is done using the file ID for example myInFileId or myOutFileId
9
© Janice Regan, CMPT 128 2007-2013 8 Reading data from a file To read from your file You have now declared and connected to your file, To read data from your input stream use myInFileId in an fscanf, to indicate which file your are reading from double mouse=0.0; fscanf( myInFileId, “%lf”, &mouse);
10
© Janice Regan, CMPT 128 2007-2013 9 Writing data to a file cout is an output stream You have now declared and connected to your own output stream To write data to this stream use myOutFileId in your fprint statement to indicate which file you are writing to int cat=0; fprintf(myOutFileId, “number of cats is %d”, cat);
11
Variable types for output streams In C when we print to the screen we usually use printf(), we can also use fprintf() The first value inside the brackets of the fprintf( ) is a variable of type FILE * stdout and stderr are predefined variables of type FILE* stdout = standard output stderr = standard error © Janice Regan, CMPT 128 2007-2013 10
12
Printing information to the screen When we would print using the cout stream in C++, we can use the standard output in C The standard output is stdout Two equivalent approaches printf( “This is note number %d\n”, num); fprintf( stdout, “This is note number %d\n”, num); © Janice Regan, CMPT 128 2007-2013 11
13
Printing errors to the screen When we would print using the cerr stream in C++, we can use the standard error output in C Standard error output is stderr fprintf( stderr, “This is note number %d\n”, num); © Janice Regan, CMPT 128 2007-2013 12
14
Reading information from the keyboard When we would read using the cin stream in C++, we can use the standard input in C The standard output is stdin Two equivalent approaches scanf( “%d”, &num); fscanf( stdin, “%d”, &num); © Janice Regan, CMPT 128 2007-2013 13
15
© Janice Regan, CMPT 128 2007-2013 14 Breaking the connection When the program has completed reading from or writing to the file, the connection to the file is no longer needed. The programmer needs to break the connection between the program and the file fclose(myInFileId); fclose(myOutFileId);
16
© Janice Regan, CMPT 128 2007-2013 15 Formatting Output We have seen how to format C output using C conversions using a printf statement We can use the same C conversions in the same ways to format information we are writing to a file using an fprintf statement
17
© Janice Regan, CMPT 128 2007-2013 16 Appending to a File FILE *myOutFileId == NULL; myOutFileId = fopen("important.txt", “a”); If file doesn’t exist it will be created If file already exists information will be added to the end of the existing file
18
© Janice Regan, CMPT 128 2007-2013 17 Checking if data was read // Data is read from a file, one line (three values at a time // Each time a line is read the cost is updated // After all data is read the cost is printed to the screen FILE *inFile = NULL; double v1, cost; inFIle = fopen(“filename”, “r”); // check if file has been opened while ( fscanf(inFile, “%lf “, v1) == 1) { cost += v1; } fclose(inFile); printf(“total cost is %lf \n“,cost);
19
return value of printf or fprintf while ( fscanf(inFile, “%lf “, v1) == 1) The statement in red is a function call When a function is called and executes it will return a value. The functions fscanf and scanf return an integer The integer represents the number of variables that were read successfully by the printf or fprintf statement © Janice Regan, CMPT 128 2007-2013 18
20
return value of printf or fprintf if fprintf(inFile, “%lf %d %f“, v1, v2, v3) If a writing error occurs returns a negative number If writing is successful returns the number of characters written © Janice Regan, CMPT 128 2007-2013 19
21
return value of scanf or fscanf if fscanf(inFile, “%lf %d %f“, &v1, &v2, &v3) Reads beyond the end of file its return value is -1 Is unable to read the 1 st character of the 1 st variable because that character cannot be part of a variable of the type of the first variable its return value is 0 Reads the value of the 1 st variable but is unable to read the 1st character of the 2 nd variable its return value is 1 Reads the value of two variables but is unable to read the 1 st character of the 3 rd variable its return value is 2 Successfully reads all three variables its return value is 3 © Janice Regan, CMPT 128 2007-2013 20
22
© Janice Regan, CMPT 128 2007-2013 21 Checking end of file // Data is read from a file, one line (three values at a time // Each time a line is read the cost is updated // After all data is read the cost is printed to the screen FILE *inFile = NULL; double v1, cost; inFIle = fopen(“filename”, “r”); // check if file has been opened while ( !feof()) { fscanf(inFile, “%lf %d %f “, &v1, &v2, &v3); cost += v1; } fclose(inFile); printf(“total cost is %lf \n“,cost);
23
Using EOF in C and C++ In some examples we have checked to see if we were at the end of the file. The test for end of file is only true when the EOF bit is set. This bit is set when an attempt is made to read BEYOND the end of the file When the last piece of data in the file is read, there has been no attempt to read beyond the end of the file so the test for EOF will return false (not at EOF) If there is a new line or space after the last variable’s value in the file, then you will need one read to read the blank line or space and one additional read to read beyond the end of the file © Janice Regan, CMPT 128 2007-2013 22
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.