open, read, writeopenopen ํจ์๋ฅผ ์ฌ์ฉํ๋ฉด โ/aa/bbโ๋ผ๋ file์ read-write๊ฐ ๊ฐ๋ฅํ๋๋ก open ํ๋ค. open ํจ์๋ file descriptor๋ผ๊ณ ๋ถ๋ฅด๋ ๊ณ ์ ๋ฒํธ๋ฅผ return ํ๋ค.
0 (standard input)1 (standard output)2 (standard error)x๋ file descriptor๋ฅผ ๋ฐ๋๋ค.x๋ 3 ๋ฏธ๋ง์ผ ๊ฒ์ด๋ค.x = open("/aa/bb", O_RDWR, 00777);
or
char fname[20];
strcpy(fname, "/aa/bb");
x = open(fname, O_RDWR, 00777); // fname contains "/aa/bb"
O_RDONLY
O_WRONLY
O_RDWR
O_RDWR | O_CREAT | O_TRUNC
00777์ ๊ธฐ๋ณธ(default) permission mode๋ฅผ ์๋ฏธํ๋ค./๋ก ์์ํ์ง ์๋๋ค๋ฉด ๊ทธ๊ฒ์ ์๋๊ฒฝ๋ก(relative path)์ด๋ค.x = open("d1/f1", O_RDWR, 00777); ๋ค์๊ณผ ๊ฐ์ ์์๋ก ํ์ผ์ ์ฐพ์ ๊ฒ์ด๋ค.
d1 ํ์d1 ๋๋ ํ ๋ฆฌ์์ f1 ํ์ready = read(x, buf, 10);
read ํจ์๋ ํ์ผ x์ ํ์ฌ file position์์ ์์ํด์ ์ต๋ 10 bytes๋ฅผ ์ฝ์ด character array buf์ ์ ์ฅํ๋ค.10๋งํผ ์ฝ๋๋ค๋ฉด, File position์ด 10๋งํผ ์์ง์ธ๋ค.0์ด๋ค.y์ ์ค์ ๋ก ์ฝ์ ๋ฐ์ดํธ ์๋งํผ ๋ฐํํ๋ค.-1์ ๋ฐํํ๋ค.0์ ๋ฐํํ๋ค.writey = write(x, buf, 10);
write ํจ์๋ buf์ ํ์ฌ file position์์ ์์ํด, ์ต๋ 10 bytes๋ฅผ file x์ writeํ๋ค.-1์ด return ๋๋ค.ํ์ผ์ ์ง์ ์ ์ผ๋ก string์ write ํ ์ ์๋ค.
y = write(x, "hello", 5);
string ํ ๋ณ์๋ฅผ ํตํด์๋ ๊ฐ๋ฅํ๋ค.
char buf[10];
strcpy(buf, "hello");
y = write(x, buf, 5);
y = read(0, buf, 10); // read 10 bytes from the standard input file (keyboard in default)
// and store them in buf
y = write(1, buf, 10); // write 10 bytes from buf in the standard output file (terminal in default)
y = write(2, buf, 10); // write 10 bytes from buf in the standard error file (terminal in default)
$ man 2 open
$ man 2 read
$ man 2 write
man์ mannual์๋ ๊ฐ ์์คํ
ํธ์ถ์ ์ฌ์ฉํด์ผ ํ๋ ํ์ผ์ ๋ํ ์ค๋ช
์ด ๋์ ์๋ค.
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
int main(){
int x, y;
char buf[50];
x=open("f1", O_RDONLY, 00777); // open f1 in the current directory
y=read(x, buf, 20); // read max 20 bytes. return actual number of bytes read in y.
buf[y]=0; // make buf a string
printf("%s\n", buf); // and display
return 0;
}
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
int main(){
int x, y;
char buf[50];
x=open("f1", O_RDONLY, 00777); // open f1 in the current directory
y=read(x, buf, 20); // read max 20 bytes. return actual number of bytes read in y.
write(1, buf, y); // write y bytes in buf to the terminal.
return 0;
}
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
int main(){
int x1, x2, y;
char buf[50];
x1=open("f1", O_RDONLY, 00777); // open f1 for reading
x2=open("f2", O_RDWR | O_CREAT | O_TRUNC, 00777); // open f2 for writing
y=read(x1, buf, 20); // read max 20 bytes. return actual number of bytes read in y.
write(x2, buf, y); // write y bytes in buf to f2
return 0;
}
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
int main(){
int x1, x2, y;
char buf[50];
x1=open("f1", O_RDONLY, 00777); // open f1 for reading
x2=open("f2", O_RDWR | O_CREAT | O_TRUNC, 00777); // open f2 for writing
for(;;){
y=read(x1, buf, 20); // read max 20 bytes from f1
if (y==0) break; // if end-of-file, get out
write(x2, buf, y); // write to f2
}
return 0;
}
gcc -g -o myx x.c
gdb commands
b main : break point on main
r : start the program
n : execute the next statement (if function, execute the function and return)
s : execute the next statement (if function, enter the function and execute the
first statement of it and stop)
p x1 : show the value of variable x1
p arr1 : show the contents of array arr1
x/10xb arr1 : show 10 bytes (in hexa) starting from the address of arr1
help : show all commands
help print : show the explanation about print command
q : quit
vi ex1.c and go to line number 12 (with โ:12โ) and fix the errorprintf to find the error x=open("/aa/bb", โฆ.);
y=open("f2", โฆ.);
printf("x:%d y:%d\n", x, y);
Use gdb to find the error
Sometimes, perror is better than simple printf:
When there is an error, the system calls usually returns -1. To check what was the error do followings.
#include <errno.h>
..........
x = open(............);
if (x < 0){ // we have an error
perror("error in foo\n"); // will show error message
exit(0); // stop program
}
................
$vi f1
I have a dream
that one day this nation
will rise up and
live out the true
meaning of its creed
that all men are created equal.



