Binary files Binary files can not be created by an editor (as with text files) A binary file is created by writing on it from a C-program If a human should.

Slides:



Advertisements
Similar presentations
©2002, Ed Skoudis Format String Stack View main() { char user_input[100]; char buffer[100]; int x; … /*get user_input*/ … snprintf(buffer, sizeof buffer,
Advertisements

Overview of programming in C C is a fast, efficient, flexible programming language Paradigm: C is procedural (like Fortran, Pascal), not object oriented.
Chapter 6 Structures By C. Shing ITEC Dept Radford University.
C Structures Basics of structures Typedef. Data Hierarchy Byte –8 bits (ASCII character ‘A’ = ) Field –Group of characters (character string “Fred”)
1 Chapter 10 Strings and Pointers. 2 Introduction  String Constant  Example: printf(“Hello”); “Hello” : a string constant oA string constant is a series.
Senem Kumova Metin Spring2009 BINARY TREES && TREE TRAVERSALS Chapter 10 in A Book on C.
By Senem Kumova Metin 1 POINTERS + ARRAYS + STRINGS REVIEW.
Introduction to C Programming
 2000 Prentice Hall, Inc. All rights reserved. Chapter 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line.
Pointers Discussion 5 Section Housekeeping HW 1 Issues Array Issues Exam 1 Questions? Submitting on Time!
1 TDBA66, vt-04, Lecture Ch9 Text files  Text file contains ASCII-characters  New line and end-of-file are special characters in a text file Ex. 1 
Lecture No: 16. The scanf() function In C programming language, the scanf() function is used to read information from standard input device (keyboard).
Ping Zhang 10/08/2010.  You can get data from the user (input) and display information to the user (output).  However, you must include the library.
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to C++ Programming Outline Introduction to C++ Programming A Simple Program: Printing a.
CS140: Intro to CS An Overview of Programming in C by Erin Chambers.
Chapter 5: Data Input and Output Department of Computer Science Foundation Year Program Umm Alqura University, Makkah Computer Programming Skills
C How to Program, 6/e © by Pearson Education, Inc. All Rights Reserved.
'C' Programming With Structure Records Purpose of structures Coding a structure template Defining a new data type Functions which communicate using structures.
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.
C Tokens Identifiers Keywords Constants Operators Special symbols.
Dynamic Memory Allocation Conventional array and other data declarations An incorrect attempt to size memory dynamically Requirement for dynamic allocation.
1 CHAPTER 7. Objectives: You’ll learn about;  Introduction  Fundamentals of binary data files  Processing binary files  Files with mixed type data.
 2007 Pearson Education, Inc. All rights reserved C File Processing.
Nested LOOPS.
 2007 Pearson Education, Inc. All rights reserved C File Processing.
Fundamentals of C and C++ Programming. EEL 3801 – Lotzi Bölöni Sub-Topics  Basic Program Structure  Variables - Types and Declarations  Basic Program.
Chapter 8 : Binary Data Files1 Binary Data Files CHAPTER 8.
A. Abhari CPS1251 Input/Output Files, Review Text file: a named collection of characters newline, input stream, output stream This is first line. This.
File I/O. fstream files File: similar to vector of elements Used for input and output Elements of file can be –character (text)struct –object (non-text.
EXERCISE Arrays, structs and file processing. Question You own a pet store. You want to keep an inventory of all the pets that you have. Pets available.
CECS 130 EXAM 1.  int main() { printf (“%c %c \n", 'a', 65); printf ("%d %ld\n", 1977, L); printf (" %10d \n", 1977); printf ("%010d \n", 1977);
Functions: Part 2 of /11/10: Lecture 16 CMSC 104, Section 0101 John Y. Park 1.
Computer Programming for Engineering Applications ECE 175 Intro to Programming.
Chapter 8 Characters and Strings. Objectives In this chapter, you will learn: –To be able to use the functions of the character handling library ( ctype).
Chapter 12 Files (reference: Deitel ’ s chap 11) chap8.
1 CSC103: Introduction to Computer and Programming Lecture No 28.
1 CSC103: Introduction to Computer and Programming Lecture No 27.
 2007 Pearson Education, Inc. All rights reserved. 1 C File Processing.
Chapter 12 Text and Binary File Processing Dr. Jiung-yao Huang Dept. Comm. Eng. Nat. Chung Cheng Univ. TA: 鄭筱親 陳昱豪.
NOTE: C programs consist of functions one of which must be main. C programs consist of functions one of which must be main. Every C program begins executing.
 2007 Pearson Education, Inc. All rights reserved C File Processing.
UniMAP SemI-11/12EKT120: Computer Programming1 Files.
Problem Solving and Program Design in C Chap. 11 Text and Binary File Processing Chow-Sing Lin.
REEM ALAMER REVISION FOR C LANGUAGE COURSE. OUTPUTS int main (void) { int C1, C2; int *p1, *p2; C1 = 8; p1 = &C1; C2 = *p1 / 2 + 5; p2 = &C2; printf ("C1.
Chapter 1.2 Introduction to C++ Programming
11 C File Processing.
Chapter 1.2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
TMF1414 Introduction to Programming
2016.
Introduction to Computer Programming Lecture 18 Binary Files
EECE.2160 ECE Application Programming
Introduction to C Programming
Lecture 13 Input/Output Files.
פרטים נוספים בסילבוס של הקורס
CSCE 206 Lab Structured Programming in C
Introduction to C Programming
EECE.2160 ECE Application Programming
Text and Binary File Processing
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
Programs written in C and C++ can run on many different computers
EECE.2160 ECE Application Programming
Arrays.
Character Arrays char string1[] = “first”;
CSCE 206 Lab Structured Programming in C
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
Presentation transcript:

Binary files Binary files can not be created by an editor (as with text files) A binary file is created by writing on it from a C-program If a human should not read the file it saves time and memory to use binary files The internal representation in bits ia written to a binary file Contents of databases are typically binary

Example Figure 10.3 (extended) Creating a Binary File of Integers and read them into a vector of integers FILE *binaryp, *inbinp; int i, list[255]; binaryp = fopen("nums.bin", "wb"); for (i = 2; i <= 500; i += 2) fwrite(&i, sizeof (int), 1, binaryp); fclose(binaryp); /* read 250 integers into a vector */ inbinp = fopen(”nums.bin”,”rb”); fread(list, sizeof(int), 250, inbinp); fclose(inbinp); …………

Figure 10.4 Outline and Function main for Metals Database Inquiry Program /* * Displays all metals in the database that satisfy the search * parameters specified by the program user. */ #include #include #define MAX_DENSITY 20.0 /*maximum density (g/cm^3)*/ #define MAX_MELT_PT 4000 /*maximum melting point(deg.C)*/ #define MAX_TENS_MOD 350 /*maximum tensile modulus(GPa)*/ #define MAX_DAYS 90 /* maximum days to delivery */ #define STR_SIZ 80 /* number of characters in string*/ typedef struct { /* metal structure type */ char name[STR_SIZ]; double density; /* g/cm^3 */ int melt_pt, /* melting point in degrees C */ tens_mod, /* tensile modulus in GPa */ days_to_deliv; /* days from order to delivery */ } metal_t; typedef struct { /* search parameter bounds type */ char low_name[STR_SIZ], high_name[STR_SIZ]; double low_density, high_density; int low_melt_pt, high_melt_pt, low_tens_mod, high_tens_mod, low_days, high_days; } search_params_t;

Fig cont. /* Insert prototypes of other functions needed.*/ /* * Prompts the user to enter the search parameters. */ search_params_t get_params(void); /* * Displays records of all metals in the inventory that satisfy search parameters. */ void display_match(FILE *databasep, /* input - file pointer to binary database file*/ search_params_t params);/* input - search parameter bounds*/ int main(void){ char inv_filename[STR_SIZ]; /*name of inventory file */ FILE *inventoryp; /* inventory file pointer*/ search_params_t params; /* search parameter bounds */ /* Get name of inventory file and open it*/ printf("Enter name of inventory file> "); scanf("%s", inv_filename); fflush(stdin); inventoryp = fopen(inv_filename, "rb"); /* Get the search parameters */ params = get_params(); /*Display all metals that satisfy the search parameters */ display_match(inventoryp, params); return(0); }