Files.

Slides:



Advertisements
Similar presentations
File Management in C. What is a File? A file is a collection of related data that a computers treats as a single unit. Computers store files to secondary.
Advertisements

I/O means Input and Output. One way: use standard input and standard output. To read in data, use scanf() (or a few other functions) To write out data,
BITS Pilani, Pilani Campus TA C252 Computer Programming - II Vikas Singh File Handling.
File Management in C. A file is a collection of related data that a computers treats as a single unit. File is a collection of data stored permanently.
Files in C Rohit Khokher. Files in C Real life situations involve large volume of data and in such cases, the console oriented I/O operations pose two.
Chapter 11: Data Files & File Processing In this chapter, you will learn about Files and streams Creating a sequential access file Reading data from a.
FILES Files types and operations. Files Files are used to store data Data can be Text (ASCII only: 0  127) Binary (Full range: 0  256) Each file resides.
CSCI 171 Presentation 12 Files. Working with files File Streams – sequence of data that is connected with a specific file –Text Stream – Made up of lines.
File processing with functions H&K Chapter 3 Instructor – Gokcen Cilingir Cpt S 121 (June 27, 2011) Washington State University.
A First Book of ANSI C Fourth Edition Chapter 10 Data Files.
CP104 Introduction to Programming File I/O Lecture 33 __ 1 File Input/Output Text file and binary files File Input/output File input / output functions.
1. Introduction File Declaration and Initialization Creating and Opening File Closing File EOF Reading from and Writing into a File Extra : Random Access.
File IO and command line input CSE 2451 Rong Shi.
Chapter 8 File-Oriented Input and Output. 8.1 INTRODUCTION a file can also be designed to store data. We can easily update files, A data file as input.
CSEB114: Principle of programming Chapter 11: Data Files & File Processing.
1 File Handling. 2 Storage seen so far All variables stored in memory Problem: the contents of memory are wiped out when the computer is powered off Example:
Chapter 3 Input and Output
Chapter 7 Files By C. Shing ITEC Dept Radford University.
CNG 140 C Programming (Lecture set 10) Spring Chapter 10 Data Files.
24-2 Perform File I/O using file pointers FILE * data-type Opening and closing files Character Input and Output String Input and Output Related Chapter:
GAME203 – C Files stdio.h C standard Input/Output “getchar()”
1 CSC103: Introduction to Computer and Programming Lecture No 28.
Files A collection of related data treated as a unit. Two types Text
FILES IN C. File Operations  Creation of a new file  Opening an existing file  Reading from a file  Writing to a file  Moving to a specific location.
CSC Programming for Science Lecture 18: More Data Files.
Files. FILE * u In C, we use a FILE * data type to access files. u FILE * is defined in /usr/include/stdio.h u An example: #include int main() { FILE.
Lecture 20: C File Processing. Why Using Files? Storage of data in variables and arrays is temporary Data lost when a program terminates. Files are used.
Connecting to Files In order to read or write to a file, we need to make a connection to it. There are several functions for doing this. fopen() – makes.
C Programming Day 2. 2 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003 Version No. 1.0 Union –mechanism to create user defined data types.
UniMAP SemI-11/12EKT120: Computer Programming1 Files.
PROGRAMMING II( Files+Structures) Tallinn 2016 Vladimir Viies, Lembit Jürimägi, Margit Aarna
FILE I/O: Low-level 1. The Big Picture 2 Low-Level, cont. Some files are mixed format that are not readable by high- level functions such as xlsread()
Silberschatz and Galvin  C Programming Language Kingdom of Saudi Arabia Ministry of Higher Education Al-Majma’ah University College of Education.
By C. Shing ITEC Dept Radford University
Chapter 22 – part a Stream refer to any source of input or any destination for output. Many small programs, obtain all their input from one stream usually.
Structured Programming II
EKT120: Computer Programming
File Access (7.5) CSE 2031 Fall July 2018.
File I/O.
Plan for the Day: I/O (beyond scanf and printf)
CS111 Computer Programming
CSE1320 Files in C Dr. Sajib Datta
CSE1320 Files in C Dr. Sajib Datta
Programming in C Input / Output.
What you need for the 1st phase of project
Files I/O, Streams, I/O Redirection, Reading with fscanf
CSE1320 Files in C Dr. Sajib Datta
File I/O We are used to reading from and writing to the terminal:
Manipulating File IO in Visual C++
Beginning C Lecture 11 Lecturer: Dr. Zhao Qinpei
IPC144 Week 10 – Lesson 2 Working with Files
File I/O in C Lecture 7 Narrator: Lecture 7: File I/O in C.
FILE HANDLING IN C.
Text and Binary File Processing
File Input and Output.
File Handling.
Pointers.
Fundamental of Programming (C)
Programming in C Input / Output.
String manipulation string.h library
Functions continued.
File Handling in C.
Chapter 12: File I/O.
Arrays.
FILE handeling.
Module 12 Input and Output
CSc 352 File I/O Saumya Debray Dept. of Computer Science
EECE.2160 ECE Application Programming
File I/O We are used to reading from and writing to the terminal:
Files Chapter 8.
Presentation transcript:

Files

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

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

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

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

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

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

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

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

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

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

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

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

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 10 4 5 5 4 2 5 1 4 2 5 Mechatronics 5 3 4 4 5 0 Databases 3 1 2 1 ………. Read the original data from the file, store it in arrays and print it out 2018 Risto Heinsar

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