You are on page 1of 16

ใบงานที่ 5

ตอนที่ 1
จงเขียนโปรแกรมโดยใช้ ANSI C และทำำงำนบน Solaris Unix เพือ

จำำลองกำรทำำงำนของคำำสัง่ UNIX ดังต่อไปนี ้

คำำสัง่ ls ไม่มี parameter


คำำสัง่ ls มี parameter -l และ -la
คำำสัง่ cp source-file destination-file
คำำสัง่ mv source-file destination-file

ผลการทดลองคำาสัง่ ls
1. ทฤษฎีและวิธีกำรออกแบบ
โปรแกรมใช้กำรรับค่ำจำกค่ำทีป
่ ้ อนเข้ำไป โดยกำรประกำศตัวแปร
เป็ น Array Of Character ทีม
่ ีชือ
่ ของคำำสัง่ อย่่แล้วคือ ls,ls –l,ls –la
และเพิม
่ คำำสัง่ สำำหรับออกจำกโปรแกรมด้วย exit
กำรทำำงำนคือ รับค่ำจำก keyboard มำเก็บไว้ในตัวแปร Input แล้ว
นำำค่ำมำเปรียบเทียบกับ ตัวแปร data ทีไ่ ด้ตัง้ ไว้เหมือนกันหรือไม่โดย
ใช้ strcmp (String Compare)เปรียบเทียบ โดยจะต้องประกำศ
#include<stdlib.h> ประกำศเพือ
่ ใช้คำำสัง่ system
#include<string.h> ประกำศเพือ
่ ใช้คำำสัง่ strcmp
ถ้ำค่ำเหมือนกันจะได้ 0 ตรงกับเงือ
่ นไข ให้ทำำคำำสัง่ system ซึง่ ขึน
้ อย่่
กับคำำสัง่ แต่ละคำำสัง่ เช่น พิมพ์ ls ก็จะทำำคำำสัง่ ls
ถ้ำต้องกำรออกจำกโปรแกรมให้พิมพ์ exit แต่ถ้ำพิมพ์คำำสัง่ ผิด โปรแกรม
จะแจ้งว่ำ
Not command found แล้วขึน
้ => (เป็ นสัญลักษณ์ command promt) เพือ

ให้พิมพ์คำำสัง่ ใหม่
2. Input program screen
คำำสัง่ ls
คำำสัง่ ls -l

คำำสัง่ ls -la

คำำสัง่ exit (จะกลับเข้ำส่่ command promt ของ solaris )

เมือ
่ พิมพ์คำำสัง่ ผิด โปรแกรมจะแสดงข้อควำม error

3. Output program screen


4. Source Code ทีพ
่ ิมพ์ลงในกระดำษ

ผลการทดลองคำาสัง่ cp
1. ทฤษฏีและวิธีกำรออกแบบ
ในตอนแรกจะมีกำรรับค่ำโดยใช้คำำสัง่
Cp ชือ
่ ไฟล์ ชือ
่ ไฟล์ที ่ copy หรือ ตำำแหน่งปลำยทำง
ใช้กำรรับค่ำ 2 ค่ำ จำก คีย์บอร์ด เป็ น char เก็บไว้ใน สตริง
ใช้กำร fopen คือกำรเปิ ดไฟล์ “r” เปิ ดอ่ำนอย่ำงเดียว แล้ว เขียนเป็ นไฟล์
ใหม่ โดยใช้ fopen “w+”
แล้วใช้ if เช็คไฟล์ ต้นทำง แล้วใช้คำำสัง่ while วน loop โดยใช้ gets ไฟล์
ต้นฉบับ ทีละ 49 ตัวอักษร 50 สตริง
และใช้ puts ไฟล์ปลำยทำง เมือ
่ เสร็จแล้วก็จะใช้คำำสัง่ fclose และ แสดงผล
Copy Complete
2. Input program screen
สร้ำงไฟล์ test.txt ขึน
้ มำเพือ
่ ใช้ในกำรทดสอบโปรแกรม

รันโปรแกรม ชือ
่ copy ที ่ cpmpile เสร้จแล้ว และใช้คำำสัง่ cp เพือ
่ copy
ไฟล์ test.txt ไปเป็ น test2.txt

ถ้ำพิมพ์คำำสัง่ ผิดจะแสดงข้อควำม error

พิมพ์คำำสัง่ exit เพือ


