Upload
know2pro
View
216
Download
0
Embed Size (px)
Citation preview
8/14/2019 5 Solaris
1/16
5 1
ANSI C Solaris Unix UNIX
ls parameter ls parameter -l -la cp source-file destination-file mv source-file destination-file
ls1.
Array Of Character ls,ls l,ls la exit
keyboard Input
data strcmp (String Compare) #include system#include strcmp 0 system ls ls
exit Not command found => ( command promt)
2. Input program screen
ls
8/14/2019 5 Solaris
2/16
ls -l
ls -la
exit ( command promt solaris )
error
3. Output program screen
8/14/2019 5 Solaris
3/16
4. Source Code
cp1. Cp copy
8/14/2019 5 Solaris
4/16
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
8/14/2019 5 Solaris
5/16
4. Source Code
8/14/2019 5 Solaris
6/16
8/14/2019 5 Solaris
7/16
mv
1. cp remove fclose(fp1) 1 1 if if = 0 move
cp2. 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
8/14/2019 5 Solaris
8/16
4. Source Code
8/14/2019 5 Solaris
9/16
8/14/2019 5 Solaris
10/16
21. Open and Read File
1. Use vi editor to key in the following program and save it to afile read.c
#include #include
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 read3. Run the executable read4. Check the program output.
What is the output of the program?5. Use an editor to create a file called data and type abcd1234in the file.6. Run the executable read again7. Check the program output.
What is the output of the program?Why does it produce such output?
21. code read.c2. gcc read.c o read3. exe
8/14/2019 5 Solaris
11/16
4.
5. data abcd1234
6. read.exe 7. data char
8 9 \0 (Null Character)
2 . Creat and white a file2.1 vi source code write.c2.2 gcc write.c o write
2.3 write.exe2.4
2.5
8/14/2019 5 Solaris
12/16
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 afile write.c
#include #include #include
#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 write3. Run the executable write4. 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. Whatis the file content?
8/14/2019 5 Solaris
13/16
3. Standard input, standard output and standard error
A UNIX system automatically opens three files for any executingprogram. These are standard input, standard output and standarderror.
1. Use vi editor to key in the following program and save it to afile io.c
/* Filename: io.c */#include #include
#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 io3. Run the executable io4. 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 to exit the program. means endof input.4. Writing error message with fprintfprintf 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.c2. Replace the statement perror(Error in read data); tofprintf(stderr, File data not found\n);3. Save the modified source file to errorio.cThe program should look like the following:
#include #include #include
#include
8/14/2019 5 Solaris
14/16
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 errorio6. 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 datas content toscreen.
main( int argc, char **argv){.....}
argc parameter command-line() argv parameter
8/14/2019 5 Solaris
15/16
argv[0] argv[1] parameter #1argv[2] parameter #2argv[3] parameter #3
progname 37 15
argc = 3argv[0] = "progname"argv[1] = "37"argv[2] = "15"
2 . Creat and white a file2.1 vi source code write.c2.2 gcc write.c o write
8/14/2019 5 Solaris
16/16
2.3 write.exe
2.4 2.5
2.6 wfile 2.7 ls l wfile 2.8 vi cat