优化了命名

This commit is contained in:
lzy
2024-04-28 13:10:56 +08:00
parent d75a518c0f
commit ba9f2e37a7
155 changed files with 0 additions and 0 deletions

View File

@@ -0,0 +1 @@
CFLAGS+=-D_FILE_OFFSET_BITS=64 -D_GNU_SOURCE

View File

@@ -0,0 +1,28 @@
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char **argv)
{
char buf[1024];
int year = 2014, month = 5, day = 13;
printf("%d-%d-%d\n", year, month, day);
sprintf(buf, "%d-%d-%d", year, month, day);
puts(buf);
#if 0
char str[] = "123456";
printf("%d\n", atoi(str));
/**
* 输出 123456
*
* 如果str是123a456
* 输出123
*
*/
#endif
exit(0);
}

View File

@@ -0,0 +1,20 @@
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char **argv)
{
int i;
// 这里不加\n的话什么都不输出
// 要么加\n要么fflush
printf("Before while()");
fflush(stdout);
while (1)
;
printf("After while()");
fflush(NULL);
exit(0);
}

View File

@@ -0,0 +1,32 @@
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char **argv)
{
FILE *fp;
int count;
if (argc < 2)
{
fprintf(stderr, "Usage...\n");
exit(1);
}
fp = fopen(argv[1], "r");
if (NULL == fp)
{
perror("fopen()");
exit(1);
}
while (EOF != fgetc(fp))
{
count++;
}
printf("count = %d", count);
fclose(fp);
exit(0);
}

View File

@@ -0,0 +1,28 @@
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char **argv)
{
FILE *fp;
if (argc < 2)
{
fprintf(stderr, "Usage...\n");
exit(1);
}
fp = fopen(argv[1], "r");
if (NULL == fp)
{
perror("fopen()");
exit(1);
}
fseek(fp, 0, SEEK_END);
printf("%d\n", ftell(fp));
fclose(fp);
exit(0);
}

View File

@@ -0,0 +1,31 @@
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
FILE *fp;
fp = fopen("tmp", "r");
if (NULL == fp)
{
// fprintf(stderr, "fopen() failed! errno = %d\n", errno);
// out: fopen() failed! errno = 2
/* errno:定义路径 /usr/include/asm-generic/errno-base.h */
// perror("fopen()");
// out: fopen(): No such file or directory
fprintf(stderr, "fopen(): %s\n", strerror(errno));
// out: fopen(): No such file or directory
exit(1);
}
puts("OK!");
fclose(fp);
exit(0);
}

View File

@@ -0,0 +1,40 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char **argv)
{
FILE *fp;
char *linebuf;
size_t linesize;
if (argc < 2)
{
fprintf(stderr, "Usage...\n");
exit(1);
}
fp = fopen(argv[1], "r");
if (NULL == fp)
{
perror("fopen()");
exit(1);
}
// !!!
linebuf = NULL;
linesize = 0;
while (1)
{
if (getline(&linebuf, &linesize, fp) < 0)
break;
printf("%d\n", strlen(linebuf));
printf("%d\n", linesize);
// 此时输出linesize值不对是因为没有初始化
// 修改后发现linesize初始值就是120是后面不够再增加
}
fclose(fp);
exit(0);
}

View File

@@ -0,0 +1,32 @@
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
FILE *fp = NULL;
int count = 0;
while (1)
{
fp = fopen("tmp", "w");
if (NULL == fp)
{
perror("fopen()");
break;
}
count++;
}
printf("count = %d\n", count);
// out: 4089
/* 可以通过下面这个命令限制 */
/* ulimit -n */
/* 4096 */
exit(0);
}

View File

@@ -0,0 +1,51 @@
#include <stdio.h>
#include <stdlib.h>
/**
* @brief
* @details
* usage: ./mycpy <src> <dst>
*
* @return int
*/
int main(int argc, char **argv)
{
FILE *fps, *fpd;
int ch;
if (argc < 3)
{
fprintf(stderr, "Usage:%s <src> <dst>\n", argv[0]);
exit(1);
}
fopen(argv[1], "r");
if (NULL == fps)
{
perror("fopen()");
exit(1);
}
fopen(argv[2], "w");
if (NULL == fpd)
{
// !!!
fclose(fps);
perror("fopen()");
exit(1);
}
while (1)
{
ch = fgetc(fps);
if (EOF == ch)
break;
fputc(ch, fpd);
}
fclose(fpd);
fclose(fps);
exit(0);
}

