Lecture 8: Files - Programmeringsteknik DT143G PDF
Document Details
Uploaded by SaintlySphene5524
Örebro University
2024
Programmeringsteknik
Pascal Rebreyend
Tags
Summary
This document contains lecture notes on file handling in programming. Topics include file types, opening, reading, writing, and closing files. Examples of code in C are also included in the lecture notes.
Full Transcript
Introduction Programmeringsteknik DT143G Lecture 8: Files Pascal Rebreyend 02-12-2024 Pascal Rebreyend Programmeringsteknik DT143G Introduction Today’s contents Files Open and closing files read, writing t...
Introduction Programmeringsteknik DT143G Lecture 8: Files Pascal Rebreyend 02-12-2024 Pascal Rebreyend Programmeringsteknik DT143G Introduction Today’s contents Files Open and closing files read, writing to a file How to structure a file Pascal Rebreyend Programmeringsteknik DT143G Introduction Why files? To save data config files Log files Interface between softwares Pascal Rebreyend Programmeringsteknik DT143G Introduction Binary vs Text files Text files: Files we can read/write (just text) Examples:.c files, most of the logs file, often configuration files Binary files: Not human-readable Examples: executable, images, videos,... Pascal Rebreyend Programmeringsteknik DT143G Introduction The 3 steps 1 Open the file 2 Use it 3 close it Open as late as possible and close as soon as possible Risk of uncoherent state if some problem occurs between open and close We will use mainly modified version of printf and scanf (the use it part) Pascal Rebreyend Programmeringsteknik DT143G Introduction Example #include int main() { FILE *f; f=fopen("file.txt","w"); fprintf(f,"Some text\n"); fclose(f); return 0; } Pascal Rebreyend Programmeringsteknik DT143G Introduction fopen: Opening the file return a pointer or handle to the file two parameters: 1 The filename (+ absolute/relative path if needed). 2 A mode: r: for reading r+: reading and writing w: truncate file to zero length or create it. For writing w+: writing and reading (truncate or create as w) a: Open for appending a+: writing/reading (similar as a) return NULL if fopen fails mode w and w+: CAUTION you can delete data!! Pascal Rebreyend Programmeringsteknik DT143G Introduction Writing and reading We can use fprintf and fscanf: easy Caution: By default: ASCII, 1 byte per character Other standard exists (like UTF-8, 1 to 4 bytes per character) text file vs. binary file Questions: Do we know how long the file is? Pascal Rebreyend Programmeringsteknik DT143G Introduction closing Don’t forget to close the file! In theory, fclose return a value (0 if success, EOF if a problem). Important since writing can be delayed (remember \n) Pascal Rebreyend Programmeringsteknik DT143G Introduction Reading by line: fgets A common way is to read the file line by line int main() { char str[MAXCH]; FILE *myfile = fopen("ex1.c", "r"); if (myfile == NULL) { printf("Could not open the file!\n"); exit(1); } while (fgets(str, MAXCH - 1, myfile) != NULL) printf("%s", str); fclose(myfile); return 0; } Pascal Rebreyend Programmeringsteknik DT143G Introduction Reading by line: comments For a text file the line is the basic and main unit of information We have also the (eof(f)) to check the end of a file (like in a while loop) We can proceed the string by using sscanf Useful if the structure of the file depends on some values Pascal Rebreyend Programmeringsteknik DT143G Introduction Complex behavior Writing or reading is simple. Appending something to a file is simple too. Modifying a file is more complex but possible. In general: modification is done by creating and writing a temporary file and then copy it onto the original one. Pascal Rebreyend Programmeringsteknik DT143G Introduction Conclusion Dealing with files is not difficult Caution: when writing, don’t overwrite or delete previous data! Read and write in the same way: Define a pseudo (very simple) language! Lot of libraries exist for already defined file formats (images, video,maps,... ) Pascal Rebreyend Programmeringsteknik DT143G Introduction Exercise: We have a file with an unknown number of lines On each line: 4 numbers (double) Goal: produce a file with on each line the average value corresponding to the average value of the 4 numbers on the input file and an extra line showing the number of lines and the average of all the numbers. Pascal Rebreyend Programmeringsteknik DT143G