打开文件失败,返回-1 } fseek(file, 0, SEEK_END); // 将文件指针移动到文件末尾 long size = ftell(file); // 获取文件大小 fclose(file); // 关闭文件 return size; // 返回文件大小 } int main() { const char *filePath = "example.txt"; long size = getFileSize(filePath); if (size ...
#include <string.h> //定义获取文件大小的函数 int Get_File_Size() { //打开一个文本文件 FILE *fp = fopen("C:\\Users\\Administrator\\Desktop\\hello.txt", "r"); //判断是否打开成功 if (!fp) { //如果打开失败则返回fopen的错误内容 perror("fopen error"); //结束函数 return -1; } /...
//头文件 #include <stdio.h> #include <stdlib.h> #include <string.h> //定义获取文件大小的函数 int Get_File_Size() { //打开一个文本文件 FILE *fp = fopen("C:\\Users\\Administrator\\Desktop\\hello.txt", "r"); //判断是否打开成功 if (!fp) { //如果打开失败则返回fopen的错误内容 ...
_get_fmode _get_heap_handle _get_invalid_parameter_handler、_get_thread_local_invalid_parameter_handler _get_osfhandle _get_pgmptr _get_printf_count_output _get_purecall_handler、_set_purecall_handler _get_terminate _get_timezone _get_tzname ...
opening file\n");return1;}// Move file pointer to the end of the filefseek(file,0,SEEK_END);// Get the file sizelongfileSize=ftell(file);// Move file pointer back to the beginning of the filefseek(file,0,SEEK_SET);printf("File size: %ld bytes\n",fileSize);fclose(file);return...
1、文件大小查询file_size.c 方法一:fseek + ftell; 方法二:ftell 1#include <stdio.h>2#include <fcntl.h>3#include <stdlib.h>4#include <string.h>5#include <errno.h>67voiderrHandling(constchar*errMsg)8{9printf("%s: %s\n", errMsg, strerror(errno));10exit(-1);11}1213longgetFileSize1...
FILE* fp = fopen(filename, "r"); if (fp==NULL) return -1; fseek(fp, 0L, SEEK_END); return ftell(fp); } stat long get_file_size(char * filename) { struct stat f_stat; if (stat(filename, &f_stat) == -1) { return -1; ...
long filesize(FILE *stream); int main(void) { FILE *stream; stream = fopen("MYFILE.TXT", "w+"); fprintf(stream, "This is a test"); printf("Filesize of MYFILE.TXT is %ld bytes/n", filesize(stream)); fclose(stream);
long filesize(FILE *stream); int main(void) { FILE *stream; stream = fopen("MYFILE.TXT", "w+"); fprintf(stream, "This is a test"); printf("Filesize of MYFILE.TXT is %ld bytes/n", filesize(stream)); fclose(stream);
函数原型: int fseek(FILE *stream, long offset, int fromwhere); 功能:重定位流上的文件指针 在流上重新定位文件结构的位置。fseek设置与流stream相联系的文件指针到新的位置,新位置与fromwhere给定的文件位置的距离为offset字节。 参数: FILE *stream 要重定位的流 ...