Programiranje Programski jezik C. Sadržaj. Datoteke. prof.dr.sc. Ivo Ipšić 2009/2010

Similar documents
System Software Experiment 1 Lecture 7

Mode Meaning r Opens the file for reading. If the file doesn't exist, fopen() returns NULL.

File I/O. Arash Rafiey. November 7, 2017

ENG120. Misc. Topics

CS240: Programming in C

CSci 4061 Introduction to Operating Systems. Input/Output: High-level

Files and Streams Opening and Closing a File Reading/Writing Text Reading/Writing Raw Data Random Access Files. C File Processing CS 2060

Intermediate Programming, Spring 2017*

Accessing Files in C. Professor Hugh C. Lauer CS-2303, System Programming Concepts

File (1A) Young Won Lim 11/25/16

CSI 402 Lecture 2 Working with Files (Text and Binary)

fopen() fclose() fgetc() fputc() fread() fwrite()

CMPE-013/L. File I/O. File Processing. Gabriel Hugh Elkaim Winter File Processing. Files and Streams. Outline.

File IO and command line input CSE 2451

Standard File Pointers

Input/Output and the Operating Systems

Introduction to file management

a = ^ ^ ^ ^ ^ ^ ^ b = c = a^b =

PROGRAMMAZIONE I A.A. 2017/2018

Computer System and programming in C

Standard C Library Functions

Lecture 9: File Processing. Quazi Rahman

C File Processing: One-Page Summary

Content. Input Output Devices File access Function of File I/O Redirection Command-line arguments

BIL 104E Introduction to Scientific and Engineering Computing. Lecture 12

Computer programming

CSI 402 Systems Programming LECTURE 4 FILES AND FILE OPERATIONS

UNIT IV-2. The I/O library functions can be classified into two broad categories:

EM108 Software Development for Engineers

Darshan Institute of Engineering & Technology for Diploma Studies Unit 6

Input / Output Functions

C Basics And Concepts Input And Output

CS246 Spring14 Programming Paradigm Files, Pipes and Redirection

UNIX System Programming

Data File and File Handling

C PROGRAMMING. Characters and Strings File Processing Exercise

File Processing. Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan

Chapter 12. Files (reference: Deitel s chap 11) chap8

C-Refresher: Session 10 Disk IO

CS1003: Intro to CS, Summer 2008

File Handling. Reference:

Systems Programming. 08. Standard I/O Library. Alexander Holupirek

C How to Program, 6/e by Pearson Education, Inc. All Rights Reserved.

CSI 402 Lecture 2 (More on Files) 2 1 / 20

Chapter 12. Text and Binary File Processing. Instructor: Öğr. Gör. Okan Vardarlı. Copyright 2004 Pearson Addison-Wesley. All rights reserved.

Naked C Lecture 6. File Operations and System Calls

C for Engineers and Scientists: An Interpretive Approach. Chapter 14: File Processing

Ch 11. C File Processing (review)

2009 S2 COMP File Operations

CSCI 171 Chapter Outlines

What Is Operating System? Operating Systems, System Calls, and Buffered I/O. Academic Computers in 1983 and Operating System

C mini reference. 5 Binary numbers 12

C programming basics T3-1 -

File I/O. Preprocessor Macros

C Programming Language

Unit 6 Files. putchar(ch); ch = getc (fp); //Reads single character from file and advances position to next character

Computer Programming Unit v

MARKS: Q1 /20 /15 /15 /15 / 5 /30 TOTAL: /100

8. Structures, File I/O, Recursion. 18 th October IIT Kanpur

M.CS201 Programming language

Lecture6 File Processing

25.2 Opening and Closing a File

Introduction to Computer Programming Lecture 18 Binary Files

Introduction to Computer and Program Design. Lesson 6. File I/O. James C.C. Cheng Department of Computer Science National Chiao Tung University

Lecture 8. Dr M Kasim A Jalil. Faculty of Mechanical Engineering UTM (source: Deitel Associates & Pearson)

System Programming. Standard Input/Output Library (Cont d)

Standard I/O in C, Computer System and programming in C