cat f2.

cat f2.

ls โl f2. Use xxd to find out the actual data stored in f2.
f2์ byte size๋ 130bytes์ด๋ค.
int x, y; char buf[20];
x=open("f2", O_RDONLY, 00777); // open f2 for reading
for(;;){
y=read(x, buf, 1); // read next byte
if (y==0) break; // we read all, exit the loop
printf("%x %d %c\n", buf[0], buf[0], buf[0]); // display
}

โg option and run gdb to execute each instruction one by one. Use p or x to check the value of a variable. $ gcc -g -o hw4 hw4.c
$ gdb hw4
gdb) b main -- stop at main
gdb) r -- run
............
9 x=open("f2", O_RDONLY, 00777); -- next line to execute
gdb) list -- show code list
gdb) n -- execute current line
11 y=read(x, buf, 1); -- line 9 has been executed. next is line 11
gdb) p x -- show x
$1 = 7 -- f2 is now file number 7
gdb) n
..........
gdb) p y
$2 = 1 -- we have read 1 byte
gdb) p buf[0]
$4 = 73 'I' -- assume we have 'I' in buf[0]
gdb) x/4xb buf -- show 4 bytes at buf in hexadecimal num
0x7fffffffe470: 0x49 0x06 0x40 0x00 -- we have 0x49=73='I' in buf[0]
gdb) n
............ โ- repeat a few times
gdb) list -- show rest of code
gdb) b 15 -- break point at line 15 (after loop)
gdb) c -- continue to that break point
gdb) q -- stop gdb

x = open("f3", O_RDWR | O_CREAT | O_TRUNC, 00777); // create f3
write(x, "hello there", 11); // write 11 bytes in f3

open(), read(), and write(). Confirm that they are same with cat and ls -l. x1 = open hw4.c as O_RDONLY
x2 = open cphw4.c as O_RDWR | O_CREAT | O_TRUNC
for(;;){
y = read max 20 bytes from x1 into buf
if y is 0, break
write y bytes of buf into x2
}


xxd and ls -l. Execute cphw4 to see if it runs ok.

Enter src file name
hw4.c
Enter dest file name
newhw4.c
hw4.c is copied into newhw4.c successfully.

mycat that displays the contents of a user-input file in the terminal in characters. Give a text file and a non-text file to mycat and explain the difference.$./mycat
Enter file name
f1
The content of f1 is :
I have a dream
that one day this nation
will rise up and
live out the true
meaning of its creed
that all men are created equal.
$./mycat
Enter file name
hw4
.............

cat ๋ช
๋ น์ ํ์ผ์ด๋ฆ์ ์ธ์๋ก ๋ฐ์์ ๊ทธ ๋ด์ฉ์ ์ญ ์ด์ด์ฃผ๋ ์ญํ ์ ํ๋ค.mycat ํ๋ก๊ทธ๋จ์ cat ๋ช
๋ น๊ณผ ๊ฐ์ ๊ธฐ๋ฅ์ ์ํํ๋ ํ๋ก๊ทธ๋จ์ด๋ค.
f1)์ ์
๋ ฅ์ผ๋ก ์ฃผ์์ ๋๋ ๋ด์ฉ๋ฌผ์ด ์ ๋์จ ๊ฒ์ ํ์ธํ ์ ์๋ค.4)์ ๋ฌธ์๊ฐ ๊นจ์ ธ์ ๋์จ ๊ฒ์ ํ์ธํ ์ ์๋ค.myxxd that displays the contents of a user-input file in the terminal in hexadecimal numbers. Give a text file and a non-text file to myxxd and explain the difference. You need to use printf("%x ", buf[i]) to display a byte in a hexadecimal number. Also declare the buffer as an array of unsigned char. Compare the result from the result of xxd.$./myxxd
Enter file name
f1
The content of f1 is :
49 20 68 61 ..........
$ xxd f1
..................
$./myxxd
Enter file name
hw4
.............
$ xxd hw4
...............

