C语言获取文件大小:
#define _CRT_SECURE_NO_WARNINGS //去掉安全检查
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
char path1[100] = "C:\\Users\\whx\\Desktop\\test.txt"; //文本文件
char path2[100]= "C:\\Users\\whx\\Desktop\\SpaceSniffer.exe"; //二进制文件
char* ch1 = ".txt";
char* ch2 = ".exe";
int get_file_size(char* path);
int get_file_type(char* path);
void main()
{
int num1= get_file_size(path1);
int num2 = get_file_size(path2);
printf(".txt= %d ; .exe= %d \n",num1, num2);
system("pause");
}
//获取文件大小
int get_file_size(char* path)
{
int count = -1;
FILE* fp=NULL;
if (path == NULL)
{
return count;
}
int flag= get_file_type(path);
if (flag == 1)
{
fp = fopen(path, "r");
}
else if (flag==2)
{
fp = fopen(path, "rb");
}
if (fp != NULL)
{
while (!feof(fp))
{
fgetc(fp);
count++;
}
fclose(fp);
}
return count;
}
/*--------------------------
判断文件类型
.txt 返回 1
.exe 返回 2
--------------------------*/
int get_file_type(char* path)
{
int result = 0;
const char* pStr;
pStr = strrchr(path, '.'); // 判断输入的文件名最后输出.的位置
char str[6] = { 0 };
if (pStr != NULL)
{
strcpy(str, pStr);
if (strncmp(str, ch1,sizeof(ch1))==0)
{
result = 1;
}
else if (strncmp(str, ch2, sizeof(ch2)) == 0)
{
result = 2;
}
}
return result;
}
fseek 函数:
功 能: 重定位流上的文件指针
用 法: int fseek(FILE *stream, long offset, int fromwhere);
描 述: 函数设置文件指针stream的位置。如果执行成功,stream将指向以fromwhere为基准,偏移offset个字 节的位置。如果执行失败(比如offset超过文件自身大小),则不改变stream指向的位置。
返回值: 成功,返回0,否则返回其他值。
第一个参数stream为文件指针
第二个参数offset为偏移量,整数表示正向偏移,负数表示负向偏移
第三个参数origin设定从文件的哪里开始偏移,可能取值为:SEEK_CUR、 SEEK_END 或 SEEK_SET
SEEK_SET: 文件开头
SEEK_CUR: 当前位置
SEEK_END: 文件结尾
其中SEEK_SET,SEEK_CUR和SEEK_END和依次为0,1和2.
ftell 函数:
函数原型:long int ftell(FILE *stream) stream -- 这是指向 FILE 对象的指针,该 FILE 对象标识了流。
返回值:该函数返回位置标识符的当前值。如果发生错误,则返回 -1L,全局变量 errno 被设置为一个正值。
修改上面代码:
if (fp != NULL)
{
/*while (!feof(fp))
{
fgetc(fp);
count++;
}*/
fseek(fp, 0, SEEK_END);//文件指针移动到文件末尾
count = ftell(fp);//获取当前文件指针距离文件开始的长度
fclose(fp);
}
fgets 函数:
C 库函数 char *fgets(char *str, int n, FILE *stream) 从指定的流 stream 读取一行,并把它存储在 str 所指向的字符串内。当读取 (n-1) 个字符时,或者读取到换行符时,或者到达文件末尾时,它会停止,具体视情况而定。
参数:
- str -- 这是指向一个字符数组的指针,该数组存储了要读取的字符串。
- n -- 这是要读取的最大字符数(包括最后的空字符)。通常是使用以 str 传递的数组长度。
- stream -- 这是指向 FILE 对象的指针,该 FILE 对象标识了要从中读取字符的流。
返回值:
如果成功,该函数返回相同的 str 参数。如果到达文件末尾或者没有读取到任何字符,str 的内容保持不变,并返回一个空指针。
如果发生错误,返回一个空指针。
#define _CRT_SECURE_NO_WARNINGS //去掉安全检查
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
char path1[100] = "C:\\Users\\whx\\Desktop\\数学符号.txt"; //文本文件
#define MAX_LEN 256
void main()
{
FILE *pfr = fopen(path1, "r");
if (pfr != NULL)
{
int i = 0;
/*while (!feof(pfr)) //方法1
{
char str[MAX_LEN] = { 0 };
fgets(str, MAX_LEN-1, pfr);
printf("【 %d 】 : %s",i++, str);
}*/
char str[MAX_LEN] = { 0 };
while (fgets(str, MAX_LEN-1, pfr)!=NULL) //方法2
{
printf("【 %d 】 : %s", i++, str);
}
fclose(pfr);
}
system("pause");
}
与源文件对比可以看出是按行读取的,是因为:该函数从stream所指的文件中读取以'\n'结尾的一行(包括'\n'在内)存到缓冲区str中,并且在该行末尾添加一个'\0'组成完整的字符串。另外,fgets()函数不适合取二进制的文件,这样,容易发生错误。
注:fgets从指定的文件中读一行字符到调用者提供的缓冲区中,gets从标准输入读一行字符到调用者提供的缓冲区中。
1.当遇到换行符或者缓冲区已满,fgets就会停止,返回读到的数据,值得注意的是不能用fgets读二进制文件,因为fgets会把二进制文件当成文本文件来处理,这势必会产生乱码。
2.每次调用,fgets都会把缓冲区的最后一个字符设为null,这意味着最后一个字符不能用来存放需要的数据,所以如果有一行,含有LINE_SIZE个字符(包括换行符),要想把这行读入缓冲区,请把参数n设为LINE_SIZE+1
3. 由结论1可推出:给定参数n,fgets只能读取n-1个字符(包括换行符),如果有一行超过n-1个字符,那么fgets返回一个不完整的行,也就是说,只读取该行的前n-1个字符,但是,缓冲区总是以null字符结尾,对fgets的下一次调用会继续读该行。
注意,对于fgets来说,'\n'是一个特别的字符,而'\0'并无任何特别之处,如果读到'\0'就当作普通字符读入。如果文件中存在'\0'字符(或者0x00字节),调用fgets之后就无法判断缓冲区中的'\0'究竟是从文件读上来的字符还是由fgets自动添加的结束符,所以fgets只适合读文本文件而不适合读二进制文件,并且文本文件中的所有字符都应该是可见字符,不能有'\0'。
fputs 函数:
int fputs(const char *s, FILE *stream);
返回值:成功返回一个非负整数(一般是0 【'\0'】),出错返回EOF。
fputs向指定的文件写入一个字符串,puts向标准输出写入一个字符串。
缓冲区s中保存的是以'\0'结尾的字符串,fputs将该字符串写入文件stream,但并不写入结尾的'\0'。与fgets不同的是,fputs并不关心的字符串中的'\n'字符,字符串中可以有'\n'也可以没有'\n'。puts将字符串s写到标准输出(不包括结尾的'\0'),然后自动写一个' \n'到标准输出。
fgets 和 fputs 复制文件:
#define _CRT_SECURE_NO_WARNINGS //去掉安全检查
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
char path1[100] = "C:\\Users\\whx\\Desktop\\数学符号.txt"; //文本文件
char path2[100] = "C:\\Users\\whx\\Desktop\\数学符号A.txt"; //文本文件
#define MAX_LEN 256
void main()
{
FILE *pfr = fopen(path1, "r");
FILE* pfw = fopen(path2, "w");
if (pfr != NULL&& pfw!=NULL)
{
int i = 0;
/*while (!feof(pfr)) //方法1
{
char str[MAX_LEN] = { 0 };
fgets(str, MAX_LEN-1, pfr);
printf("【 %d 】 : %s",i++, str);
}*/
char str[MAX_LEN] = { 0 };
while (fgets(str, MAX_LEN-1, pfr)!=NULL) //方法2
{
//printf("【 %d 】 : %s", i++, str);
fputs(str, pfw);//写入到文件
}
fclose(pfr);
fclose(pfw);
}
system("pause");
}
分类写入文件:
#define _CRT_SECURE_NO_WARNINGS //去掉安全检查
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
#define MAX_LEN 256
char pathA[100] = "C:\\Users\\whx\\Desktop\\test.txt";
char path1A[100] = "C:\\Users\\whx\\Desktop\\testMin.txt";
char path2A[100] = "C:\\Users\\whx\\Desktop\\testMax.txt";
void main()
{
FILE *pfr= fopen(pathA, "r");
FILE* pfw_min = fopen(path1A, "w");
FILE* pfw_max = fopen(path2A, "w");
if (pfr != NULL && pfw_min != NULL && pfw_max != NULL)
{
char str[MAX_LEN] = { 0 };
while (fgets(str, MAX_LEN - 1, pfr) != NULL)
{
int len = strlen(str);
if (len >= 25)
{
fputs(str, pfw_max);//写入到文件
}
else
{
fputs(str, pfw_min);//写入到文件
}
}
fclose(pfr);
fclose(pfw_min);
fclose(pfw_max);
}
system("pause");
}