系统编程文件部分
This commit is contained in:
28
Chapter13/io/stdio/atoi.c
Normal file
28
Chapter13/io/stdio/atoi.c
Normal 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);
|
||||
}
|
||||
20
Chapter13/io/stdio/fflush.c
Normal file
20
Chapter13/io/stdio/fflush.c
Normal 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);
|
||||
}
|
||||
32
Chapter13/io/stdio/fgetc.c
Normal file
32
Chapter13/io/stdio/fgetc.c
Normal 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);
|
||||
}
|
||||
28
Chapter13/io/stdio/flen.c
Normal file
28
Chapter13/io/stdio/flen.c
Normal 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);
|
||||
}
|
||||
@@ -25,5 +25,7 @@ int main()
|
||||
|
||||
puts("OK!");
|
||||
|
||||
fclose(fp);
|
||||
|
||||
exit(0);
|
||||
}
|
||||
32
Chapter13/io/stdio/maxfopen.c
Normal file
32
Chapter13/io/stdio/maxfopen.c
Normal 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);
|
||||
}
|
||||
51
Chapter13/io/stdio/mycpy.c
Normal file
51
Chapter13/io/stdio/mycpy.c
Normal 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);
|
||||
}
|
||||
48
Chapter13/io/stdio/mycpy_fgets.c
Normal file
48
Chapter13/io/stdio/mycpy_fgets.c
Normal 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);
|
||||
}
|
||||
50
Chapter13/io/stdio/mycpy_fread.c
Normal file
50
Chapter13/io/stdio/mycpy_fread.c
Normal 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);
|
||||
}
|
||||
Reference in New Issue
Block a user