่ ออกจำกโปรแกรม
ใช้คำำสัง่ ls เพือ
่ ด่ว่ำมีไฟล์ test2.txt ทีไ่ ด้ copy ไว้หรือไม่

3. Output program screen


4. Source Code ทีพ
่ ิมพ์ลงในกระดำษ
ผลการทดลอง mv
1. ทฤษฎีและกำรออกแบบ
ทฤษฏีเหมือนกับ cp แต่เพิม
่ คำำสัง่ remove จำกที ่ fclose(fp1) คือปิ ดไฟล์ที ่
1 เป็ น กำรลบไฟล์ที ่ 1 และ ใช้ if เช็ค ว่ำลบไปหรือยัง ถ้ำ if เช็ค แล้ว = 0
จะแสดง ผลกำร move เสร็จเรียบร้อย และส่วนอืน
่ ๆ ก็คล้ำยคลึงกับ
โปรแกรม cp
2. Input program screen
สร้ำงไฟล์ test.txt และโฟลเดอร์ test_folder ไว้ที ่ /root/Desktop

รันโปรแกรม move แล้วใช้คำำสัง่ mv เพือ


่ ย้ำยไฟล์ test.txt ไปไว้ทีโ่ ฟลเดอร์
Test_folder โดยเปลีย
่ นชือ
่ เป็ น test2.txt

ทดสอบกำรพิมพ์คำำสัง่ ผิด โปรแกรมจะแสดงข้อควำม error

พิมพ์คำำสัง่ exit เพือ


่ ออกจำกโปรแกรม
แล้วใช้คำำสัง่ ls เพือ
่ ด่ว่ำไฟล์ test.txt ถ่กย้ำยไปหรือไม่

จำกนัน
้ เข้ำไปทีโ่ ฟลเดอร์ test_folder ว่ำมีไฟล์ทถ
ี ่ ่กย้ำยไว้หรือไม่

3.Output program screen


4. Source Code ทีพ
่ ิมพ์ลงในกระดำษ
ตอนที ่ 2
1. Open and Read File
1. Use vi editor to key in the following program and save it to a
file read.c

#include <fcntl.h>
#include <unistd.h>

main(){
int fd;
ssize_t nread;
char buf[1024];

/* open file "data" for reading */


fd = open("data", O_RDONLY);

if (fd == -1){
perror(“Error in read data”);
exit(1);
}

/* read in the data */


nread = read(fd, buf, 1024);
printf(“Number of characters read: %d”, nread);

/* close the file */


close(fd);
}
2. Compile the program by typing: gcc read.c –o read
3. Run the executable read
4. Check the program output.
What is the output of the program?
5. Use an editor to create a file called “data” and type “abcd1234”
in the file.
6. Run the executable read again
7. Check the program output.
What is the output of the program?
Why does it produce such output?

ผลการทดลองตอนที่ 2
1. เขียน code ทีใ่ ห้มำ ตัง้ ชือ
่ เป็ น read.c
2. คอมไพล์ ใช้คำำสัง่ gcc read.c –o read
3. รันไฟล์ exe
4. เช็คเอำท์พุต

5. สร้ำงไฟล์ data มี abcd1234 อย่่ในไฟล์


6. รัน ไฟล์ read.exe อีกรอบ
7. เช็คเอำท์พุต สรุปได้ว่ำ โปรแกรมนีไ้ ปอ่ำนไฟล์ data เห็น char
จำำนวน 8 รวมตัว แต่เห็นเป็ น 9 ตัวเพรำะ สตริงจะใช้ช่องสุดท้ำยในกำร
เก็บ \0 (Null Character) เพือ
่ แสดงถึงกำรสิน
้ สุดของสตริง

2 . Creat and white a file


2.1 ใช้ vi เขียน source code ตำมใบงำน แล้ว เซฟไฟล์ชือ
่ write.c
2.2 คอมไพล์ โดยใช้ gcc write.c –o write

2.3 รัน write.exe


2.4 เข้ำใจในโปรแกรม
2.5 เช็คเอำท์พุตของโปรแกรม
2.6 ด่ไฟล์ wfile ใน ไดเรคทอรี ่ ปั จจุบัน
2.7 ใช้คำำสัง่ ls –l wfile เช็ค กำรยินยอม ให้อ่ำนเขียน
2.8 ใช้ vi หรือใช้ cat ด่ไฟล์

