Writing and Reading Raw Data

Slides:



Advertisements
Similar presentations
Overview of programming in C C is a fast, efficient, flexible programming language Paradigm: C is procedural (like Fortran, Pascal), not object oriented.
Advertisements

INSTRUCTION SET ARCHITECTURES
CMSC 2021 C++ I/O and Other Topics. CMSC 2022 Using C++ Stream I/O Default input stream is called cin Default output stream is called cout Use the extraction.
File I/O Finished off Colin Cherry standing in for Dr. Lin.
FILE OPEN MODES File open modes n ios::app n ios::ate n ios::binary n ios::in n ios::out n ios::trunc n ios::nocreate n ios::noreplace.
Copyright © 2012 Pearson Education, Inc. Chapter 12: Advanced File Operations.
An Introduction to Programming with C++ Fifth Edition Chapter 13 Sequential Access Files.
17 File Processing. OBJECTIVES In this chapter you will learn:  To create, read, write and update files.  Sequential file processing.  Random-access.
. Plab – Tirgul 11 RTTI, Binary files. RTTI – why? Problem: u Up-casting works fine.  Treating sub-class as base class Shape * s = new Circle(); u What.
Implementation of a Stored Program Computer
Run Time Type Information, Binary files. RTTI – why? Problem: Up-casting works fine. –Treating sub-class as base class Shape * s = new Circle(); What.
1 File I/O In C++, I/O occurs in streams. A stream is a sequence of bytes Each I/O device (e.g. keyboard, mouse, monitor, hard disk, printer, etc.) receives.
CENG 311 Machine Representation/Numbers
Stream Handling Streams - means flow of data to and from program variables. - We declare the variables in our C++ for holding data temporarily in the memory.
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.
Implementation of a Stored Program Computer ITCS 3181 Logic and Computer Systems 2014 B. Wilkinson Slides2.ppt Modification date: Oct 16,
IT253: Computer Organization Lecture 3: Memory and Bit Operations Tonga Institute of Higher Education.
Chapter 18 – Miscellaneous Topics. Multiple File Programs u Makes possible to accommodate many programmers working on same project u More efficient to.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
Working With Main Memory. Why Main Memory Register space limited Used for communication.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 12: Advanced File Operations.
An Introduction to Programming with C++ Sixth Edition Chapter 14 Sequential Access Files.
Computer Programming TCP1224 Chapter 13 Sequential File Access.
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.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 13 File Input and.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 12 Advanced File Operations.
Info stored in computer (memory) Numbers All in binaray – can be converted to octal, hex Characters ASCII – 1-byte/char Unicode – 2-byte/char Unicode-table.com/en.
Slides created by: Professor Ian G. Harris Hello World #include main() { printf(“Hello, world.\n”); }  #include is a compiler directive to include (concatenate)
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Member Functions for Reading and Writing Files 12.5.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
Chapter 7 C supports two fundamentally different kinds of numeric types: (a) integer types - whole numbers (1) signed (2) unsigned (b) floating types –
Streams, and File I/O Review. STREAMS Review STREAMS Streams are sequences of bytes. C++ I/0 occurs in streams Input – bytes flow from device to memory.
Chapter 10 Instruction Sets: Characteristics and Functions Felipe Navarro Luis Gomez Collin Brown.
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Assignment 1 due Friday, 7pm. RAD due next Friday. Presentations week 6. Today: –More details on functions,
1 CSC241: Object Oriented Programming Lecture No 32.
Files in Python The Basics. Why use Files? Very small amounts of data – just hardcode them into the program A few pieces of data – ask the user to input.
File Processing Files are used for data persistance-permanent retention of large amounts of data. Computer store files on secondary storage devices,such.
Learners Support Publications Working with Files.
Lecture 14 Arguments, Classes and Files. Arguments.
Binary Files. Text Files vs. Binary Files Text files: A way to store data in a secondary storage device using Text representation (e.g., ASCII characters)
Binary IO Writing and Reading Raw Data. Files Two major flavors of file: Text Binary.
Programming II I/O Streams and Data Files 1(c) Asma AlOsaimi.
CS212: Object Oriented Analysis and Design
Chapter 3 Data Representation
Chapter 14: Sequential Access Files
Programming with ANSI C ++
EET 2259 Unit 13 Strings and File I/O
17 File Processing.
Chapter 6: Data Types Lectures # 10.
CPSC 315 – Programming Studio Spring 2012
A Closer Look at Instruction Set Architectures
CISC/CMPE320 - Prof. McLeod
FILE HANDLING IN C++.
Strings, Line-by-line I/O, Functions, Call-by-Reference, Call-by-Value
17 File Processing.
Introduction to Programming
Computer Organization and ASSEMBLY LANGUAGE
File I/O with Records Lesson xx
17 File Processing.
files Dr. Bhargavi Goswami Department of Computer Science
Topics Input and Output Streams More Detailed Error Testing
Chapter 12: Advanced File Operations.
Data File Handling in C++
C++ Programming: chapter 6 – iostream
Reading from and Writing to Files
Real-World File Structures
EET 2259 Unit 13 Strings and File I/O
Exploitation Part 1.
Reading from and Writing to Files
Chapter 10 Instruction Sets: Characteristics and Functions
Presentation transcript:

