![Page 1: System Programming: File Managementcourses.cs.vt.edu/~cs2204/spring2004/notes/week11.pdf · C versus C++ No string data types Use character arrays instead Use strcpy(), strncpy(),](https://reader034.vdocuments.us/reader034/viewer/2022052021/603540d56f9a3b40e305bc64/html5/thumbnails/1.jpg)
System Programming: FileManagement
March 22nd, 2004
Class Meeting 9
![Page 2: System Programming: File Managementcourses.cs.vt.edu/~cs2204/spring2004/notes/week11.pdf · C versus C++ No string data types Use character arrays instead Use strcpy(), strncpy(),](https://reader034.vdocuments.us/reader034/viewer/2022052021/603540d56f9a3b40e305bc64/html5/thumbnails/2.jpg)
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
![Page 3: System Programming: File Managementcourses.cs.vt.edu/~cs2204/spring2004/notes/week11.pdf · C versus C++ No string data types Use character arrays instead Use strcpy(), strncpy(),](https://reader034.vdocuments.us/reader034/viewer/2022052021/603540d56f9a3b40e305bc64/html5/thumbnails/3.jpg)
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
![Page 4: System Programming: File Managementcourses.cs.vt.edu/~cs2204/spring2004/notes/week11.pdf · C versus C++ No string data types Use character arrays instead Use strcpy(), strncpy(),](https://reader034.vdocuments.us/reader034/viewer/2022052021/603540d56f9a3b40e305bc64/html5/thumbnails/4.jpg)
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
![Page 5: System Programming: File Managementcourses.cs.vt.edu/~cs2204/spring2004/notes/week11.pdf · C versus C++ No string data types Use character arrays instead Use strcpy(), strncpy(),](https://reader034.vdocuments.us/reader034/viewer/2022052021/603540d56f9a3b40e305bc64/html5/thumbnails/5.jpg)
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
![Page 6: System Programming: File Managementcourses.cs.vt.edu/~cs2204/spring2004/notes/week11.pdf · C versus C++ No string data types Use character arrays instead Use strcpy(), strncpy(),](https://reader034.vdocuments.us/reader034/viewer/2022052021/603540d56f9a3b40e305bc64/html5/thumbnails/6.jpg)
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
![Page 7: System Programming: File Managementcourses.cs.vt.edu/~cs2204/spring2004/notes/week11.pdf · C versus C++ No string data types Use character arrays instead Use strcpy(), strncpy(),](https://reader034.vdocuments.us/reader034/viewer/2022052021/603540d56f9a3b40e305bc64/html5/thumbnails/7.jpg)
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)
![Page 8: System Programming: File Managementcourses.cs.vt.edu/~cs2204/spring2004/notes/week11.pdf · C versus C++ No string data types Use character arrays instead Use strcpy(), strncpy(),](https://reader034.vdocuments.us/reader034/viewer/2022052021/603540d56f9a3b40e305bc64/html5/thumbnails/8.jpg)
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));
![Page 9: System Programming: File Managementcourses.cs.vt.edu/~cs2204/spring2004/notes/week11.pdf · C versus C++ No string data types Use character arrays instead Use strcpy(), strncpy(),](https://reader034.vdocuments.us/reader034/viewer/2022052021/603540d56f9a3b40e305bc64/html5/thumbnails/9.jpg)
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));
![Page 10: System Programming: File Managementcourses.cs.vt.edu/~cs2204/spring2004/notes/week11.pdf · C versus C++ No string data types Use character arrays instead Use strcpy(), strncpy(),](https://reader034.vdocuments.us/reader034/viewer/2022052021/603540d56f9a3b40e305bc64/html5/thumbnails/10.jpg)
close()
return_val = close(fd)
Closes an open file descriptor
Returns 0 on success, -1 on error
![Page 11: System Programming: File Managementcourses.cs.vt.edu/~cs2204/spring2004/notes/week11.pdf · C versus C++ No string data types Use character arrays instead Use strcpy(), strncpy(),](https://reader034.vdocuments.us/reader034/viewer/2022052021/603540d56f9a3b40e305bc64/html5/thumbnails/11.jpg)
File IO using FILEs
Most Unix programs use higher-level IOfunctions fopen()
printf()
scanf()
fclose()
These use the FILE data type instead offile descriptors
![Page 12: System Programming: File Managementcourses.cs.vt.edu/~cs2204/spring2004/notes/week11.pdf · C versus C++ No string data types Use character arrays instead Use strcpy(), strncpy(),](https://reader034.vdocuments.us/reader034/viewer/2022052021/603540d56f9a3b40e305bc64/html5/thumbnails/12.jpg)
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
![Page 13: System Programming: File Managementcourses.cs.vt.edu/~cs2204/spring2004/notes/week11.pdf · C versus C++ No string data types Use character arrays instead Use strcpy(), strncpy(),](https://reader034.vdocuments.us/reader034/viewer/2022052021/603540d56f9a3b40e305bc64/html5/thumbnails/13.jpg)
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
![Page 14: System Programming: File Managementcourses.cs.vt.edu/~cs2204/spring2004/notes/week11.pdf · C versus C++ No string data types Use character arrays instead Use strcpy(), strncpy(),](https://reader034.vdocuments.us/reader034/viewer/2022052021/603540d56f9a3b40e305bc64/html5/thumbnails/14.jpg)
printf() (cont)
int term = 15;
printf(“Twice is \n”, term, 2*term);%d %d
formatted string expressions
![Page 15: System Programming: File Managementcourses.cs.vt.edu/~cs2204/spring2004/notes/week11.pdf · C versus C++ No string data types Use character arrays instead Use strcpy(), strncpy(),](https://reader034.vdocuments.us/reader034/viewer/2022052021/603540d56f9a3b40e305bc64/html5/thumbnails/15.jpg)
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
![Page 16: System Programming: File Managementcourses.cs.vt.edu/~cs2204/spring2004/notes/week11.pdf · C versus C++ No string data types Use character arrays instead Use strcpy(), strncpy(),](https://reader034.vdocuments.us/reader034/viewer/2022052021/603540d56f9a3b40e305bc64/html5/thumbnails/16.jpg)
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
![Page 17: System Programming: File Managementcourses.cs.vt.edu/~cs2204/spring2004/notes/week11.pdf · C versus C++ No string data types Use character arrays instead Use strcpy(), strncpy(),](https://reader034.vdocuments.us/reader034/viewer/2022052021/603540d56f9a3b40e305bc64/html5/thumbnails/17.jpg)
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);
![Page 18: System Programming: File Managementcourses.cs.vt.edu/~cs2204/spring2004/notes/week11.pdf · C versus C++ No string data types Use character arrays instead Use strcpy(), strncpy(),](https://reader034.vdocuments.us/reader034/viewer/2022052021/603540d56f9a3b40e305bc64/html5/thumbnails/18.jpg)
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