#include <stdio.h>
//功能描述:
//利用fopen()打开一个不存在的文件或一个存在的文件
//通过这个程序了解一下文件的打开方式和报错的方式
//2023.6.27
//Liyumin
int main()
{
FILE *fp ;
//1.尝试打开一个不存在的文件
// if(fp = fopen("1.txt","r") == NULL)
// {
// perror("fail to open ");
// }
//2.打开一个存在的文件
if(fp = fopen("2.txt","r") == NULL)
{
perror("fail to open ");
}
else printf("I have open 2.txt successfully!!!\n");
fclose(fp);
return 0;
}
#include <stdio.h>
//功能描述:
//将键盘上输入的字符串中的数字打印出来
//通过这个程序了解一下标准IO的按字符(字节)输入输出流
//2023.6.27
//Liyumin
int main()
{
int c;
while(1){
c = fgetc(stdin);
if((c>='0')&&(c<='9'))
fputc(c,stdout);
if(c=='\n')
break;
}
return 0;
}
#include <stdio.h>
#include <string.h>
//功能描述:
//统计文件行数 (一次回车算一行)
//通过这个程序了解一下标准IO的fgets();
//2023.6.27
//Liyumin
int main(int argc,char *argv[])
{
int line = 0;
char buf[128];
FILE *fp;
if(argc < 2)
{
printf(" Usage : %s <file>",argv[0]);
return 1;
}
if((fp = fopen(argv[1],"r")) == NULL)
{
perror("fail to open");
return -1;
}
while(fgets(buf,128,fp) != NULL)
{
if(buf[strlen(buf)-1] == '\n')
{
line++;
}
}
printf("Line is : %d",line);
return 0;
}
#include <stdio.h>
#include <string.h>
//功能描述:
//实现Cat指令
//通过这个程序了解一下标准IO的fgetc()\fopen()\;
//2023.6.27
//Liyumin
int main(int argc,char *argv[])
{
FILE *fp;
char ch;
if(argc < 2)
{
printf(" Usage : %s <file>",argv[0]);
return 1;
}
if((fp = fopen(argv[1],"r")) == NULL)
{
perror("fail to open");
return -1;
}
while((ch=fgetc(fp) )!= EOF)
{
printf("%c",ch);
}
fclose(fp);
return 0;
}
#include <stdio.h>
#include <string.h>
//功能描述:
//实现从文件中读取字母
//通过这个程序了解一下标准IO的fgetc()\fopen();
//2023.6.27
//Liyumin
int main(int argc,char *argv[])
{
FILE *fp;
char ch;
if(argc < 2)
{
printf(" Usage : %s <file>",argv[0]);
return 1;
}
if((fp = fopen(argv[1],"r")) == NULL)
{
perror("fail to open");
return -1;
}
while((ch=fgetc(fp) )!= EOF)
{
if((ch>'a')&&(ch<'z') || (ch>'A')&&(ch<'Z'))
printf("%c",ch);
}
fclose(fp);
return 0;
}
#include <stdio.h>
//功能描述:
//实现文件的复制
//通过这个程序了解一下标准IO的fgetc()\fopen();
//2023.6.27
//Liyumin
#define MAX_BUF_SIZE 1024
//命令行输入参数:源文件 目标文件 (没有目标文件系统会自动创建)
int main(int argc, char *argv[]) {
if (argc != 3) {
printf("Usage: %s <source_file> <destination_file>\n", argv[0]);
return 1;
}
//1.打开文件
FILE *source_file = fopen(argv[1], "r");
if (source_file == NULL) {
printf("Failed to open source file.\n");
return 1;
}
FILE *dest_file = fopen(argv[2], "w");
if (dest_file == NULL) {
printf("Failed to open destination file.\n");
fclose(source_file);
return 1;
}
//2.文件内数据的复制
char buffer[MAX_BUF_SIZE];
while (fgets(buffer, MAX_BUF_SIZE, source_file) != NULL) {
if (fputs(buffer, dest_file) == EOF) {
printf("Failed to write to destination file.\n");
break;
}
}
fclose(source_file);
fclose(dest_file);
printf("File copied successfully.\n");
return 0;
}
#include <stdio.h>
//功能描述:
//对文件流按照指定大小进行读写操作
//fread()\fwrite()的使用
//2023.6.29
//Liyumin
#define N 64
//命令行输入参数:源文件 目标文件 (没有目标文件系统会自动创建)
int main(int argc, char *argv[]) {
int n;
char buf[N];
if (argc != 3) {
printf("Usage: %s <source_file> <destination_file>\n", argv[0]);
return 1;
}
//1.打开文件
FILE *source_file = fopen(argv[1], "r");
if (source_file == NULL) {
printf("Failed to open source file.\n");
return 1;
}
FILE *dest_file = fopen(argv[2], "w");
if (dest_file == NULL) {
printf("Failed to open destination file.\n");
fclose(source_file);
return 1;
}
//2.文件内数据的复制
if ((n = fread(buf,1,N,source_file)) >=0) {
fwrite(buf,1,N,dest_file);
}
fclose(source_file);
fclose(dest_file);
printf("File copied successfully.\n");
return 0;
}
#include <stdio.h>
//功能描述:
//实现文件数据的大小
//fseek();ftell();
//2023.6.29
//Liyumin
int main(int argc,char *argv[])
{
FILE *fp;
if(argc < 2)
{
printf("Usage :%s<file>\n",argv[0]);
return -1;
}
if((fp = fopen(argv[1],"r")) == NULL)
{
perror("fail to fopen");
return 1;
}
fseek(fp,0,SEEK_END);
printf("The size of %s is %d \n",argv[1],ftell(fp));
return 0;
}