Writing and Reading Raw Data Binary IO Writing and Reading Raw Data

Files Two major flavors of file: Text Binary

Text Files Text files Easier to read by hand Easier to reverse engineer Easier to hand edit More portable

Binary Files Binary files More compact Faster "260838.0573 " = 12 ascii chars = 96 bytes 1 double = 8 bytes Faster No conversions from strings to numbers Easier random access Known size for numerics

Working in Binary Mode Can specify binary more when opening a file //Open an output filestream binary mode ofstream file("info.dat", ios::binary); //Open a filestream using both output mode and binary fstream file2("info.dat", ios::out | ios::binary); //Open a filestream using allowing output, input both in binary fstream file3("info.dat", ios::out | ios::in | ios::binary);

Working in Binary Mode Binary IO : n bytes starting at address s Address expressed as char * char = 1 byte

Writing Write c-string:

Results Outputting ascii chars… Hex editor: Good text editor: Notepad:

Writing Non-Chars Write other types: Get pointer to data Cast as a char* Use sizeof( ) to calculate number of bytes

Cast Types Static_cast sanity checks types Reinterpret_cast sanity checks size C-Style cast picks whichever int x = 15; //Ask to do conversion if compiler knows rule to change type double y = static_cast<double>(x); //Tell compiler to ignore logic and treat bits as new type // Has to be same size double y2 = reinterpret_cast<double>(x); //C-Style : might do static, might do reinterpret double y3 = (double)(x);

Cast Types Static_cast sanity checks types Reinterpret_cast sanity checks size C-Style cast picks whichever int x = 15; //Ask to do conversion if compiler knows rule to change type char* dataPointer = static_cast<char*>(&x); //Tell compiler to ignore logic and treat bits as new type char* dataPointer2 = reinterpret_cast<char*>(&x); //C-Style : might do static, might do reinterpret char* dataPointer3 = (char*)(&x);

Results Outputting ascii followed by bits for 15 (F) Hex editor: Good text editor: Notepad:

Size Int = 4 bytes

Results Outputting 15 (F16) followed by 258 (10216)

Endianess Endianess : bytes order of a word in main memory

Little vs Big Endian Big is "Normal": Little weird Words in order Bytes in a word backwards

Little Endian Arrangement Little Endian Arrangement Results Outputting 15 (F16) followed by 258 (10216) Little Endian Arrangement Meaning 0F 00 Little Endian Arrangement Meaning 02 01 00

Reading Need to read string, 2 ints String unknown length

Reading Read string char by char:

Reading Same, using c-string

Reading Reading two ints:

Complex Files Graphic/Sound/etc… files have defined structure:

Other Option Structured text : XML Less efficient than text Machine parseable Wide collection of tools