by Deitel & Associates CREARE E SCRIVERE UN FILE SEQUENZIALE

Similar documents
Module 816. File Management in C. M. Campbell 1993 Deakin University

Illustration 1: Diagram of program function and data flow

File Handling. What is a file?

System Calls and Standard I/O

As previously noted, a byte can contain a numeric value in the range Computers don't understand Latin, Cyrillic, Hindi, Arabic character sets!

/* File: blkcopy.c. size_t n

The C Programming Language course syllabus associate level

System Calls Related to File Manipulation

Sources: On the Web: Slides will be available on:

Embedded Systems Design Course Applying the mbed microcontroller

Giving credit where credit is due

Unit Write iterative and recursive C functions to find the greatest common divisor of two integers. [6]

การโปรแกรมคอมพ วเตอร (Computer Programing)

LOW LEVEL FILE PROCESSING

Informatica e Sistemi in Tempo Reale

IPC. Semaphores were chosen for synchronisation (out of several options).

BSc (Hons) Business Information Systems, BSc (Hons) Computer Science with Network Security. & BSc. (Hons.) Software Engineering

1) The postfix expression for the infix expression A+B*(C+D)/F+D*E is ABCD+*F/DE*++

A Typical Hardware System Systemprogrammering 2008

Tutorial on C Language Programming

ASCII Encoding. The char Type. Manipulating Characters. Manipulating Characters

Output: struct treenode{ int data; struct treenode *left, *right; } struct treenode *tree_ptr;

Dalhousie University CSCI 2132 Software Development Winter 2015 Lab 7, March 11

DESIGN: A Program to Create Data Entry Programs

Keywords are identifiers having predefined meanings in C programming language. The list of keywords used in standard C are : unsigned void

How To Write Portable Programs In C

Lecture 16: System-Level I/O

PROGRAMMING IN C PROGRAMMING IN C CONTENT AT A GLANCE

Corso: Mastering Microsoft Project 2010 Codice PCSNET: MSPJ-11 Cod. Vendor: Durata: 3

Capitolo 14: Flussi. Capitolo 14. Flussi Apogeo srl Horstmann-Concetti di informatica e fondamenti di Java 2

GUJARAT TECHNOLOGICAL UNIVERSITY, AHMEDABAD, GUJARAT COURSE CURRICULUM. Course Title: Advanced Computer Programming (Code: )

Senem Kumova Metin & Ilker Korkmaz 1

Computational Physics

Embedded Systems. Review of ANSI C Topics. A Review of ANSI C and Considerations for Embedded C Programming. Basic features of C

C Dynamic Data Structures. University of Texas at Austin CS310H - Computer Organization Spring 2010 Don Fussell

C / C++ and Unix Programming. Materials adapted from Dan Hood and Dianna Xu

Introduction to Programming II Winter, 2014 Assignment 2

So far we have considered only numeric processing, i.e. processing of numeric data represented

Microcontroller Systems. ELET 3232 Topic 8: Slot Machine Example

C++ Programming Language

KITES TECHNOLOGY COURSE MODULE (C, C++, DS)

Chapter 2 Introduction to Java programming

MAX = 5 Current = 0 'This will declare an array with 5 elements. Inserting a Value onto the Stack (Push)

Main Points. File layout Directory layout

Operating Systems and Programming Languages

Boolean Expressions, Conditions, Loops, and Enumerations. Precedence Rules (from highest to lowest priority)

Education: P.h.D. Candidate (Santa Clara University, California) M.S. in Computer Engineering (Santa Clara University, California)

SAFE TOP OPEN. Sistema di chiusura Locking system

Data Structure with C

SAFE. DESIGNED in italy CASSEFORTI PER HOTEL HOTEL SAFES

In order to print off elements as columns, use the \n command. The format commands will be used over and over until all the elements are printed.

C++ INTERVIEW QUESTIONS

Lecture 25 Systems Programming Process Control

SECTION C [short essay] [Not to exceed 120 words, Answer any SIX questions. Each question carries FOUR marks] 6 x 4=24 marks

Lex et Yacc, exemples introductifs

Facebook Twitter YouTube Google Plus Website

Lecture 24 Systems Programming in C

Fortify on Demand Security Review. Fortify Open Review

64-Bit NASM Notes. Invoking 64-Bit NASM

Source code security testing

Help on the Embedded Software Block

B A S I C S C I E N C E S

Lab 4: Socket Programming: netcat part

Fundamentals of Programming

CISC 181 Project 3 Designing Classes for Bank Accounts

5 Arrays and Pointers

Memory management. Announcements. Safe user input. Function pointers. Uses of function pointers. Function pointer example

Qt Signals and Slots. Olivier Goffart. October 2013

UNIVERSITY OF MUMBAI. Revised syllabus for F.Y.BSc. Computer Science to be implemented. from

- ProFTPd: - ftp://ftp.proftpd.org/distrib/source/proftpd tar.gz

1 /*ora sincrinizzata con NTP. Inserendo codice OTP si attiva un servo che ruota di 95 grad. Per chiudere il servo premere 2 "*" mentre per azzerrare

Simple C Programs. Goals for this Lecture. Help you learn about:

CP Lab 2: Writing programs for simple arithmetic problems

Programmable Graphics Hardware

Jorix kernel: real-time scheduling

Stacks. Linear data structures

Binary storage of graphs and related data

1 Abstract Data Types Information Hiding

Lecture 12 Doubly Linked Lists (with Recursion)

CS1010 Programming Methodology A beginning in problem solving in Computer Science. Aaron Tan 20 July 2015

The programming language C. sws1 1

TREE BASIC TERMINOLOGIES

PROGRAMMA CORSO DI LOGIC PRO 9

Molecular Dynamics Simulations with Applications in Soft Matter Handout 7 Memory Diagram of a Struct

Session NM059. TCP/IP Programming on VMS. Geoff Bryant Process Software

Reverse Engineering USB Device Drivers

Corso: Supporting and Troubleshooting Windows 10 Codice PCSNET: MW10-3 Cod. Vendor: Durata: 5

Phys4051: C Lecture 2 & 3. Comment Statements. C Data Types. Functions (Review) Comment Statements Variables & Operators Branching Instructions

Self Service User Authentication Service Usage Guidelines. Copyright 2013, CionSystems Inc.

Lecture 3. Arrays. Name of array. c[0] c[1] c[2] c[3] c[4] c[5] c[6] c[7] c[8] c[9] c[10] c[11] Position number of the element within array c

Passing 1D arrays to functions.

3/13/2012. Writing Simple C Programs. ESc101: Decision making using if-else and switch statements. Writing Simple C Programs

C++FA 5.1 PRACTICE MID-TERM EXAM

RUBY vers. 2. The strings are written between or between (but in this case they will become interpreted) for ex.:

Foundations of Programming Using C

Networks and Protocols Course: International University Bremen Date: Dr. Jürgen Schönwälder Deadline:

DToolsX-DWG. Version: An ActiveX DLL To Retrieve DWG/DXF information. DToolsX-DWG REFERENCE MANUAL

Freescale MQX TM I/O Drivers. Users Guide

Device Management API for Windows* and Linux* Operating Systems

10CS35: Data Structures Using C

Transcription:

CREARE E SCRIVERE UN FILE SEQUENZIALE 1 /* Create a sequential file */ 3 #include <stdio.h> 5 int main() 6 { 7 int account; /* account number */ 8 char name[ 30 ]; /* account name */ 9 double balance; /* account balance */ 10 11 FILE *cfptr; /* cfptr = clients.dat file pointer */ 12 13 /* fopen opens file. Exit program if unable to create file */ 14 if ( ( cfptr = fopen( "clients.dat", "w" ) ) == NULL ) { 15 printf( "File could not be opened\n" ); 16 } /* end if */ 17 else { 18 printf( "Enter the account, name, and balance.\n" ); 19 printf( "Enter EOF to end input.\n" ); 20 printf( "? " ); 21 scanf( "%d%s%lf", &account, name, &balance ); 22 /* write account, name and balance into file with fprintf */ 23 while (!feof( stdin ) ) { 24 fprintf( cfptr, "%d %s %.2f\n", account, name, balance ); 25 printf( "? " ); 26 scanf( "%d%s%lf", &account, name, &balance ); 27 } /* end while */ 28 fclose( cfptr ); /* fclose closes file */ 29 } /* end else */ 30 return 0; /* indicates successful termination */ 31 } /* end main */