2. Create and Write a file


1. Use vi editor to key in the following program and save it to a
file write.c

#include <fcntl.h>
#include <unistd.h>
#include <string.h>

#define PERMS 0644 /* permission for open with O_CREAT */

main(){
int fd;
char *filename = "wfile";
char *content = "Hello World!";

fd = open(filename, O_WRONLY|O_CREAT, PERMS);

if (fd == -1){
printf("Couldn't create %s\n", filename);
exit(1); /* error, so exit */
}

printf(“Before writing string to file\n”);


write(fd, content, strlen(content));
printf(“After writing string to file\n”);
close(fd);

exit(0);
}

2. Compile the program by typing: gcc write.c –o write


3. Run the executable write
4. Try to understand the flow of program
5. Check the program output.
What is the output of the program?

6. Look for a file wfile in the current directory.


7. Use ls –l wfile to check the file permission of the file.
What is the file permission?
Why it has such file permission?

8. Use vi to open it or use cat to list out the file content. What
is the file content?
3. Standard input, standard output and standard error

A UNIX system automatically opens three files for any executing


program. These are standard input, standard output and standard
error.

1. Use vi editor to key in the following program and save it to a


file io.c

/* Filename: io.c */
#include <stdlib.h>
#include <unistd.h>

#define SIZE 512

main(){
ssize_t nread;
char buf[SIZE];

while( (nread = read(0, buf, SIZE)) > 0){


write(1, buf, nread);
}
exit(0);
}

2. Compile the program by typing: gcc io.c –o io


3. Run the executable io
4. Enter some text.
5. Check the screen output.
What is the program doing?
In system call read(0, buf, SIZE), what does the first argument
0 means?
In system call write(1, buf, nread), what does the first
argument 1 means?

6. Press <CONTROL-D> to exit the program. <CONTROL-D> means end


of input.
4. Writing error message with fprintf
printf can be used for displaying diagnostic messages.
Unfortunately, it writes to standard output, not standard error.
However, we can now use fprintf, a generalization of printf, to do
this.

1. Use vi editor to modify read.c


2. Replace the statement perror(“Error in read data”); to
fprintf(stderr, “File data not found\n”);
3. Save the modified source file to errorio.c
The program should look like the following:

#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
main(){
int fd;
ssize_t nread;
char buf[1024];

/* open file "data" for reading */


fd = open("data", O_RDONLY);

if (fd == -1){
fprintf(stderr, “File data not found\n”);
exit(1);
}

/* read in the data */


nread = read(fd, buf, 1024);
printf(“Number of characters read: %d\n”, nread);

/* close the file */


close(fd);
}

4. Remove file data by entering: rm data.


5. Compile the program by typing: gcc errorio.c –o errorio
6. Run the executable errorio

7. Check the screen output.


What is the program output?
What is the difference between read.c and errorio.c?

Revision Exercise

Try to modify the read.c file to display file data’s content to


screen.

main( int argc, char **argv)


{
.....
}
โดยที ่ argc คือจำำนวน parameter ทีส
่ ่งผ่ำนมำจำก command-line
(นับรวมชือ
่ โปรแกรมเองด้วย)
และ argv คือ แอเรย์ ของแอเรย์ของสตริง ของ parameter ทีส
่ ่งผ่ำน
มำนัน
่ เอง
ซึง่
argv[0] คือ ชือ
่ โปรแกรมเอง
argv[1] คือ parameter #1
argv[2] คือ parameter #2
argv[3] คือ parameter #3
ฯลฯ

เช่น
progname 37 15
จะได้
argc = 3
argv[0] = "progname"
argv[1] = "37"
argv[2] = "15"

2 . Creat and white a file


2.1 ใช้ vi เขียน source code ตำมใบงำน แล้ว เซฟไฟล์ชือ
่ write.c
2.2 คอมไพล์ โดยใช้ gcc write.c –o write
2.3 รัน write.exe
2.4 เข้ำใจในโปรแกรม
2.5 เช็คเอำท์พุตของโปรแกรม

2.6 ด่ไฟล์ wfile ใน ไดเรคทอรี ่ ปั จจุบัน


2.7 ใช้คำำสัง่ ls –l wfile เช็ค กำรยินยอม ให้อ่ำนเขียน
2.8 ใช้ vi หรือใช้ cat ด่ไฟล์

You might also like