system programming: file managementcourses.cs.vt.edu/~cs2204/spring2004/notes/week11.pdf · c...

Post on 07-Oct-2020

3 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

System Programming: FileManagement

March 22nd, 2004

Class Meeting 9

Layers in a Unix-based System

Hardware(CPU, memory, disks, terminals, etc.)

Unix Operating System(process/memory management, file system, I/O)

Standard Library(open, close, read, write, etc.)

Standard Utility Programs(shells, editors, compilers, etc.)

UsersUser Interface

Library Interface

System calls

kernel

usermode

Unix System Programming

Programming that uses special featuresof the Unix system (kernel)

Programs make system calls via libraries

Types of system calls File IO

Process Management

Inter-Process Communication (IPC)

Signal Handling

C versus C++

No string data types Use character arrays instead Use strcpy(), strncpy(), strcmp(),strncmp() to “assign” and compare characterarrays

No embedded declarations Must declare all variables at the beginning of a

code block

Very different File and Standard IO functions printf() versus cout scanf() and fgets() versus cin

Basic File IO

Remember everything in Unix is a file

Processes keep a list of open files

Files can be opened for reading, writing Must include <stdio.h> to use IO

system calls

Basic File IO (cont)

Each file is referenced by a filedescriptor (integer)

Three files are opened automatically FD 0: standard input

FD 1: standard output

FD 2: standard error

When new files are opened, it isassigned the lowest available FD

open()

fd = open(path, flags, mode) path: char*, absolute or relative path flags:

O_RDONLY – open for reading O_WRONLY – open for writing O_RDWR – open for reading and writing O_CREAT – create the file if it doesn’t exist O_TRUNC – truncate the file it exists (overwrite) O_APPEND – only write at the end of the file

mode: specify permissions if using O_CREATE Returns newly assigned file descriptor fd = open(“myFile”, O_CREAT, 00644)

read()

bytes = read(fd, buffer, count) Read from file associated with fd; place count bytes

into buffer fd: file descriptor to read from buffer: pointer to an array count: number of bytes to read Returns number of bytes read or -1 if an error

occurred

int fd = open(“someFile”, O_RDONLY, 0);char buffer[4];int bytes =

read(fd, &buffer, 4*sizeof(char));

write()

bytes = write(fd, buffer, count) Write contents of buffer to file associated with fd fd: file descriptor buffer: pointer to an array count: number of bytes to write Returns the number of bytes written or -1 if an error

occurred

int fd = open(“someFile”, O_WRONLY, 0);char buffer[4];int bytes =

write(fd, buffer, 4*sizeof(char));

close()

return_val = close(fd)

Closes an open file descriptor

Returns 0 on success, -1 on error

File IO using FILEs

Most Unix programs use higher-level IOfunctions fopen()

printf()

scanf()

fclose()

These use the FILE data type instead offile descriptors

fopen()

FILE *file_stream = fopen(path, mode)

path: char*, absolute or relative path mode:

r – open file for reading r+ – open file for reading and writing w – overwrite file or create file for writing w+ – open for reading and writing; overwrites file a – open file for appending (writing at end of file) a+ – open file for appending and reading

fclose(file_stream) Closes open file stream

printf()

printf(formatted_string,...)

formatted_string: string that describesthe output information variable types are escaped with % (see next

slide)

string is followed by as manyexpressions as are referenced in theformatted string

printf() (cont)

int term = 15;

printf(“Twice is \n”, term, 2*term);%d %d

formatted string expressions

Escaping Variable Types

%d, %i – decimal integer %u – unsigned decimal integer %o – unsigned octal integer %x, %X – unsigned hexadecimal integer %c - character %s – string or character array %f – float %e, %E – double (scientific notation) %g, %G – double or float %% - outputs a % character

printf() Examples

printf("The sum of %d, %d, and %dis %d\n", 65, 87, 33, 65+87+33); Output: The sum of 65, 87, and 33 is 185

printf("Error %s occurred at line%d \n", emsg, lno); emsg and lno are variables Output: Error invalid variable occurred at line 27

printf("Hexadecimal form of %d is%x \n", 59, 59); Output: Hexadecimal form of 59 is 3B

scanf()

scanf(formatted_string, ...)

Similar syntax as printf, only theformatted string represents the data thatyou are reading in

Must pass variables by reference

Example scanf(“%d %c %s”, &int_var,&char_var, &string_var);

printf() and scanf() Families

fprintf(file_stream, formatted_string, ...)

Prints to a file stream instead of stdout sprintf(char_array, formatted_string, ...)

Prints to a character array instead of stdout

fscanf(file_stream, formatted_string, ...)

Reads from a file stream instead of stdin sscanf(char_array, formatted_string, ...)

Reads from a string instead of stdin

top related