add all
This commit is contained in:
parent
e45ea8febc
commit
f1c858469d
7
.vscode/settings.json
vendored
Normal file
7
.vscode/settings.json
vendored
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
{
|
||||||
|
"files.associations": {
|
||||||
|
"types.h": "c",
|
||||||
|
"stat.h": "c",
|
||||||
|
"unistd.h": "c"
|
||||||
|
}
|
||||||
|
}
|
0
Code/copy.txt
Normal file
0
Code/copy.txt
Normal file
67
Code/io.c
Normal file
67
Code/io.c
Normal file
@ -0,0 +1,67 @@
|
|||||||
|
/****************************************************************
|
||||||
|
题目:
|
||||||
|
实现文件的拷贝,将一个有数据的文件拷贝它的数据到另一个空白的文件里面。
|
||||||
|
*****************************************************************
|
||||||
|
先在/home/xk/目录下创建一个origin.txt文件和一个copy.txt文件
|
||||||
|
然后在origin.txt文件中写入一些数据
|
||||||
|
*****************************************************************/
|
||||||
|
|
||||||
|
#include<stdio.h>
|
||||||
|
#include <sys/types.h>
|
||||||
|
#include <sys/stat.h>
|
||||||
|
#include <fcntl.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
int open_status;
|
||||||
|
int read_status;
|
||||||
|
int write_status;
|
||||||
|
int close_status;
|
||||||
|
char buf[256] = {0};
|
||||||
|
|
||||||
|
//1、打开源文件
|
||||||
|
open_status = open("/home/xk/origin.txt", O_RDWR);
|
||||||
|
if(open_status == -1)
|
||||||
|
printf("打开源文件失败\n");
|
||||||
|
else
|
||||||
|
printf("打开源文件成功\n");
|
||||||
|
|
||||||
|
//2、拷贝源文件数据
|
||||||
|
read_status = read(open_status, buf, sizeof(buf) - 1);
|
||||||
|
if(read_status > 0)
|
||||||
|
printf("已复制源文件内容为:%s", buf);
|
||||||
|
else
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
//3、关闭源文件
|
||||||
|
close_status = close(open_status);
|
||||||
|
if(close_status == 0)
|
||||||
|
printf("关闭源文件成功\n\n");
|
||||||
|
else
|
||||||
|
printf("关闭源文件失败\n\n");
|
||||||
|
|
||||||
|
//4、打开目标文件
|
||||||
|
open_status = open("/home/xk/copy.txt", O_RDWR);
|
||||||
|
if(open_status == -1)
|
||||||
|
printf("打开目标文件失败\n");
|
||||||
|
else
|
||||||
|
printf("打开目标文件成功\n");
|
||||||
|
|
||||||
|
//5、写入数据到目标文件
|
||||||
|
write_status = write(open_status, buf, strlen(buf));
|
||||||
|
if(write_status > 0)
|
||||||
|
printf("复制成功,有%d个字符\n", write_status);
|
||||||
|
else
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
//6、关闭目标文件
|
||||||
|
close_status = close(open_status);
|
||||||
|
if(close_status == 0)
|
||||||
|
printf("关闭目标文件成功\n");
|
||||||
|
else
|
||||||
|
printf("关闭目标文件失败\n");
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
56
Code/io2.c
Normal file
56
Code/io2.c
Normal file
@ -0,0 +1,56 @@
|
|||||||
|
/************************************************
|
||||||
|
使用标准IO完成文件拷贝
|
||||||
|
************************************************/
|
||||||
|
#include<stdio.h>
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
FILE *fp1, *fp2;
|
||||||
|
char buf[256] = {0};
|
||||||
|
int read_status, write_status, close_status;
|
||||||
|
|
||||||
|
//1、打开源文件
|
||||||
|
fp1 = fopen("./origin.txt", "r");
|
||||||
|
if(fp1 == NULL)
|
||||||
|
printf("打开源文件失败\n");
|
||||||
|
else
|
||||||
|
printf("打开源文件成功\n");
|
||||||
|
|
||||||
|
//2、拷贝源文件数据
|
||||||
|
read_status = fread(buf, sizeof(char), sizeof(buf) - 1, fp1);
|
||||||
|
if(read_status > 0)
|
||||||
|
printf("已复制源文件内容为:%s", buf);
|
||||||
|
else
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
//3、关闭源文件
|
||||||
|
close_status = fclose(fp1);
|
||||||
|
if(close_status == 0)
|
||||||
|
printf("关闭源文件成功\n\n");
|
||||||
|
else
|
||||||
|
printf("关闭源文件失败\n\n");
|
||||||
|
|
||||||
|
//4、打开目标文件
|
||||||
|
fp2 = fopen("./copy.txt", "w");
|
||||||
|
if(fp2 == NULL)
|
||||||
|
printf("打开目标文件失败\n");
|
||||||
|
else
|
||||||
|
printf("打开目标文件成功\n");
|
||||||
|
|
||||||
|
//5、写入数据到目标文件
|
||||||
|
write_status = fwrite(buf, sizeof(char), read_status, fp2);
|
||||||
|
if(write_status > 0)
|
||||||
|
printf("复制成功,有%d个字符\n", write_status);
|
||||||
|
else
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
//6、关闭目标文件
|
||||||
|
close_status = fclose(fp2);
|
||||||
|
if(close_status == 0)
|
||||||
|
printf("关闭目标文件成功\n");
|
||||||
|
else
|
||||||
|
printf("关闭目标文件失败\n");
|
||||||
|
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
39
Code/jincheng.c
Normal file
39
Code/jincheng.c
Normal file
@ -0,0 +1,39 @@
|
|||||||
|
/***************************************************
|
||||||
|
子进程每隔1秒打印一次apple,父进程每隔2秒打印一次hello。
|
||||||
|
****************************************************/
|
||||||
|
#include<stdio.h>
|
||||||
|
#include <sys/types.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
// 创建子进程
|
||||||
|
pid_t pid = fork();
|
||||||
|
|
||||||
|
// 错误处理
|
||||||
|
if (pid < 0)
|
||||||
|
{
|
||||||
|
printf("创建子进程失败\n");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 子进程
|
||||||
|
else if (pid == 0)
|
||||||
|
{
|
||||||
|
while (1) {
|
||||||
|
printf("apple\n");
|
||||||
|
sleep(1); // 每隔1秒打印一次apple
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 父进程
|
||||||
|
else
|
||||||
|
{
|
||||||
|
while (1) {
|
||||||
|
printf("hello\n");
|
||||||
|
sleep(2); // 每隔2秒打印一次hello
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0; // 程序结束
|
||||||
|
}
|
52
Code/jincheng2.c
Normal file
52
Code/jincheng2.c
Normal file
@ -0,0 +1,52 @@
|
|||||||
|
/**********************************************
|
||||||
|
创建一个子进程,打印hello后父进程打印world,之后父进程再创建两条进程,一个进程打印end,另一个进程打印...
|
||||||
|
**********************************************/
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <sys/types.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <sys/wait.h>
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
// 创建子进程
|
||||||
|
pid_t pid = fork();
|
||||||
|
|
||||||
|
// 错误处理
|
||||||
|
if (pid < 0)
|
||||||
|
{
|
||||||
|
printf("创建子进程失败\n");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 子进程
|
||||||
|
else if (pid == 0)
|
||||||
|
{
|
||||||
|
printf("hello\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
// 父进程
|
||||||
|
else
|
||||||
|
{
|
||||||
|
wait(NULL); // 等待子进程结束
|
||||||
|
|
||||||
|
printf("world\n");
|
||||||
|
|
||||||
|
// 创建两个新的子进程
|
||||||
|
pid_t pid1 = fork();
|
||||||
|
if (pid1 == 0)
|
||||||
|
{
|
||||||
|
printf("end\n");
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
pid_t pid2 = fork();
|
||||||
|
if (pid2 == 0)
|
||||||
|
{
|
||||||
|
printf("...\n");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0; // 程序结束
|
||||||
|
}
|
1
Code/origin.txt
Normal file
1
Code/origin.txt
Normal file
@ -0,0 +1 @@
|
|||||||
|
this is a test
|
BIN
Code/output/io
Normal file
BIN
Code/output/io
Normal file
Binary file not shown.
BIN
Code/output/io2
Normal file
BIN
Code/output/io2
Normal file
Binary file not shown.
BIN
Code/output/jincheng
Normal file
BIN
Code/output/jincheng
Normal file
Binary file not shown.
BIN
Code/output/jincheng2
Normal file
BIN
Code/output/jincheng2
Normal file
Binary file not shown.
BIN
Code/output/xiancheng
Normal file
BIN
Code/output/xiancheng
Normal file
Binary file not shown.
14
Code/xiancheng.c
Normal file
14
Code/xiancheng.c
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
#include<stdio.h>
|
||||||
|
#include<pthread.h>
|
||||||
|
|
||||||
|
void* start_routine(void* arg)
|
||||||
|
{
|
||||||
|
printf("Thread is running...\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user