LEGGERE E STAMPARE UN FILE SEQUENZIALE 1 /* Reading and printing a sequential file */ 3 #include <stdio.h> 5 int main() 6 { 7 int account; /* account number */ 8 char name[ 30 ]; /* account name */ 9 double balance; /* account balance */ 10 11 FILE *cfptr; /* cfptr = clients.dat file pointer */ 12 13 /* fopen opens file; exits program if file cannot be opened */ 14 if ( ( cfptr = fopen( "clients.dat", "r" ) ) == NULL ) { 15 printf( "File could not be opened\n" ); 16 } /* end if */ 17 else { /* read account, name and balance from file */ 18 printf( "%-10s%-13s%s\n", "Account", "Name", "Balance" ); 19 fscanf( cfptr, "%d%s%lf", &account, name, &balance ); 21 /* while not end of file */ 22 while (!feof( cfptr ) ) { 23 printf( "%-10d%-13s%7.2f\n", account, name, balance ); 24 fscanf( cfptr, "%d%s%lf", &account, name, &balance ); 25 } /* end while */ 27 fclose( cfptr ); /* fclose closes the file */ 28 } /* end else */ 30 return 0; /* indicates successful termination */ 32 } /* end main */

ELABORARE I DATI CONTENUTI IN UN FILE SEQUENZIALE ( parte 1 di 4) 1 /* Credit inquiry program */ 3 #include <stdio.h> 5 /* function main begins program execution */ 6 int main() 7 { 8 int request; /* request number */ 9 int account; /* account number */ 10 double balance; /* account balance */ 11 char name[ 30 ]; /* account name */ 12 FILE *cfptr; /* clients.dat file pointer */ 13 14 /* fopen opens the file; exits program if file cannot be opened */ 15 if ( ( cfptr = fopen( "clients.dat", "r" ) ) == NULL ) { 16 printf( "File could not be opened\n" ); 17 } /* end if */ 18 else { 20 /* display request options */ 21 printf( "Enter request\n" 22 " 1 - List accounts with zero balances\n" 23 " 2 - List accounts with credit balances\n" 24 " 3 - List accounts with debit balances\n" 25 " 4 - End of run\n? " );

