Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/56.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
显示文件的大小[C]_C_File_Ftell - Fatal编程技术网

显示文件的大小[C]

显示文件的大小[C],c,file,ftell,C,File,Ftell,我正在制作一个简单的套接字程序,将文本文件或图片文件发送到另一个连接到端口的套接字。但是,我还想将文件的大小发送到客户机套接字,以便它知道要接收多少字节 我还想实现一些东西,我可以发送一定数量的字节,而不是文件本身。例如,如果我想发送的文件是14003字节,我想发送400字节,那么只发送400字节 我正在实施这样的措施: #include <stdio.h> int main(int argc, char* argv[]) { FILE *fp; char*

我正在制作一个简单的套接字程序,将文本文件或图片文件发送到另一个连接到端口的套接字。但是,我还想将文件的大小发送到客户机套接字,以便它知道要接收多少字节

我还想实现一些东西,我可以发送一定数量的字节,而不是文件本身。例如,如果我想发送的文件是14003字节,我想发送400字节,那么只发送400字节

我正在实施这样的措施:

 #include <stdio.h>

 int main(int argc, char* argv[]) {
     FILE *fp;
     char* file = "text.txt";
     int offset = 40;
     int sendSize = 5;
     int fileSize = 0;

     if ((fp = fopen(file, "r")) == NULL) {
         printf("Error: Cannot open the file!\n");
         return 1;
     } else {
         /* Seek from offset into the file */
         //fseek(fp, 0L, SEEK_END);
         fseek(fp, offset, sendSize + offset); // seek to sendSize
         fileSize = ftell(fp); // get current file pointer
         //fseek(fp, 0, SEEK_SET); // seek back to beginning of file
     }

     printf("The size is: %d", fileSize);
 }
#包括
int main(int argc,char*argv[]){
文件*fp;
char*file=“text.txt”;
整数偏移=40;
int sendSize=5;
int fileSize=0;
if((fp=fopen(文件,“r”))==NULL){
printf(“错误:无法打开文件!\n”);
返回1;
}否则{
/*从偏移量搜索到文件中*/
//fseek(fp,0L,SEEK_END);
fseek(fp,offset,sendSize+offset);//查找sendSize
fileSize=ftell(fp);//获取当前文件指针
//fseek(fp,0,SEEK_SET);//返回到文件开头
}
printf(“大小为:%d”,文件大小);
}
offset
几乎要在文件中放入40个字节,然后将任何
sendSize
字节发送到另一个程序

我一直得到
0
的输出,而不是
5
。有什么原因吗?

你可以试试这个

#include <stdio.h>

int main(int argc, char* argv[]) {
    FILE *fp;
    char* file = "text.txt";
    int offset = 40;
    int sendSize = 5;
    int fileSize = 0;

    if ((fp = fopen(file, "r")) == NULL) {
        printf("Error: Cannot open the file!\n");
        return 1;
    } else {
        fseek(fp, 0L, SEEK_END);
        fileSize = ftell(fp);
    }

    printf("The size is: %d", fileSize);
}
#包括
int main(int argc,char*argv[]){
文件*fp;
char*file=“text.txt”;
整数偏移=40;
int sendSize=5;
int fileSize=0;
if((fp=fopen(文件,“r”))==NULL){
printf(“错误:无法打开文件!\n”);
返回1;
}否则{
fseek(fp,0L,SEEK_END);
fileSize=ftell(fp);
}
printf(“大小为:%d”,文件大小);
}
您可以试试这个

#include <stdio.h>

int main(int argc, char* argv[]) {
    FILE *fp;
    char* file = "text.txt";
    int offset = 40;
    int sendSize = 5;
    int fileSize = 0;

    if ((fp = fopen(file, "r")) == NULL) {
        printf("Error: Cannot open the file!\n");
        return 1;
    } else {
        fseek(fp, 0L, SEEK_END);
        fileSize = ftell(fp);
    }

    printf("The size is: %d", fileSize);
}
#包括
int main(int argc,char*argv[]){
文件*fp;
char*file=“text.txt”;
整数偏移=40;
int sendSize=5;
int fileSize=0;
if((fp=fopen(文件,“r”))==NULL){
printf(“错误:无法打开文件!\n”);
返回1;
}否则{
fseek(fp,0L,SEEK_END);
fileSize=ftell(fp);
}
printf(“大小为:%d”,文件大小);
}

我认为由于第三个参数,您的搜索不起作用: 尝试用
搜索
(fp、偏移、寻道集)

由于他将尝试使用数字sendSize+Offset作为“origin”常量,因此会将其与下面的3个常量值进行比较(它是0、1或2),并且由于没有任何比较,它似乎始终返回0

参数

流、偏移、原点
用作偏移参考的位置。它由中定义的以下常量之一指定,以用作此函数的参数:

恒定参考位置
查找\u设置文件的开头
查找文件指针的当前位置

SEEK_END of file

我认为由于第三个参数,您的SEEK无法工作: 尝试用
搜索
(fp、偏移、寻道集)

由于他将尝试使用数字sendSize+Offset作为“origin”常量,因此会将其与下面的3个常量值进行比较(它是0、1或2),并且由于没有任何比较,它似乎始终返回0

参数

流、偏移、原点
用作偏移参考的位置。它由中定义的以下常量之一指定,以用作此函数的参数:

恒定参考位置
查找\u设置文件的开头
查找文件指针的当前位置
SEEK_END of file

将fseek()搜索到底,然后ftell()方法是获取文件大小的一种合理的可移植方法,但不能保证正确。它不会透明地处理换行符/回车符转换,因此,该标准实际上不能保证ftell()的返回除了用于寻找相同位置以外的任何用途

唯一可移植的方法是读取文件,直到数据用完,并保留字节计数。或stat()使用(非ANSI)Unix标准函数创建文件

fseek()到最后,ftell()方法是获取文件大小的一种合理的可移植方法,但不能保证是正确的。它不会透明地处理换行符/回车符转换,因此,该标准实际上不能保证ftell()的返回除了用于寻找相同位置以外的任何用途


唯一可移植的方法是读取文件,直到数据用完,并保留字节计数。或stat()使用(非ANSI)Unix标准函数创建文件

您可能正在以文本模式打开文件,如下所示

您不能使用
ftell()
获取以文本模式打开的文件的大小。根据7.21.9.4 C标准的
ftell
功能

对于文本流,其文件位置指示器包含未指定的信息,可由
fseek
函数用于返回文件 将流的位置指示器设置为其当时的位置
ftell
呼叫的这两种回报之间的差异 值不一定是对数据数量的有意义的度量 写入或读取的字符

即使返回文件的“大小”,转换为“文本”可能会改变实际读取的字节数

使用
fseek()。Per7.21.9.2
fseek
功能

二进制流不需要有意义地支持带有
SEEK\u END的
值从哪里来


您可能正在以文本模式打开文件,如下所示: