所有读取文件的 C 函数甚至在到达 SEEK_END 之前就设置了 EOF

All C functions to read a file sets EOF even before reaching SEEK_END

我正在编写一个 C 程序来在连接到公共网络的两台计算机(运行 Windows OS)之间共享文件(只是一个实验室项目)。当我阅读 .docx 或 .gif 等非 .txt 文件时,我无法获取整个文件。大多数时候,服务器程序向客户端发送了一些空字符(或一些未知字符),但客户端从未收到过。 这是我的服务器程序:

#include<io.h>
#include<stdio.h>
#include<winsock2.h>
#include<string.h>

#pragma comment(lib,"ws2_32.lib") //Winsock Library

int main(int argc , char *argv[])
{
    WSADATA wsa;
    SOCKET s , new_socket;
    struct sockaddr_in server , client;
    int c;
    char message[10240];
    char filename[100];
    char response[50];
    int k = 0;

    printf("Enter filename :");
    scanf("%s",&filename);

    FILE *f = fopen(filename, "rb");
    fseek(f, 0, SEEK_END);
    long fsize = ftell(f);
    fseek(f, 0, SEEK_SET);  /* same as rewind(f); */

    char *string = malloc(fsize + 1);
    int count = 0;
    while(count < fsize){
        string[count++] = getc(f);
        if(getc(f) == EOF) 
            fseek(f, 0, count+1);
    }
    fclose(f);

    printf("\nInitialising Winsock...");
    if (WSAStartup(MAKEWORD(2,2),&wsa) != 0)
    {
        printf("Failed. Error Code : %d",WSAGetLastError());
        return 1;
    }

    printf("Initialised.\n");

    //Create a socket
    if((s = socket(AF_INET , SOCK_STREAM , 0 )) == INVALID_SOCKET)
    {
        printf("Could not create socket : %d" , WSAGetLastError());
    }

    printf("Socket created.\n");

    //Prepare the sockaddr_in structure
    server.sin_family = AF_INET;
    server.sin_addr.s_addr = INADDR_ANY;
    server.sin_port = htons( 8888 );

    //Bind
    if( bind(s ,(struct sockaddr *)&server , sizeof(server)) == SOCKET_ERROR)
    {
        printf("Bind failed with error code : %d" , WSAGetLastError());
    }

    puts("Bind done");

    //Listen to incoming connections
    listen(s , 3);

    //Accept and incoming connection
    puts("Waiting for incoming connections...");

    c = sizeof(struct sockaddr_in);
    new_socket = accept(s , (struct sockaddr *)&client, &c);
    if (new_socket == INVALID_SOCKET)
    {
        printf("accept failed with error code : %d" , WSAGetLastError());
    }

    puts("Connection accepted");

    //Reply to client
    // message = "Hello Client , I have received your connection. But I have to go now, bye\n";
    send(new_socket , filename , strlen(filename) , 0);

    int len = recv(new_socket, response, 50, 0);
    response[len] = '[=11=]';

    if(strcmp(response, "OKAY") == 0) printf("SUCCESS");
    else printf("ERROR");

    int i = 0;
    char cn = 'a';
    // getchar();
    int j = 0;
    int ssize = strlen(string);
    printf("FIle size is : %d",ssize);

    getchar(); getchar();

    while(j < ssize){
        while(i < 1024){
            message[i++] = string[j++];
            if(j == ssize) break;
        }
        printf("sent :%s\n",message);
        send(new_socket, message, strlen(message), 0);
        printf("%d\n",k); k++;
        i = 0;
    }

    send(new_socket, "...END...CONNECTION...", strlen("...END...CONNECTION..."), 0);
    printf("sent end connection");
    recv(new_socket, response, strlen(response), 0);

    if(strcmp(response, "...END...CONNECTION...") != 0)
        send(new_socket, "...END...CONNECTION...", strlen("...END...CONNECTION..."), 0);

    printf("SUCCESS");
    closesocket(s);
    WSACleanup();

    return 0;
}

这是我的客户端程序:

#include<stdio.h>;
#include<winsock2.h>;

#pragma comment(lib,&quot;ws2_32.lib&quot;) //Winsock Library