ELABORARE I DATI CONTENUTI IN UN FILE SEQUENZIALE ( parte 2 di 4) 26 scanf( "%d", &request ); 27 28 /* process user's request */ 29 while ( request!= 4 ) { 30 31 /* read account, name and balance from file */ 32 fscanf( cfptr, "%d%s%lf", &account, name, &balance ); 33 34 switch ( request ) { 35 36 case 1: 37 printf( "\naccounts with zero balances:\n" ); 38 39 /* read file contents (until eof) */ 40 while (!feof( cfptr ) ) { 41 42 if ( balance == 0 ) { 43 printf( "%-10d%-13s%7.2f\n", 44 account, name, balance ); 45 } /* end if */ 46 47 /* read account, name and balance from file */ 48 fscanf( cfptr, "%d%s%lf", 49 &account, name, &balance ); 50 } /* end while */ 52 break;

ELABORARE I DATI CONTENUTI IN UN FILE SEQUENZIALE ( parte 3 di 4) 54 case 2: 55 printf( "\naccounts with credit balances:\n" ); 56 57 /* read file contents (until eof) */ 58 while (!feof( cfptr ) ) { 59 60 if ( balance < 0 ) { 61 printf( "%-10d%-13s%7.2f\n", 62 account, name, balance ); 63 } /* end if */ 64 65 /* read account, name and balance from file */ 66 fscanf( cfptr, "%d%s%lf", 67 &account, name, &balance ); 68 } /* end while */ 69 70 break; 71

ELABORARE I DATI CONTENUTI IN UN FILE SEQUENZIALE ( parte 4 di 4) 72 case 3: 73 printf( "\naccounts with debit balances:\n" ); 74 75 /* read file contents (until eof) */ 76 while (!feof( cfptr ) ) { 78 if ( balance > 0 ) { 79 printf( "%-10d%-13s%7.2f\n", 80 account, name, balance ); 81 } /* end if */ 82 83 /* read account, name and balance from file */ 84 fscanf( cfptr, "%d%s%lf", 85 &account, name, &balance ); 86 } /* end while */ 87 88 break; 89 90 } /* end switch */ 91 92 rewind( cfptr ); /* return cfptr to beginning of file */ 94 printf( "\n? " ); 95 scanf( "%d", &request ); 96 } /* end while */ 98 printf( "End of run.\n" ); 99 fclose( cfptr ); /* fclose closes the file */ 100 } /* end else */ 102 return 0; /* indicates successful termination */ 104 } /* end main */

CREARE E SCRIVERE SEQUENZIALMENTE UN FILE AD ACCESSO CASUALE (Parte 1 di 2) 1 /* Creating a randomly accessed file sequentially */ 3 #include <stdio.h> 5 /* clientdata structure definition */ 6 struct clientdata { 7 int acctnum; /* account number */ 8 char lastname[ 15 ]; /* account last name */ 9 char firstname[ 10 ]; /* account first name */ 10 double balance; /* account balance */ 11 }; /* end structure clientdata */ 12 13 int main() 14 { 15 int i; /* counter */ 17 /* create clientdata with no information */ 18 struct clientdata blankclient = { 0, "", "", 0.0 }; 19 20 FILE *cfptr; /* credit.dat file pointer */ 21