Chapter 11 File Processing

Memory Management and

Main Program Revisited. Reading Assignment. K.N. King Chapter 3, 22. K.N. King Sections Example of Argument Processing

STRUCTURES & FILE IO

HIGH LEVEL FILE PROCESSING

A stream is infinite. File access methods. File I/O in C++ 4. File input/output David Keil CS II 2/03. The extractor and inserter form expressions

UNIT-V CONSOLE I/O. This section examines in detail the console I/O functions.

Fundamentals of Programming. Lecture 10 Hamed Rasifard

CE Lecture 11

CS240: Programming in C

Quick review of previous lecture Ch6 Structure Ch7 I/O. EECS2031 Software Tools. C - Structures, Unions, Enums & Typedef (K&R Ch.

Princeton University Computer Science 217: Introduction to Programming Systems. I/O Management

Princeton University. Computer Science 217: Introduction to Programming Systems. I/O Management

IO = Input & Output 2

UNIT- 2. Binary File

CS 261 Fall Mike Lam, Professor. Structs and I/O

Princeton University Computer Science 217: Introduction to Programming Systems I/O Management

Goals of this Lecture

I/O Management! Goals of this Lecture!

I/O Management! Goals of this Lecture!

LAB 13 FILE PROCESSING

UNIX Shell. The shell sits between you and the operating system, acting as a command interpreter

mywbut.com 12 File Input/Output

CSE2301. Introduction. Streams and Files. File Access Random Numbers Testing and Debugging. In this part, we introduce

LANGUAGE OF THE C. C: Part 6. Listing 1 1 #include <stdio.h> 2 3 int main(int argc, char *argv[]) PROGRAMMING

Data Files. Computer Basics

Programming Fundamentals

Copyright The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

Organization of a file

LAB 13 FILE PROCESSING

Preprocessing directives are lines in your program that start with `#'. The `#' is followed by an identifier that is the directive name.

Computer Language. It is a systematical code for communication between System and user. This is in two categories.

C: How to Program. Week /June/18

Transcription:

Programiranje Programski jezik C prof.dr.sc. Ivo Ipšić 2009/2010 Sadržaj Ulazno-izlazne funkcije Datoteke Formatirane datoteke Funkcije za rad s datotekama Primjeri Datoteke komunikacija između programa i okoline informacije o datotekama: mjesto zapisa datoteke na disku tip datoteke operacije nad datotekom pristup datotekama struktura tipa FILE, definirana u header datoteci stdio.h

Datoteke deklaracija pokazivača na datoteku FILE *ime_pok_na_datoteku; osnovne operacije otvaranje datoteke čitanje pisanje struktura FILE typedef struct char *buffer; /*pokazivac na adresu medjuspremnika */ char *ptr; /* pokazivac na slijedeci element */ int cnt; /* broj znakova u medjuspremniku */ int flags; /*parametri datoteke */ int fd; /* deskriptor datoteke */ FILE; Pristup podacima u datotekama pokazivač na međuspremnik pokazivač na slijedeći element broj znakova parametri datoteke deskriptor pisanje podataka pisanje podataka čitanje čitanje FILE *pok_na_dat; podataka podataka program međuspremnik datoteka

Datoteke fopen otvara datoteku FILE *fopen( const char *filename, const char *mode ); funkcija vraća pokazivač na otvorenu datoteku ili pokazivač tipa NULL mode r, w, a r čitanje w pisanje a dodavanje zapisa na kraj datoteke fclose zatvara datoteku int fclose( FILE *stream ); funkcija vraća 0 ukoliko je datoteka uspješno zatvorena int fcloseall(); funkcija fcloseall zatvara sve otvorene datoteke u programu čitanje i pisanje znakova fgetc čita znak iz datoteke int fgetc( FILE *stream ); stream pokazivač na strukturu tipa FILE funkcija vraća int pročitanog znaka ili znak EOF fputc piše znak u datoteku int fputc( int c, FILE *stream ); funkcija vraća int upisanog znaka

#include <stdio.h> #include <stdlib.h> void main() int broj_znakova=0; FILE *ulaz; int c; ulaz = fopen("text.txt","r"); if (ulaz == NULL) printf("datoteka ne postoji\n"); c = fgetc(ulaz); while (c!= EOF) broj_znakova++; c = fgetc(ulaz); fclose(ulaz); printf("broj znakova u datoteci: %d\n",broj_znakova); čitanje - fread int fread( void *buffer, int size, int count, FILE *stream ); vraća broj pročitanih jedinica, koji može biti manji od count ukoliko se je došlo do kraja datoteke count je broj jedinica koji se želi pročitati size je veličina jedne jedinice u bajtovima buffer je polje za zapis pročitanih jedinica stream je pokazivač na datoteku pisanje - fwrite intfwrite( const void *buffer, int size, int count, FILE *stream ); vraća broj zapisa count je broj jedinica koji se želi zapisati size je veličina jedne jedinice u bajtovima buffer je polje iz kojega se prenose podaci u datoteku stream je pokazivač na datoteku

čitanje - fread primjer čitanja jednog znaka while (!feof(ulaz)) fread(&c,sizeof(char),1,ulaz); broj_znakova++; funkcija feof vraća 0 ukoliko pokazivač ne pokazuje na kraj datoteke man feof 4.5 `feof'--test for end of file SYNOPSIS #include <stdio.h> int feof(file *FP); DESCRIPTION `feof' tests whether or not the end of the file identified by FP has been reached. RETURNS `feof' returns `0' if the end of file has not yet been reached; if at end of file, the result is nonzero. PORTABILITY `feof' is required by ANSI C. #include <stdio.h> #include <stdlib.h> void main() int broj_znakova=0; FILE *ulaz; int c; ulaz = fopen("text.txt","r"); if (ulaz == NULL) printf("datoteka ne postoji\n"); while (!feof(ulaz)) fread(&c,sizeof(char),1,ulaz); broj_znakova++; fclose(ulaz); printf("broj znakova u datoteci: %d\n",broj_znakova);

čitanje strukture struct osoba int sifra; char prez_ime[20+1]; unsigned int post_br; char adresa[30+1]; zaposlenik; fread(&zaposlenik,sizeof(struct osoba),1,fp); Primjer zapis podataka organiziranih u strukturu struct osoba int sifra; char prez_ime[20+1]; unsigned int post_br; char adresa[30+1]; ; struct osoba zaposlenik[100], zap_osoba; Primjer čitanje zapisa pomoću funkcije scanf i zapis strukture u datoteku for (i=0; i<br; i++) printf("upisi sifru osobe\n"); scanf("%2d", &zaposlenik[i].sifra); printf("upisi prezime osobe\n"); scanf("%s", zaposlenik[i].prez_ime); printf("osoba %2d: %s\n",zaposlenik[i].sifra, zaposlenik[i].prez_ime); fwrite(&zaposlenik[i],sizeof(struct osoba),1,izlaz);

#include <stdio.h> #include <stdlib.h> void main() struct osoba int sifra; char prez_ime[20+1]; unsigned int post_br; char adresa[30+1]; ; struct osoba zaposlenik[100], zap_osoba; int i, br; FILE *izlaz, *ulaz; printf("upisi broj osoba\n "); scanf("%d", &br); if ((izlaz= fopen("zap.txt","w")) == NULL) printf("datoteka se ne moze otvoriti\n"); for (i=0; i<br; i++) printf("upisi sifru osobe\n"); scanf("%2d", &zaposlenik[i].sifra); printf("upisi prezime osobe\n"); scanf("%s", zaposlenik[i].prez_ime); printf("osoba %2d: %s\n",zaposlenik[i].sifra, zaposlenik[i].prez_ime); fwrite(&zaposlenik[i],sizeof(struct osoba),1,izlaz); fclose(izlaz); if ((ulaz= fopen("zap.txt","r")) == NULL) printf("datoteka se ne moze otvoriti\n"); for (i=0; i<br; i++) fread(&zap_osoba,sizeof(struct osoba),1,ulaz); printf("%5d: %s\n",zap_osoba.sifra, zap_osoba.prez_ime); fclose(ulaz); /* kraj funkcije main*/ čitanje cijelog polja zaposlenik i ispis pojedinih elemenata struct osoba int sifra; char prez_ime[20+1]; unsigned int post_br; char adresa[30+1]; zaposlenik[100] ; fread(zaposlenik,sizeof(struct osoba),br,ulaz); for (i=0; i<br; i++) printf("%5d: %s\n", zaposlenik[i].sifra, zaposlenik[i].prez_ime);

Program čita znakove iz datoteke i izpisuje njihovu učestalost u datoteci. #include <stdio.h> #include <stdlib.h> main() struct znakovi int vrijednost_znaka; int ucestalost_znaka; ; struct znakovi frekvencije[256]; int i,broj_znakova=0; FILE *infile; int c; char datoteka[20]; printf("upisi ime datoteke\n"); scanf("%s",datoteka); infile = fopen(datoteka,"rb"); if (infile == NULL) printf("datoteka %s ne postoji\n",datoteka); for (i=0; i<256;i++) frekvencije[i].vrijednost_znaka=i; frekvencije[i].ucestalost_znaka=0; c = fgetc(infile); while (c!= EOF) broj_znakova++; if (c >= ' ') frekvencije[c].ucestalost_znaka++; c = fgetc(infile); fclose(infile); printf("%d znakova u datoteci\n",broj_znakova); for (i=0; i<256;i++) if (frekvencije[i].ucestalost_znaka!= 0) printf("%2c (%2d) = %d\n", i, frekvencije[i].vrijednost_znaka, frekvencije[i].ucestalost_znaka); Čitanje i pisanje znakovnih polja fgets char *fgets( char *string, intn, FILE*stream ); čita znakovno polje (string) maksimalne dužine n fputs int fputs( const char *string, FILE*stream ); #include <stdio.h> #include <stdlib.h> void main() int broj_redka=0; FILE *ulaz, *izlaz; int c; char buffer[255]; ulaz = fopen("text.txt","r"); if (ulaz == NULL) printf("datoteka ne postoji\n"); izlaz = fopen("text_novi.txt","w"); if (izlaz == NULL) printf("datoteka ne postoji\n"); while (fgets(buffer,255,ulaz)) fprintf(izlaz,"%3d %s\n",++broj_redka,buffer); fclose(ulaz); fclose(izlaz);

1 #include <stdio.h> 2 #include <stdlib.h> 3 4 void main() 5 6 int broj_redka=0; 7 FILE *ulaz, *izlaz; 8 int c; 9 char buffer[255]; 10 11 ulaz = fopen("text.txt","r"); 12 if (ulaz == NULL) 13 printf("datoteka ne postoji\n"); 14 15 16 izlaz = fopen("text_novi.txt","w"); 17 if (izlaz == NULL) 18 printf("datoteka ne postoji\n"); 19 20 21 22 23 while (fgets(buffer,255,ulaz)) 24 fprintf(izlaz,"%3d %s\n",++broj_redka,buffer); 25 26 27 fclose(ulaz); 28 fclose(izlaz); 29 30 Formatirane datoteke fprintf int fprintf( FILE *stream, const char *format [, argument ]...); fprintfvraća broj bajtova zapisanih u datoteku na koju pokazuje pokazivač stream Čitanje formatiranih datoteka fcanf int fscanf( FILE *stream, const char *format [, argument ]... ); funkcija fcanf vraća broj pročitanih elemenata primjer: float fp; char s[20]; FILE *pok_datoteke; fscanf( pok_datoteke, "%f", &fp ); fscanf( pok_datoteke, "%s", s );

Funkcije za rad s datotekama pomicanje - pozicioniranje u datoteci int fseek( FILE *stream, long offset, intorigin ); pomiće pokazivač offset bajtova u odnosu na origin SEEK_CUR Current position of file pointer SEEK_END End of file SEEK_SET Beginning of file long ftell( FILE *stream ); vraća položaj pokazivača u bajtovima