int main(int argc , char *argv[])
{
    WSADATA wsa;
    SOCKET s;
    struct sockaddr_in server;
    char *message , server_reply[1024];
    int recv_size;

    printf("\nInitialising Winsock...");
    if (WSAStartup(MAKEWORD(2,2),&wsa) != 0)
    {
        printf(";Failed. Error Code : %d",WSAGetLastError());
        return 1;
    }

    printf("Initialised.\n");

    //Create a socket
    if((s = socket(AF_INET , SOCK_STREAM , 0 )) == INVALID_SOCKET)
    {
        printf("Could not create socket : %d" , WSAGetLastError());
    }

    printf("Socket created.\n");


    server.sin_addr.s_addr = inet_addr("127.0.0.1");
    server.sin_family = AF_INET;
    server.sin_port = htons( 8888 );

    //Connect to remote server
    if (connect(s , (struct sockaddr *)&server , sizeof(server)) < 0)
    {
        puts("connect error");
        return 1;
    }

    puts("Connected");

    if((recv_size = recv(s , server_reply , 1024 , 0)) == SOCKET_ERROR)
    {
        puts("recv failed");
    }

    puts("filename : ");
    server_reply[recv_size] = '[=12=]';
    puts(server_reply);

    FILE* fptr = fopen(server_reply, "wb");

    send(s , "OKAY" , strlen("OKAY") , 0);
    int i = 0;
    while(1){
        if((recv_size = recv(s , server_reply , 1024, 0)) == SOCKET_ERROR)
        {
            puts("recv failed"); break;
        }
        if(strcmp(server_reply, "...END...CONNECTION...") == 0) {
            printf("recieved end connection"); break;
        }
        fputs(server_reply, fptr);
        server_reply[recv_size] = '[=12=]';
        printf("%s\n", server_reply);
        printf("%d,\n",i++);
    }

    send(s, "...END...CONNECTION...", strlen("...END...CONNECTION..."), 0);
    printf("sent : end connection");
    fclose(fptr);
    puts("File received\n");

    return 0;
}

然后我想知道我的程序实际读取了多少字节。所以我写了一个简单的程序来读取文件,但这一次,我将使用 fseek 函数找到 EOF 并打印它,读取文件,然后打印它读取的缓冲区的长度。 这是我使用的代码:

#include <stdio.h>
#include <stdlib.h>

void main(){
    printf("Enter file : ");
    char filename[50];
    scanf("%s",&filename);

    FILE *f = fopen(filename, "rb");
    fseek(f, 0, SEEK_END);
    long fsize = ftell(f);
    fseek(f, 0, SEEK_SET);  /* same as rewind(f); */

    printf("File size is : %ld bytes (according to SEEK_END)\n", fsize);

    char *string = malloc(fsize + 1);
    fread(string, 1, fsize, f);
    long len = strlen(string);

    printf("File size is : %ld bytes (according to freed)\n", len);

    fclose(f);

    string[fsize] = 0;
    puts(string);
}

我得到的输出是这样的:

Enter file : 1.docx
File size is : 12920 bytes (according to SEEK_END)
File size is : 5 bytes (according to freed)
PK

谁能帮我解决这个问题:(我真的不知道为什么这两个值不同。 它们应该是相等的,对吧?

所以我尝试使用 fsanf、fgets、read 读取 .docx 文件。所以我的理解是,Fscanf 和 fgets 在文件中搜索 ascii 字符,因此不适合读取二进制文件。 fread 读取二进制文件,但您不能将其存储在 char 数组中,相反我们可以使用 uint8_t。 进行这些更改后,现在效果很好。

所以结论是,所有的函数都读到EOF,但是我们用来存储它们的数据类型不能容纳所有的值。这就是为什么我在使用 strlen() 时得到不同值的原因。

这是我得到的输出

Enter file : 1.docx
File size is : 12920 bytes (according to SEEK_END)
File size is : 12920 bytes (according to freed)
PK

当我把代码改成这个的时候

#include <stdio.h>
#include <stdlib.h>

void main(){
    printf("Enter file : ");
    char filename[50];
    scanf("%s",&filename);

    FILE *f = fopen(filename, "rb");
    fseek(f, 0, SEEK_END);
    long fsize = ftell(f);
    fseek(f, 0, SEEK_SET);  /* same as rewind(f); */

    printf("File size is : %ld bytes (according to SEEK_END)\n", fsize);

    char *string = malloc(fsize + 1);
    long len = fread(string, 1, fsize, f);
    // long len = strlen(string);

    printf("File size is : %ld bytes (according to freed)\n", len);

    fclose(f);

    string[fsize] = 0;
    puts(string);
}

现在更有意义了。