Presentation is loading. Please wait.

Presentation is loading. Please wait.

Files.

Similar presentations


Presentation on theme: "Files."— Presentation transcript:

1 Files

2 Information Start with Your big homework!
Read through the task description and Your task variant Create the input files with some basic data Create your initial program, that just reads through the file and prints it out Next week, we’ll start to work with structures – then you will be able to store them as required 2018 Risto Heinsar

3 Opening a file Working with files requires us to use file pointers To declare a file pointer: FILE *fp; To open a file: filePointer = fopen(fileName, mode); Both filename and mode are type char* (char pointers aka strings) fopen() returns a NULL-pointer when opening the file fails You should always verify that the file was opened successfully! 2018 Risto Heinsar

4 Arguments for fopen Modes: File name: "r " – read
"w" – write (when the file already exists, it will be overwritten. If there is no file, it will be created) "a" – append (write to the end of an existing file) File name: Full name of the file (name + extension) as a string (char*) The file will be searched for in the same directory as the executable (relative path) You can also specify the exact file location from the root (full path) 2018 Risto Heinsar

5 In code You should always check if the file was opened successfully! When opening a file fails, reading from it will crash the program. FILE *fi; // file pointer declaration char inputFile[] = "numbers.txt"; fi = fopen(inputFile,"r"); //opening it if (fi == NULL) // check for errors { exit(1); // abort. Exit comes from stdlib.h } 2018 Risto Heinsar

6 Closing a file Resources should be freed when not needed
All of the files should be closed before exiting Never use fclose() on an invalid or NULL pointer To close a file: fclose(filePointer); 2018 Risto Heinsar

7 Functions for files (see stdio.h for more)
fscanf(filePointer, format, …); read formatted text from a file fgets(destination, length, filePointer); read line by line or until length is exceeded fprintf(filePointer, format, …); write formatted text to a file fputs(contents, filePointer); write line by line to a file fflush(filePointer); Writes the output buffer contents to the file EOF - a constant to indicate the end of file. Typically -1 in C 2018 Risto Heinsar

8 Buffers Most file read/write functions are buffered
Data is not written each time fprintf(), fputs(), fwrite(), … is called nor is it read each time fscanf(), fgets(), fread(), … is called Data is typically read as large chunks and passed to the appropriate functions as requested. Data is typically written to the file once the buffer becomes full or the file is closed. The contents of the buffer may be lost if the program crashes! This is especially important for log files! You can force output buffer write using fflush() 2018 Risto Heinsar

9 Some additional information
MS Office, OpenOffice, …. don’t produce basic ASCII text files we are working with Windows and Linux typically use a different line ending You can open files in binary form instead of ASCII Each time you call any read function, the next portion of the file will be passed to you (from buffer) Think of reading a book while tracking your progress with your finger This position can be manipulated with 2018 Risto Heinsar

10 Sample 1 (reading using EOF)
#include <stdio.h> int main(void) { FILE *fi; int temp; char inputFile[] = „numbers.txt“; fi = fopen(inputFile,"r"); while (fscanf(fo, "%d", &temp) != EOF) printf("Got: %d\n", temp); fclose(fp); return 0; } 2018 Risto Heinsar

11 Sample 2 (check success count)
#include <stdio.h> #define INPUT_FILE "numbers.txt" int main(void) { FILE *fi = fopen(INPUT_FILE,"r"); int used, quota; while (fscanf(fi, „%d %d“, &used, &quota) == 2) printf(“User is using %d from %d\n“, used, quota); fclose(fi); return 0; } 2018 Risto Heinsar

12 Sample 3 (writing to a file)
#include <stdio.h> #define LIMIT 5 int main(void) { FILE *fo = fopen(“numbers2.txt“,"w"); int i; for (i = 0; i < LIMIT; i++) fprintf(fo, "Output: %d\n", i); fclose(fo); return 0; } 2018 Risto Heinsar

13 Task 1 Read integers from input file (number count is not known)
The numbers will be divided between 2 separate files based on the following principle: Even digits go to the first file Odd digits go to the second file Don’t forget some basic error proofing Don’t overdo it, less is more in this case 2018 Risto Heinsar

14 Task 2 (part 1/3) The input file contains statistics on different subjects (at least 5) The data format on each line is as follows: <subject> <grade count> <grades> Sample input data Programming Mechatronics Databases ………. Read the original data from the file, store it in arrays and print it out 2018 Risto Heinsar

15 Task 2 (parts 2 and 3) Part 2: You must be able to repeatedly perform following searches from the results Search for a subject by name Search for subjects where the average grade is more / less than a given value Search for subjects that have more / less than given amount of grades Search keywords and values are specified by the user from keyboard input Part 3: Add logging Each query must be logged Each start of the program must be logged The log file must contain the logs for each previous run 2018 Risto Heinsar


Download ppt "Files."

Similar presentations


Ads by Google