COMP1521 2022T3 Q6 · 文件I/O
COMP1521 2022T3 Q6 · 10 marks · C
知识点: 文件I/O(FILE) · 次: 文本处理/临时文件 · 难度: 中等
Topic: File I/O · Source: https://cgi.cse.unsw.edu.au/~cs1521/26T1/resources/practice_exams.html
一句话 / Summary: 删除文件的第n行
Write a C program, 22t3final_q6.c ,
which takes two arguments, a filename and an integer n .
Your program should delete the n -th line from the file.
Lines are numbered starting at 1.
If the file has less than n lines, your program should do nothing.
Your program should print nothing on stdout.
It should only modify the file .
For example:
printf 'Hello\nhow\nare\nyou\n' > hello.txt
cat hello.txt
Hello
how
are
you
./22t3final_q6 hello.txt 3
cat hello.txt
Hello
how
you
printf 'I\nam\nreally great thank you\n' > answer.txt
cat answer.txt
I
am
really great thank you
./22t3final_q6 answer.txt 5
cat answer.txt
I
am
really great thank you
./22t3final_q6 answer.txt 1
cat answer.txt
am
really great thank you
seq 7 15 > numbers.txt
cat numbers.txt
7
8
9
10
11
12
13
14
15
./22t3final_q6 numbers.txt 5
cat numbers.txt
7
8
9
10
12
13
14
15
You may remember from the lectures, reading and writing to a single file
simultaneously can cause a range of tricky issues,
You may find creating a extra file (a temporary file) helpful in this exercise.
You can use make(1) to build your code:
make 22t3final_q6
Your program should not produce any output on stdout or stderr.
Assume a line is zero or more characters followed by a '\n' character.
Your program can assume it is always given as argument the name of an existing file and a positive integer.
You are permitted to create extra (temporary) files in the current directory.
You may define and call your own functions if you wish.
Your solution must be in C only.
Your solution must not be in MIPS , C++ , Python , or any other language.
Your program cannot call external programs.
Your program cannot use any of: fork() / exec() , posix_spawn() , system() , or any other similar functions.
Your program may call any functions from the C standard library available by default on CSE Linux systems.
Including but not limited to: stdio.h , stdlib.h , string.h , math.h , assert.h , etc.
No error checking is necessary or required.
🤖 Adapted: write a C program with args<file> <n>. Print the file content to stdout with line n (1-indexed) removed. If the file has fewer than n lines, print it unchanged.
Fixture files are provided in the run workspace; reference outputs verified on the sandbox.
样例
Hello how you
I am ok