文章目录
- 标准IO和文件IO
-
- 1.fopen打开流
- 2.perror错误处理
- 3.strerror错误处理
- 4.fclose关闭流
- 5.fgetc按字符输入
- 6.fputc按字符输出
- 7.fgets按行输入
- 8.fputs按行输出
- 9.fread和fwrite对象的输入和输出
- 10.fflush刷新流
- 11.ftell、fseek、rewind定位流
- 12.ferror、feof判断流是否出错或结束
- 13.printf、sprintf、fprintf格式化输出
- 14.fscanf、sscanf格式化输入
- 15.open打开文件
- 16.close关闭文件
- 17.read读文件
- 18.write写文件
- 19.lseek定位文件
- 20.opendir打开目录
- 21.readdir读取目录流中的内容
- 22.closedir关闭目录文件
- 23.chmod、fchmod修改文件访问权限
- 24.stat、lstat、fstat获取文件属性
- 进程和线程
-
- 1.fork创建进程
- 2.exit进程结束
- 3.wait进程回收
- 4.waitpid回收指定的子进程
- 5.exec函数族
- 6.system对exec函数的封装
- 7.pthread_create创建线程、pthread_self查看自己的tid
- 8.pthread_join回收线程
- 9.pthread_exit结束当前线程
- 10.pthread_detach线程自动回收
- 11.pthread_cancel取消线程
- 12.sem_init、sem_wait、sem_post信号量实现线程同步机制
- 13.pthread_mutex_init、pthread_mutex_lock、pthread_mutex_unlock互斥锁实现线程间互斥机制
- 14.pipe进程间通信——无名管道
- 15.mkfifo进程间通信——有名管道
- 16.kill、raise、alarm、pause进程间通信——信号相关函数
- 17.signal设置信号捕捉
- 18.SIGCHLD子进程结束信号
- 19.ftok生成key
- 20.shmget、shmat、shmdt、shmctl进程间通信——共享内存
- 21.msgget、msgsnd、msgrcv、msgctl进程间通信——消息队列
- 22.semget、semctl、semop进程间通信——信号灯
- 网络编程
-
- 1.socket创建套接字
- 2.bind绑定端口号和地址
- 3.listen把主动套接字变成被动套接字
- 4. accept阻塞等待客户端连接请求
- 5.connect客户端阻塞等待连接函数
- 6.htonl、htons将主机字节序转换成网络字节序
- 7.ntohl、ntohs将网络字节序转换成主机字节序
- 8.inet_addr IP地址转换成网络字节序
- 9.inet_pton、inet_ntop IP地址和网络字节序相互转换
- 10.send、write网络数据发送
- 11.recv、read网络数据接收
- 12.sendto、recvfrom udp编程函数
- 13.select多路复用
- 14.FD_ZERO、FD_SET、FD_CLR、FD_ISSET多路复用相关函数
- 15.epoll多路复用
标准IO和文件IO
1.fopen打开流
FILE *fopen(const char *path, const char *mode);
此函数可用于打开一个标准IO流。成功时返回流指针,出错时返回NULL。除去stdin、stdout、stderr(标准输入、标准输出、标准错误)一共可打开1021个流。
path
:要打开的文件路径
mode
:
用法举例
:
#include <stdio.h>
int main(int argc, const char *argv[])
{
FILE *fp;
// fp = fopen("1.txt", "r");
// fp = fopen("1.txt", "r+");
fp = fopen("1.txt", "w");
// fp = fopen("1.txt", "w+");
// fp = fopen("1.txt", "a");
// fp = fopen("1.txt", "a+");
printf("fp = %x\n", (int)fp);
return 0;
}
2.perror错误处理
void perror(const char *str);
把一个描述性错误消息输出到标准错误 stderr。
str
:自定义的提示错误信息
用法举例
:
#include <stdio.h>
int main ()
{
FILE *fp;
/* 首先重命名文件 */
rename("file.txt", "newfile.txt");
/* 现在让我们尝试打开相同的文件 */
fp = fopen("file.txt", "r");
if( fp == NULL ) {
perror("Error");
return(-1);
}
fclose(fp);
return(0);
}
//Error: No such file or directory
3.strerror错误处理
extern int errno;
char *strerror(int errno);
根据错误号返回对应的错误信息。
#include <stdio.h>
#include <errno.h>
#include <string.h>
int main(int argc, const char *argv[])
{
FILE *fp;
fp = fopen("1.txt", "r");
printf("%s\n", strerror(errno));
printf("fp = %x\n", (int)fp);
return 0;
}
//No such file or directory
4.fclose关闭流
int fclose(FILE *stream);
关闭流,在进行完流操作(打开、读写)后最后要关闭流。
5.fgetc按字符输入
用法举例
:
#include <stdio.h>
int main(int argc, const char *argv[])
{
/* char ch;
ch = fgetc(stdin);
printf("read:%c\n", ch);
*/
FILE *fp = fopen("1.txt", "r");
if (fp != NULL)
printf("read:%c\n", fgetc(fp));
else
perror("fopen");
return 0;
}
//read:a
6.fputc按字符输出
用法举例
:
#include <stdio.h>
#include <errno.h>
#include <string.h>
int main(int argc, const char *argv[])
{
FILE *fp = fopen("1.txt", "a");
if (fp != NULL)
//将x写到fp代表的流文件中
printf("write:%c\n", fputc('x', fp));
else
printf("%s\n", strerror(errno));
return 0;
}
7.fgets按行输入
用法举例
:
abcd abcde(因为最后一个是\0)
8.fputs按行输出
用法举例
:
#include <stdio.h>
int main(int argc, const char *argv[])
{
char ch[5];
fgets(ch, 5, stdin);
// puts(ch);
fputs(ch, stdout);
return 0;
}
9.fread和fwrite对象的输入和输出
用法举例
:
#include <stdio.h>
#include <string.h>
typedef struct
{
char name[16];
int no;
char sex[8];
}Student;
int main(int argc, const char *argv[])
{
FILE *fp;
fp = fopen("1.txt", "w+");
if (fp == NULL)
return -1;
Student stu;
strcpy(stu.name, "zhangsan");
stu.no = 10;
strcpy(stu.sex, "male");
int fw = fwrite(&stu, sizeof(stu), 1, fp);
if (fw >0)
{
fread(&stu, sizeof(stu), 1, fp);
printf("name:%s, no:%d, sex:%s\n", stu.name, stu.no, stu.sex);
}
fclose(fp);
return 0;
}
10.fflush刷新流
不再等待缓冲区的数据,而是直接将流缓冲区中的数据写入实际文件。
用法举例
:
#include <stdio.h>
int main(int argc, const char *argv[])
{
int i;
for (i = 0; i < 100; i++)
{
printf("a");
}
fflush(stdout);
while(1);
return 0;
}
一般地,当遇到换行符或return时才会释放缓冲区。
11.ftell、fseek、rewind定位流
用法举例
:
#include <stdio.h>
int main(int argc, const char *argv[])
{
FILE *fp = fopen("2.txt", "a+");
if (fp == NULL)
{
perror("fopen");
return -1;
}
printf("ftell:%ld\n", ftell(fp));
fputs("xxx", fp);
printf("ftell:%ld\n", ftell(fp));
fseek(fp, 0, SEEK_SET);
//rewind(fp);定位到文件头
char ch[5];
fgets(ch, 4, fp);
printf("read:%s\n", ch);
return 0;
}
12.ferror、feof判断流是否出错或结束
13.printf、sprintf、fprintf格式化输出
printf拼接输出到标准输出,fprintf拼接输出到stream所代表的流文件中, sprintf拼接输出到s代表的字符串中
用法举例
:
#include <stdio.h>
int main(int argc, const char *argv[])
{
int year = 2021;
int mon = 5;
int day = 25;
FILE *fp = fopen("2.txt", "w");
if (fp)
{
fprintf(fp, "%04d-%02d-%02d\n", year, mon, day);
}
char ch[16];
sprintf(ch, "%04d-%02d-%02d\n", year, mon, day);
puts(ch);
return 0;
}
//2.txt中会有日期的内容,字符串ch中会有日期的内容
14.fscanf、sscanf格式化输入
15.open打开文件
用法举例
:
16.close关闭文件
文件IO结束后最后用close关闭文件。
17.read读文件
用法举例
:
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
int main(int argc, const char *argv[])
{
int fd;
int n = 0;
char buf[5];
fd = open("1.txt", O_RDONLY);
if (fd)
{
while (read(fd, buf, 1) > 0)
{
n++;
}
printf("n = %d\n", n);
}
else
perror("open");
return 0;
}
//计算文件大小
18.write写文件
用法举例
:
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
int main(int argc, const char *argv[])
{
int fd;
fd = open("1.txt", O_WRONLY | O_CREAT, 00666);
char buf[10];
if (fd)
{
fgets(buf, 8, stdin);
write(fd, buf, 8);
}
return 0;
}
//从键盘输入一行写到文件中去
19.lseek定位文件
20.opendir打开目录
name
:要打开的文件目录
用法举例
:
int main()
{
DIR *dir;
dir = open(".");
if(dir)//if dir != NULL打开成功
{
...
}
return 0;
}
21.readdir读取目录流中的内容
用法举例
:
#include <stdio.h>
#include <dirent.h>
int main(int argc, const char *argv[])
{
DIR *dir;
struct dirent *dent;
dir = opendir(".");
if (dir)
{
while ((dent = readdir(dir)) != NULL)
{
printf("%s\n", dent->d_name);
}
}
return 0;
}
//读取目录下所有文件名