Last updated 10/30/18
Input/Output Streams Information flow between entities is done with streams Keyboard Text input stream data stdin Data Text output stream Monitor stdout stderr printf formats data for the text output stream scanf formats data for the text input stream #include <stdio.h> 2 tj
Input/Output Stream I/O formatting src: Forouzan 3 tj
Input/Output Stream I/O formatting 4 src: Forouzan tj
Input/Output printf Combines text and data and inserts it into the output stream text and data conversion is contained in double quotes data is comma separated data conversion is identified as %xxx % [flag] [min width] [precision] [size] code %+6.3Lf +12.345 sign, 6 total, 3 fractional, Long float 5 tj
Input/Output printf printf( %d%c%f, 12, a, 5.3); 12a5.300000 printf( %d %c %f, 12, a, 5.3); 12 a 5.300000 int z = 51; printf( %d %f %c %x, z, z, z, z); 51 51.00000 3 33 printf( the value of z is: %d, z); the value of z is: 51 printf{ I think %d is the value of z, z); I think 51 is the value of z Special characters are preceded by \ \n new line, \t tab, \% -%, \ printf{ I think \ %d\ is the value of z, z); I think 51 is the value of z 6 tj
Input/Output scanf Extracts data from an input stream and formats it lots of options requires a pointer (addresses) for any variables text and data conversion is contained in double quotes variable pointers are comma separated whitespace is ignored except for characters data conversion is identified as %xxx % [flag] [max width] [size] code 7 tj
Input/Output scanf 123 456 7a scanf( %d%d%d%c, &a, &b, &c, &d); a = 123, b=456, c=7, d= a 123 456 7 a scanf( %d%d%d %c, &a, &b, &c, &d); a = 123, b=456, c=7, d= a 14/15 16/17 scanf( %2d/%2d %2d/%2d, &num1, &den1, &num2, &den2); num1 = 14, den1 = 15, num2 = 16, den2 = 17 8 tj
File Formats Files can contain information in 2 different formats text Text Characters Line terminated by a newline (\n) Binary Raw bytes File terminated by end of file EOF short int 257 257A char A 0011 0010 0011 0101 0011 0111 0100 0001 0000 0001 0000 0001 0100 0001 asci 2 5 7 A binary hex 257 257A A 9 tj
File I/O - text Need to create a stream to transfer the data to/from the file from/to our program Identify the stream by name Use a pointer FILE* pointer_name; FILE* spstudentdata; 10 tj
File I/O - text Need to identify the file we are creating the stream to/from open the file assign the pointer to the opened file file_pointer = fopen( filename, mode ); spstudentdata = fopen( ee1910.dat, w ); spstudentdata = fopen( C:\\users\\tim\\winter\\ee1910.dat, r ); 11 tj
File I/O - text Open file modes r w a read only, start at beginning if does not exist error write only, start at beginning (erase all contents) if does not exist creates it append only, start at end of current data if does not exist creates it Returns address of file or NULL if an error occurs 12 tj
File I/O - text Close a file fclose(file_pointer); fclose(spstudent); 13 tj
File I/O - text #include <stdio.h> int main{ FILE* spaddress; if((spaddress = fopen( addresses.dat, r )) == NULL){ printf( Error opening file addresses.dat\n ); exit (100); // terminate program } // end if } // end main 14 tj
File I/O - text Formatting stream data fprintf(stream_pointer, control_string, vars) fscanf(stream_pointer, control_string, &vars) 15 tj
File I/O - text Read 3 integers from my_data_file.dat 16 tj
File I/O - text Read 3 integers from my_data_file.dat int j,k,l; FILE* spdata; spdata = fopen( my_data_file.dat, r ); fscanf(spdata, %d %d %d, &j, &k, &l); fclose(spdata); 17 tj
File I/O - text Append a date to my_data_file.dat in m-d-y format 18 tj
File I/O - text Append a date to my_data_file.dat in m-d-y format FILE* spdata; spdata = fopen( my_data_file.dat, a ); fprintf(spdata, %d-%d-%d, m, d, y); fclose(spdata); 19 tj
File I/O - text Read the date from my_data_file.dat m-d-y format 20 tj
File I/O - text Read the date from my_data_file.dat m-d-y format int m,d,y; FILE* spdata; spdata = fopen( my_data_file.dat, r ); fscanf(spdata, %d-%d-%d, &m, &d, &y); fclose(spdata); 21 tj
/* text_file_example.c Created by johnsontimoj Rev 0, 11/15/17 */ // read and write to a text file /////////////////////////////////////////////// // no need to recreate the stream pointer // open existing file spmydata = fopen("mydatafile.dat", "r"); #include <stdio.h> int main(void){ setbuf(stdout, NULL); // disable buffering int foo = 24; char mystring[] = "hello"; // create a stream pointer for the file FILE * spmydata; //create a new file spmydata = fopen("mydatafile.dat", "w"); // write to the file fprintf(spmydata, "%c ", 'A'); fprintf(spmydata, "%i ", foo); fprintf(spmydata, "%x ", foo); fprintf(spmydata, "%s ", mystring); // close the file fclose(spmydata); // read the data char noo; int boo; int loo; char mynewstring[6]; fscanf(spmydata, "%c", &noo); fscanf(spmydata, "%i", &boo); fscanf(spmydata, "%x", &loo); fscanf(spmydata, "%s", mynewstring); // print results printf("%c\n", noo); printf("%i\n", boo); printf("%x\n", loo); printf("%s\n", mynewstring); fclose(spmydata); return 0; } // end main 22 tj
File I/O - binary Need to identify the file we are creating the stream to/from open the file assign the pointer to the opened file file_pointer = fopen( filename, mode ); spstudentdata = fopen( ee1910.bin, rb ); spstudentdata = fopen( C:\\users\\tim\\winter\\ee1910.bin, ab ); 23 tj
File I/O - binary Open file modes rb wb ab read binary only, start at beginning if does not exist error write binary only, start at beginning (erase all contents) if does not exist creates it append binary only, start at end of current data if does not exist creates it Returns address of file or NULL if an error occurs 24 tj
File I/O - binary Open file modes r+b w+b a+b read binary (can write), start at beginning if does not exist error write binary (can read), start at beginning (erase all contents) if does not exist creates it append binary (can read), start at end of current data if does not exist creates it Returns address of file or NULL if an error occurs 25 tj
File I/O - binary Close a file fclose(file_pointer); fclose(spstudent); 26 tj
File I/O - binary #include <stdio.h> int main{ FILE* spaddress; if((spaddress = fopen( addresses.bin, rb )) == NULL){ printf( Error opening file addresses.bin\n ); exit (100); // terminate program } // end if } // end main 27 tj
File I/O - binary Formatting stream data Block format no conversions, raw bytes int fread( void* in_location_ptr, int element_size, int count, FILE* stream_ptr); returns the # of items read 28 tj
File I/O - binary read 3 integers at a time until all read FILE* spdata; int numread; int myarray[3]; spdata = fopen( my_int_file.bin, rb ); while((numread = fread(myarray, sizeof(int), 3, spdata))!= 0){ do something with the array } // end while 29 tj
File I/O - binary read 3 integers FILE* spdata; int myarray[3]; spdata = fopen( my_int_file.bin, rb ); fread(myarray, sizeof(int), 3, spdata); 30 tj
File I/O - binary Formatting stream data Block format no conversions, raw bytes int fwrite( void* out_location_ptr, int element_size, int count, FILE* stream_ptr); returns the # of items written 31 tj
File I/O - binary write 1 integer FILE* spdata; int foo; spdata = fopen( my_int_file.bin, wb ); fwrite(&foo, sizeof(int), 1, spdata); 32 tj
File I/O binary write out a student record (structure) typedef struct{ int birthyear; int id; char[40] name } STUDENT; STUDENT onestudent = {1990, 1234, { f, o, o }}; STUDENT* onestudent_ptr = &onestudent int numout; FILE* spdata; numout = fwrite(onestudent_ptr, sizeof(student), 1, spdata); 33 tj
/* data_file_example.c Created by johnsontimoj Rev 0, 11/15/17 */ // read and write to a data file #include <stdio.h> int main(void){ setbuf(stdout, NULL); // disable buffering // create a stream pointer for the file FILE * spmydata; //create a new file spmydata = fopen("mydatafile.bin", "wb"); // write to the file int i; for(i = 0; i < 20; i++){ fwrite(&i, sizeof(int), 1, spmydata); } // close the file fclose(spmydata); /////////////////////////////////////////////// // no need to recreate the stream pointer // open existing file spmydata = fopen("mydatafile.bin", "rb"); // read the data int mynewdata[20]; for(i = 0; i < 20; i++){ fread(&mynewdata[i], sizeof(int), 1, spmydata); } // print results for(i = 0; i < 20; i++){ printf("%i ", mynewdata[i]); } fclose(spmydata); return 0; } // end main 34 tj