View File

@@ -0,0 +1,48 @@
#include <stdio.h>
#include <stdlib.h>
#define BUFSIZE 1024
/**
* @brief
* @details
* usage: ./mycpy <src> <dst>
*
* @return int
*/
int main(int argc, char **argv)
{
FILE *fps, *fpd;
char buf[BUFSIZE];
if (argc < 3)
{
fprintf(stderr, "Usage:%s <src> <dst>\n", argv[0]);
exit(1);
}
fopen(argv[1], "r");
if (NULL == fps)
{
perror("fopen()");
exit(1);
}
fopen(argv[2], "w");
if (NULL == fpd)
{
// !!!
fclose(fps);
perror("fopen()");
exit(1);
}
while (NULL != fgets(buf, BUFSIZE, fps))
fputs(buf, fpd);
fclose(fpd);
fclose(fps);
exit(0);
}

View File

@@ -0,0 +1,50 @@
#include <stdio.h>
#include <stdlib.h>
#define BUFSIZE 1024
/**
* @brief
* @details
* usage: ./mycpy <src> <dst>
*
* @return int
*/
int main(int argc, char **argv)
{
FILE *fps, *fpd;
char buf[BUFSIZE];
int n;
if (argc < 3)
{
fprintf(stderr, "Usage:%s <src> <dst>\n", argv[0]);
exit(1);
}
fopen(argv[1], "r");
if (NULL == fps)
{
perror("fopen()");
exit(1);
}
fopen(argv[2], "w");
if (NULL == fpd)
{
// !!!
fclose(fps);
perror("fopen()");
exit(1);
}
/* 用n接收读到的个数因为不知道能不能正好读到BUFSIZE */
while ((n = fread(buf, 1, BUFSIZE, fps)) > 0)
fread(buf, 1, n, fpd);
fclose(fpd);
fclose(fps);
exit(0);
}

View File

@@ -0,0 +1,27 @@
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main(int argc, char **argv)
{
putchar('a');
write(1, "b", 1);
putchar('a');
write(1, "b", 1);
putchar('a');
write(1, "b", 1);
/**
* 这里标准IO与文件IO混用
* 实际输出:
* bbbaaa
*
* 命令 strace
* 可以跟踪系统调用
*
*/
exit(0);
}

View File

@@ -0,0 +1,51 @@
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#define FNAME "/tmp/out"
int main(int argc, char **argv)
{
int fd;
/***
* bug:
* 1. 假如默认没有1这里有bug。
* 2. 假如这个非原子操作的操作的过程,有可能被别的进程影响。
*/
fd = open(FNAME, O_WRONLY | O_CREAT | O_TRUNC, 0600);
if (fd < 0)
{
perror("open");
exit(1);
}
// close(1);
// dup(fd);
dup2(fd, 1);
// 使用 dup2 原子操作
// 且dup2如果两个参数相等不会做任何事
// !!!
if (1 != fd)
close(fd);
#if 0
close(1);
fd = open(FNAME, O_WRONLY | O_CREAT | O_TRUNC, 0600);
if (fd < 0)
{
perror("open");
exit(1);
}
#endif
/********************************************************************/
puts("hello!");
// TODO: 将sdtout回到终端
exit(0);
}

View File

@@ -0,0 +1,71 @@
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#define BUFSIZE 1024
/**
* 可以用 time 命令找到 BUFSIZE 最佳值
*
* time ./mycpy /etc/services /tmp/out
*/
int main(int argc, char **argv)
{
int sfd, dfd;
char buf[BUFSIZE];
int len, ret, pos;
if (argc < 3)
{
fprintf(stderr, "Usage: %s <src> <dst>\n", argv[0]);
exit(1);
}
sfd = open(argv[1], O_RDONLY);
if (sfd < 0)
{
perror("open");
exit(1);
}
dfd = open(argv[2], O_WRONLY | O_CREAT | O_TRUNC, 0600);
if (dfd < 0)
{
// !!!
close(sfd);
perror("open");
exit(1);
}
while (1)
{
len = read(sfd, buf, BUFSIZE);
if (len < 0)
{
perror("read");
break;
}
if (len == 0)
break;
pos = 0;
while (len > 0)
{
/* 可能没写完被别的中断打断 */
ret = write(dfd, buf + pos, len);
if (ret < 0)
{
perror("write");
exit(1);
}
pos += ret;
len -= ret;
}
}
close(dfd);
close(sfd);
exit(0);
}