Memory Layout, File I/O Bryce Boe 2013/06/27 CS24, Summer 2013 C.

Slides:



Advertisements
Similar presentations
1 Chapter 10 Strings and Pointers. 2 Introduction  String Constant  Example: printf(“Hello”); “Hello” : a string constant oA string constant is a series.
Advertisements

Lecture 20 Arrays and Strings
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 7 Process Environment Chien-Chung Shen CIS, UD
Memory and Files Dr. Andrew Wallace PhD BEng(hons) EurIng
A file reminder Files are used to store data and information They are manipulated through a pointer to FILE (FILE *) This pointer is returned when opening.
File I/O.
An Introduction to C Programming (assuming that you already know Java; this is not an introduction to C++)
University of Washington CSE 351 : The Hardware/Software Interface Section 5 Structs as parameters, buffer overflows, and lab 3.
University of Calgary – CPSC 441. C PROGRAM  Collection of functions  One function “main()” is called by the operating system as the starting function.
1 Homework Introduction to HW7 –Complexity similar to HW6 –Don’t wait until last minute to start on it File Access will be needed in HW8.
Chapter 6 Buffer Overflow. Buffer Overflow occurs when the program overwrites data outside the bounds of allocated memory It was one of the first exploited.
Lone Leth Thomsen Input / Output and Files. April 2006Basis-C-8/LL2 sprintf() and sscanf() The functions sprintf() and sscanf() are string versions of.
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.
Recitation 9: Error Handling, I/O, Man Andrew Faulring Section A 4 November 2002.
22. FILE INPUT/OUTPUT. File Pointers and Streams Declarations of functions that perform file I/O appear in. Each function requires a file pointer as a.
Chapter 18 I/O in C.
File Handling In C By - AJAY SHARMA. We will learn about- FFile/Stream TText vs Binary Files FFILE Structure DDisk I/O function OOperations.
1 Lecture09: File I/O 5/6/2013 Slides modified from Yin Lou, Cornell CS2022: Introduction to C.
Introduction to Programming Using C Files. 2 Contents Files Working with files Sequential files Records.
File IO and command line input CSE 2451 Rong Shi.
File I/O, Project 1: List ADT Bryce Boe 2013/07/02 CS24, Summer 2013 C.
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:
1 Lecture09: File I/O 11/19/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C.
Chapter 11: Data Files and File Processing Files and streams Creating a sequential access file Reading data from a sequential access file Using fgetc()
FILE IO in ‘C’ by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
GAME203 – C Files stdio.h C standard Input/Output “getchar()”
Recitation 9: Error Handling, I/O, Man Anubhav Gupta Section D.
1 CSC103: Introduction to Computer and Programming Lecture No 28.
Gramming An Introduction to C Programming (assuming that you already know Java; this is not an introduction to C++)
Files A collection of related data treated as a unit. Two types Text
CSCI N305: C Programming Copyright ©2006  Department of Computer & Information Science File Handling in C.
CS 241 Section Week #5 9/22/11. 2 Topics This Section File I/O Advanced C.
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.
IO revisited CSE 2451 Rong Shi. stdio.h Functions – printf – scanf(normally stops at whitespace) – fgets – sscanf Standard streams – stdin(defaults to.
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.
“Success consists of going from failure to failure without loss of enthusiasm.” Winston Churchill.
Real Numbers Device driver process within the operating system that interacts with I/O controller logical record 1 logical record 2 logical record 3.
Chapter 7 Process Environment Chien-Chung Shen CIS/UD
An Introduction to C Programming (assuming that you already know Java; this is not an introduction to C++)
 Dynamic Memory Allocation  Linked Lists  void main() {{ ◦ FILE *file; ◦ char file_name[] = “file.txt”; ◦ if ((file = fopen(file_name, "w")) ==
Variables Bryce Boe 2012/09/05 CS32, Summer 2012 B.
Error handling I/O Man pages
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.
Lecture 11 File input/output
Chapter 7 Text Input/Output Objectives
An Introduction to C Programming
File I/O.
CSC215 Lecture Input and Output.
Plan for the Day: I/O (beyond scanf and printf)
CS111 Computer Programming
File Input/Output.
Computer Science 210 Computer Organization
Files I/O, Streams, I/O Redirection, Reading with fscanf
Hank Childs, University of Oregon
File I/O We are used to reading from and writing to the terminal:
CSC215 Lecture Input and Output.
Beginning C Lecture 11 Lecturer: Dr. Zhao Qinpei
File Input and Output.
File Handling.
Accessing Files in C Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition,
Line at a time I/O with fgets() and fputs()
Module 12 Input and Output
File I/O & UNIX System Interface
CSc 352 File I/O Saumya Debray Dept. of Computer Science
Professor Jodi Neely-Ritz University of Florida
I/O CS580U - Fall 2018.
Professor Jodi Neely-Ritz University of Florida
File I/O We are used to reading from and writing to the terminal:
Presentation transcript:

Memory Layout, File I/O Bryce Boe 2013/06/27 CS24, Summer 2013 C

Outline Review HW1 (+command line arguments) Memory Layout File I/O

HW1 REVIEW

HW1 Common Problems Taking input from stdin (via scanf) Performing failure testing too late Not handling the 0 case Whitespace issues Others?

HW1 Solution

MEMORY LAYOUT

View from three Levels The address space of a process Function activation records on the stack Data within an activation record

Simplified process’s address space STACK HEAP CODE+ 0xFFFFFFFF 0x

Data Segments Code(+) – Program code – Initialization data, global and static variables Heap – Memory chunks allocated via malloc Stack – Function activation records

Creating and destroying activation records Program starts with main main calls strsafeconcat strsafeconcat calls strlength strlength returns strsafeconcat calls strlength strlength returns strsafeconcat returns main strsafeconcat strlength top of stack High Memory

Simplified function activation records Stores values passed into the function as parameters Stores the function’s local variables

How many bytes is the simplified activation record? int main(int argc, char *argv[]) { char buf[8]; for (int i = 0; i < argc; ++i) buf[i] = argv[i][0]; }

main’s simplified activation record argc (int) 4 bytes argc (int) 4 bytes argv (pointer) 4 bytes argv (pointer) 4 bytes buf (char array) 8 bytes buf (char array) 8 bytes i (int) 4 bytes i (int) 4 bytes Total: 20 bytes data stored

Think about it int main() { char msg[] = “hello”; char buf[] = “ ”; char msg2[] = “world”; } What value does buf[8] hold? What about msg[7]?

Similar but different int main() { int x = 0xDEADBEEF; char buf[] = “ ”; } What value does buf[8] hold?

Inspecting addresses in a program

FILE I/O

File I/O I/O stands for input/output Provided by the stdio library (stdio.h) Allows reading and writing to/from streams (type: FILE *) – stdin (read only) – stdout / stderr (write only) – named files

stdio.h Explore via opengroup: /basedefs/stdio.h.html Explore via `man stdio.h` (same info as opengroup, but not easily searchable or linkable)

Opening named files and closing streams FILE* fopen(char *filename, char *mode) – open a file specifying whether to open for reading, writing, appending, and create or truncate if desired int fclose(FILE *stream) – close an open FILE* – Flush and close the stream – Return 0 on success

Reading from streams int fgetc(FILE *stream) – Returns the next character in the stream or EOF (this is why it returns an int) char *fgets(char *buf, int n, FILE *stream) – Read at most n-1 bytes from stream into buf – Also stops when ‘\n’ or EOF is reached – Terminates buf with the null character ‘\0’

Other read functions fread – very useful (especially when input is unbounded), we won’t use in this class fscanf – useful, but tricky, don’t use in this class Functions with similar names less the ‘f’ – Uses the stdin stream thus doesn’t require the stream argument

Writing to streams int fputc(int c, FILE *stream) – Write a single character c to stream int fputs(char *buf, FILE *stream) – Write the null-terminated string in buf to stream int fprintf(FILE *stream, char *format, …) – Write formatted string to stream making the specified replacements

SECURITY WARNING NEVER do the following: fprintf(stdout, buf); // buf is some c-string Could allow an attacker to inspect and change your program (format string exploit) Use either fputs or fprintf(stdout, “%s”, buf)

Other write functions fwrite – generally very useful, we won’t use in this class Functions with similar names less the ‘f’ – Uses the stdout stream thus doesn’t require the stream argument

Other useful stream functions int feof(FILE *stream) – Return non-zero if the stream has reached the end of the file int fflush(FILE *stream) – Force writing any buffered data to the stream – Flushing typically occurs when a newline is encountered, thus fflush is often needed when newlines aren’t used

I/O Questions Why does fgetc/fputc return/get an integer? If a file with only a single newline at the end is 32 bytes long, how many bytes does the buffer for fgets require to read the entire file?

More I/O Questions When using fgets, how can you determine if the string is longer than the value of ‘n’ (the number of bytes to read) What will happen if the ‘n’ parameter to fgets is larger than the buffer?

Real-time cat program writing

For Tuesday Finish reading chapter 1 in the text book