CREARE E SCRIVERE SEQUENZIALMENTE UN FILE AD ACCESSO CASUALE (Parte 2 di 2) 22 /* fopen opens the file; exits if file cannot be opened */ 23 if ( ( cfptr = fopen( "credit.dat", "wb" ) ) == NULL ) { 24 printf( "File could not be opened.\n" ); 25 } /* end if */ 26 else { 27 28 /* output 100 blank records to file */ 29 for ( i = 1; i <= 100; i++ ) { 30 fwrite( &blankclient, sizeof( struct clientdata ), 1, cfptr ); 31 } /* end for */ 32 33 fclose ( cfptr ); /* fclose closes the file */ 34 } /* end else */ 35 36 return 0; /* indicates successful termination */ 37 38 } /* end main */

SCRIVERE CASUALMENTE IN UN FILE AD ACCESSO CASUALE (Parte 1 di 3) 1 /* 2 Writing to a random access file */ 3 #include <stdio.h> 5 /* clientdata structure definition */ 6 struct clientdata { 7 int acctnum; /* account number */ 8 char lastname[ 15 ]; /* account last name */ 9 char firstname[ 10 ]; /* account first name */ 10 double balance; /* account balance */ 11 }; /* end structure clientdata */ 12 13 int main() 14 { 15 FILE *cfptr; /* credit.dat file pointer */ 17 /* create clientdata with no information */ 18 struct clientdata client = { 0, "", "", 0.0 }; 19 20 /* fopen opens the file; exits if file cannot be opened */ 21 if ( ( cfptr = fopen( "credit.dat", "rb+" ) ) == NULL ) { 22 printf( "File could not be opened.\n" ); 23 } /* end if */

SCRIVERE CASUALMENTE IN UN FILE AD ACCESSO CASUALE (Parte 2 di 3) 24 else { 26 /* require user to specify account number */ 27 printf( "Enter account number" 28 " ( 1 to 100, 0 to end input )\n? " ); 29 scanf( "%d", &client.acctnum ); 30 31 /* user enters information, which is copied into file */ 32 while ( client.acctnum!= 0 ) { 33 34 /* user enters last name, first name and balance */ 35 printf( "Enter lastname, firstname, balance\n? " ); 36 37 /* set record lastname, firstname and balance value */ 38 fscanf( stdin, "%s%s%lf", client.lastname, 39 client.firstname, &client.balance ); 40

SCRIVERE CASUALMENTE IN UN FILE AD ACCESSO CASUALE (Parte 3 di 3) 41 /* seek position in file of user-specified record */ 42 fseek( cfptr, ( client.acctnum - 1 ) * 43 sizeof( struct clientdata ), SEEK_SET ); 44 45 /* write user-specified information in file */ 46 fwrite( &client, sizeof( struct clientdata ), 1, cfptr ); 47 48 /* enable user to specify another account number */ 49 printf( "Enter account number\n? " ); 50 scanf( "%d", &client.acctnum ); 51 } /* end while */ 52 53 fclose( cfptr ); /* fclose closes the file */ 54 } /* end else */ 55 56 return 0; /* indicates successful termination */ 57 58 } /* end main */

ERRORI TIPICI: Aprire in scrittura ( w ) un file già esistente senza avere intenzione di eliminare il contenuto precedente. Aprire in scrittura ( w ) un file per aggiornarne il contenuto ( r+ ), provocando l indesiderata eliminazione del contenuto precedente. Aprire in lettura ( r ) un file inesistente. Dimenticare di aprire il file prima di farvi riferimento. Usare il puntatore sbagliato per fare riferimento ad un file. Non chiudere un file appena non se ne farà più uso.

MODALITÀ DI APERTURA DI UN FILE (una lista più dettagliata) Mode Description r Open a file for reading. w Create a file for writing. If the file already exists, discard the current contents. a Append; open or create a file for writing at end of file. r+ Open a file for update (reading and writing). w+ Create a file for update. If the file already exists, discard the current contents. a+ Append; open or create a file for update; writing is done at the end of the file. rb Open a file for reading in binary mode. wb Create a file for writing in binary mode. If the file already exists, discard the current contents. ab Append; open or create a file for writing at end of file in binary mode. rb+ Open a file for update (reading and writing) in binary mode. wb+ Create a file for update in binary mode. If the file already exists, discard the current contents. ab+ Append; open or create a file for update in binary mode; writing is done at the end of the file.