1:使用 fputc 和 fgetc 实现文件的拷贝功能
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <pthread.h>
#include <semaphore.h>
#include <wait.h>
#include <signal.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#include <semaphore.h>
#include <sys/msg.h>
#include <sys/shm.h>
#include <sys/un.h>
typedef struct sockaddr_in addr_in_t;
typedef struct sockaddr addr_t;
typedef struct sockaddr_un addr_un_t;
int main(int argc, const char *argv[])
{
FILE* fp1 = fopen(argv[1],"r");
if(NULL == fp1)
{
perror("fp1");
return -1;
}
FILE* fp2 = fopen(argv[2],"w");
if(NULL == fp2)
{
perror("fp2");
return -1;
}
char ch;
while ((ch = fgetc(fp1)) != EOF) {
fputc(ch, fp2); // 将读取的字符写入目标文件
}
printf("拷贝成功!\n");
fclose(fp1);
fclose(fp2);
return 0;
return 0;
2:将结构体数组的加载保存的代码,把结构体数组改成链表再来一次
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <pthread.h>
#include <semaphore.h>
#include <wait.h>
#include <signal.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#include <semaphore.h>
#include <sys/msg.h>
#include <sys/shm.h>
#include <sys/un.h>
typedef struct sockaddr_in addr_in_t;
typedef struct sockaddr addr_t;
typedef struct sockaddr_un addr_un_t;
typedef int DataType;
typedef struct Node
{
union
{
int len;
DataType data;
};
struct Node *next;
}linkList,*linkListPtr;
//创建链表
linkListPtr link_create()
{
linkListPtr H = (linkListPtr)malloc(sizeof(linkList));
if(NULL == H)
{
printf("创建链表失败!\n");
return NULL;
}
H->len = 0;
H->next = NULL;
printf("创建链表成功!\n");
return H;
}
//申请节点 封装数据
linkListPtr create_node(DataType e)
{
linkListPtr p = (linkListPtr)malloc(sizeof(linkList));
if(NULL == p)
{
printf("申请节点失败!\n");
return NULL;
}
p->data = e;
p->next = NULL;
return p;
}
//头插
int head_add(linkListPtr H,DataType e)
{
if(NULL == H)
{
printf("头插失败!\n");
return 0;
}
//申请节点 封装数据
linkListPtr p = create_node(e);
p->next = H->next;
H->next = p;
H->len++;
return 1;
}
int main(int argc, const char *argv[])
{
linkListPtr H = link_create();
head_add(H, 10);
head_add(H, 20);
head_add(H, 30);
head_add(H, 40);
head_add(H, 50);
FILE* fp = fopen(argv[1],"w");
if(fp ==NULL)
{
perror("fopen");
return -1;
}
linkListPtr q = H;
for(int i=0;i<H->len;i++)
{
q = q->next;
fprintf(fp,"%d",q->data);
}
fclose(fp);
return 0;
}