xxd ๋ช
๋ น์ ๋ฐ์ด๋๋ฆฌ ๋ฐ์ดํฐ๋ฅผ 16์ง์๋ก, 16์ง์ ๋ฐ์ดํฐ๋ฅผ ๋ฐ์ด๋๋ฆฌ ๋ฐ์ดํฐ๋ก ๋ณํํด์ฃผ๋ ๋ช
๋ น์ด๋ค.myxxd ํ๋ก๊ทธ๋จ์ xxd ๋ช
๋ น๊ณผ ๊ฐ์ ๊ธฐ๋ฅ์ ์ํํ๋ ํ๋ก๊ทธ๋จ์ด๋ค.
f1)์ ์
๋ ฅ์ผ๋ก ์ฃผ์์ ๋๋ ๋ด์ฉ๋ฌผ์ด ์ ๋์จ ๊ฒ์ ํ์ธํ ์ ์๋ค.

4)์ ์
๋ ฅ์ผ๋ก ์ฃผ์์ ๋๋ ๋ด์ฉ๋ฌผ์ด ์ ๋์จ ๊ฒ์ ํ์ธํ ์ ์๋ค.
myxxd๋ก 4๋ฅผ ์คํํ์ ๋

xxd๋ก 4๋ฅผ ์คํํ์ ๋

f8 with cat and xxd respectively. Explain the results. int x=open("f8", O_CREAT|O_RDWR|O_TRUNC, 00777);
write(x, "ab", 2);
int y=10;
write(x, &y, 4);
write(x, "cd", 2);
y=235;
write(x, &y, 4);

write(x, "ab", 2);์ write(x, "cd", 2);๋ฅผ ์คํํ๋ฉด x์ ๊ฐ๊ฐ โabโ์ โcdโ๊ฐ ์ ์ฅ๋๋ค.write(x, &y, 4);๋ฅผ ์คํํ์ ๋ y์ ๊ฐ์ ์์คํค์ฝ๋๋ก์ ํด์ํ์ ๋์ ๋ฌธ์์ด๋ก ์ ์ฅํ๊ฒ ๋๋ค.f8์ cat ๋ช
๋ น์ผ๋ก ์คํํ๋ฉด ์๋์ ๊ฐ์ด ์ผ๋ถ ๊ธ์๊ฐ ๊นจ์ ธ์ ์ถ๋ ฅ๋๋ค.ab
cd๏ฟฝ
f8์ xxd ๋ช
๋ น์ผ๋ก ์คํํ๋ฉด ์๋์ ๊ฐ์ด ์ถ๋ ฅ๋๋ค.00000000: 6162 0a00 0000 6364 eb00 0000 ab....cd....
xxd๋ก ๋ณด๋ฉด 6162๋ก ์์ํ๋ ๊ฒ์ผ๋ก ๋ณด์ โabโ๊ฐ ๋งจ ์์ ๊ธฐ๋ก๋ ๊ฒ์ ํ์ธํ ์ ์๋ค. ๊ทธ๋ค์ โ0aโ(\n)์ผ๋ก ์ค ๋ฐ๊ฟ์ด ๋์๊ณ ๊ทธ ๋ค 3(=4-1)์นธ์ด 0์ผ๋ก ์ฑ์์ง ๊ฒ์ ํ์ธํ ์ ์๋ค.fstat() to find out the size of a file. Enter file name to split
f9
f9 is split into f91, f92, and f93.


char temp0[20], *temp1[10], *temp2[10];
printf("enter src file name\n");
gets(temp0);
temp1[0]=temp0;
printf("enter dest file name\n");
gets(temp0);
temp2[0]=temp0;
printf("src file:%s dest file:%s\n", temp1[0], temp2[0]);

temp1[0]=temp0;๊ณผ temp2[0]=temp0;๋ temp1[0]๊ณผ temp2[0]์ temp0์ ์์์ง์ ํฌ์ธํฐ๋ฅผ ๊ธฐ๋กํ๊ฒ ํ๋ ๋์์ด๋ค. ์ฆ, ๋ฌธ์์ด์ ๋ณต์ฌํ๋ ๊ธฐ๋ฅ์ด ์๋๋ค. ๊ทธ๋ฐ๋ฐ ๋ ๋ฒ์งธ gets(temp0);์์ temp0 ๊ฐ์ด ๋ฎ์ด ์์์ ธ temp1[0]๊ณผ temp2[0]์ด ๊ฐ์ ๊ฐ์ ๊ฐ๊ฒ ๋์๋ค. ์๋์ ๊ฐ์ด strcpy๋ก ํ์ผ ๋ด์ฉ์ ๋ณต์ฌํ๋ค๋ฉด ์๋์ ์๋๋๋ก ๋ฌธ์ ๋ฅผ ํด๊ฒฐํ ์ ์๋ค.

int x, x1, y;
x=open("f1", O_RDONLY, 00777);
x1=open("f2", O_WRONLY|O_CREAT|O_TRUNC,00777);
char buf[512];
int cnt=0;
for(;;){
y=read(x,buf,1);
if (y==0) break;
cnt++;
}
write(x1, buf, cnt);

If โf1โ has
ab
c
then, the output should be 61+62+a+63+a=13a in hexadecimal number as follows.
$ ./hexa_sum f1
sum: 13a
See hexa_sum.c (Ref myxxd 11.c and code)