我正在制作一个简单的套接字程序,以将文本文件或图片文件发送到连接到端口的另一个套接字。但是,我还想将文件的大小发送到客户端套接字,以便它知道要接收多少字节。
我还想实现一些可以发送一定数量的字节而不是文件本身的东西。例如,如果我要发送的文件是 14,003 字节,而我想发送 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);
}
offset
几乎会将 40 个字节放入文件中,然后将任何sendSize
字节发送到其他程序。
我不断得到输出0
而不是5
。这背后有什么原因吗?