CS代考 TUT11 – Programing with Files – cscodehelp代写
PowerPoint Presentation
TUT11 – Programing with Files
COMP1411: Introduction to Computer Systems
Copyright By cscodehelp代写 加微信 cscodehelp
Dr. Xianjin XIA
Department of Computing
The Polytechnic University
Spring 2022
These slides are only intended to use internally. Do not publish it anywhere without permission.
Train yourself with file related programming in C/C++
What is the output of the following program?
#include “stdio.h”
#include “stdlib.h”
#include “fcntl.h” // allows you to use system APIs for file operations
int main()
int fd1, fd2;
fd1 = open(“foo.txt”, O_RDONLY, 0);
close(fd1);
fd2 = open(“baz.txt”, O_RDONLY, 0);
printf(“fd2 = %d
”, fd2);
Solution to Problem 1
If you do not have the file baz.txt in the current directory, fd = -1
If you have the file baz.txt in the current directory, fd = 3
Try to comment out the statement “close(fd1);”, and try to print out both fd1 and fd2, see what happens?
Suppose the disk file foobar.txt consists of the six ASCII characters “foobar”. Then what is the output of the following program?
#include “stdio.h”
#include “stdlib.h”
#include “fcntl.h”
int main()
int fd1, fd2;
fd1 = open(“foobar.txt”, O_RDONLY, 0);
fd2 = open(“foobar.txt”, O_RDONLY, 0);
read(fd1, &c, 1);
read(fd2, &c, 1);
printf(“c = %c
”, c);
Solutions to Problem 2
Although the same file foobar.txt is opened twice, but opened by different file descriptors. Fd1 and fd2 will have their own open file table entry and each one has its own file position when reading foobar.txt.
Suppose the disk file foobar.txt consists of the six ASCII characters “foobar”. Then what is the output of the following program?
#include “stdio.h”
#include “stdlib.h”
#include “fcntl.h”
int main()
fd = open(“foobar.txt”, O_RDONLY, 0);
if (fork() == 0) {
read(fd, &c, 1);
wait(NULL);
read(fd, &c, 1);
printf(“c = %c
”, c);
Solution to problem 3
Note that the child inherits the parent’s descriptor table and that all processes shared the same open file table.
Thus, the descriptor fd in both the parent and child points to the same open file table entry.
When the child reads the first byte of the file, the file position increases by 1.
Thus, the parent reads the second byte, and the output is c=o
Programming practice on basic file operations
Write a program using C/C++
Read in the file score.txt, which contains multiple lines and each line contains the information of a student and his score in a test
Re-order the lines according to the score (highest first)
And write the sorted lines of information to a new file newscore.txt
Create a new sub-directory named “result” in the current working directory
Put the file newscore.txt in the “result” directory
Note that the delimiter between any two fields of a line is the space character ‘ ‘
Jack male UK 88
Bob male HK 90
Alice female US 85
Bob male HK 90
Jack male UK 88
Alice female US 85
result/newscore.txt
Your program
Solution to Problem 4
#include
#include
#include
#include
#include
#include
int read_score(char *line)
char name[20], gender[10], country[5], score[5];
sscanf(line, “%s %s %s %s”, name, gender, country, score);
return atoi(score);
Solution to Problem 4
int main()
int num = 0; // how many lines have been read
char line[100][100]; // to save each line
int sort[100][2] = {-1}; // sort[0] stores the index of the lines
FILE *fd; // file descriptor
int i, j, k;
int curr_l, curr_s;
// first, read all lines into the memory
fd = fopen(“score.txt”, “r”);
while (fgets(line[num], 100, fd) != NULL)
printf(“Totally %d lines read…
”, num);
fclose(fd);
// read scores
for (i = 0; i < num; i++){ // process each line
sort[i][0] = i;
sort[i][1] = read_score(line[i]);
//printf("line %d = %d
", sort[i][0], sort[i][1]);
Solution to Problem 4
// sort the scores with insertion
if (num == 0){
printf("No record exists...
");
return -1;
// need to sort if there are more than 1 lines
if (num > 1){
for (i = 1; i < num; i++){
curr_l = sort[i][0];
curr_s = sort[i][1];
for (j = 0; j < i; j++){
if (sort[j][1] < curr_s){
for (k = i; k > j; k–){
sort[k][0] = sort[k-1][0];
sort[k][1] = sort[k-1][1];
sort[j][0] = curr_l;
sort[j][1] = curr_s;
Solution to Problem 4
// write the outputs
mkdir(“result”, 0777);
fd = fopen(“result/newscore.txt”, “w”);
for (i = 0; i < num; i++){
fputs(line[sort[i][0]], fd);
fclose(fd);
Programming practice on file sharing between processes
Write a multi-process program using C/C++
In the parent process, create an empty file on the disk
Then, the parent process creates a child process
The child process write some string into the file that is created by the parent process
The parent process waits for the child process to finish writing the file, after it received notification from the child process, the parent process read the file content, and print out the string that has been written into the file by the child process.
Using standard I/O libraries provided by the C programming language, which is easier
To enable the parent to wait for the child, please check the wait mechanism in lecture “Concurrency”
Solution to Problem 5
#include "sys/stat.h"
#include "sys/types.h"
#include "sys/wait.h"
#include "signal.h"
#include "string.h"
#include "stdlib.h"
#include "stdio.h"
#include "fcntl.h"
int main()
pid_t pid;
int status;
// parent creates a new file
fp = fopen("msg.txt", "w");
printf("File opened, fp = %d
", fp);
// creates a child process;
pid = fork();
// child process
if (pid == 0){
fputs("Child is speaking
", fp);
fclose(fp);
// parent process
if ((waitpid(-1, &status, 0) > 0)){
char s[100];
printf(“child exits
”);
fp = fopen(“msg.txt”, “r”);
fgets(s, 100, fp);
printf(“Parents reads: %s
”, s);
fclose(fp);
File operation by C standard library:
https://en.wikipedia.org/wiki/C_file_input/output
https://www.programiz.com/c-programming/c-file-input-output
Many more other online resources
Fortunately, you only need a few functions of file operations
/docProps/thumbnail.jpeg
程序代写 CS代考 加微信: cscodehelp QQ: 2235208643 Email: kyit630461@163.com