gpt4 book ai didi

c - 在 C 中一次从文件中读入 X 个字符

转载 作者:行者123 更新时间:2023-11-30 17:36:39 24 4
gpt4 key购买 nike

我正在尝试编写一个程序,允许我从文件中读取数据,然后使用数据层将其发送到另一个程序,该程序将其写入另一个文件。我遇到的问题是帧大小限制为 100 个字符,包括 1 个字符的 header 和一个 2 个字符的 CRC 值(我稍后将添加)。这意味着我一次读取 97 个字符,然后将其发送出去,但我不知道如何只读取该 man 字符而不是清除字符并重新开始。我将发布我拥有的代码,并且感谢任何帮助。

发送文件

#include <stdio.h>
#define MAXFRAME 97
main(int argc, char* argv[]){
char *frame;
int len = 0;
int c;
dlinits("spirit.cba.csuohio.edu", 43520);
frame = malloc(MAXFRAME);

FILE *file = fopen(argv[1], "r");
if (file == NULL)
return NULL;

while ((c = fgetc(file)) != EOF)
{
frame[len++] = (char) c;
}


}

接收文件

#include <string.h>
char* dlrecv();

main(){
char* test[100];
dlinitr(43520);
strcpy(test,dlrecv());

printf("%s\n", test);



}

数据层

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <fcntl.h>
#include <errno.h>
#include <stdio.h>
#define BUFMAX 100

static int sk;
static struct sockaddr_in remote;
static struct sockaddr_in local;

dlinits(char* host, int port){//initialize sender

struct hostent *hp;
sk = socket(AF_INET, SOCK_DGRAM, 0);

remote.sin_family = AF_INET;

hp = gethostbyname(host);
if (hp == NULL){
printf("Can't find host name\n");
exit(1);
}

bcopy(hp->h_addr,&remote.sin_addr.s_addr,hp->h_length);

remote.sin_port = ntohs(port);
}

dlinitr(int port){//initialize receiver
int rlen = sizeof(remote);
int len = sizeof(local);
char buf[BUFMAX];

sk = socket(AF_INET,SOCK_DGRAM,0);

local.sin_family = AF_INET;
local.sin_addr.s_addr = INADDR_ANY;
local.sin_port = htons(port);
bind (sk, &local,sizeof(local));

getsockname(sk,&local,&len);

}

dlsend(char* msg, int len){//send data

printf("%s\n", msg);
sendto(sk,msg,strlen(msg)+1,0,&remote,sizeof(remote));
}

char* dlrecv(){//receive data
char* msg = malloc(sizeof(char) * 100);

recvfrom(sk,msg,BUFMAX,0,&remote,sizeof(remote));
printf("%s\n", msg);
return msg;
}

最佳答案

核心问题是出去的数据并不总是97 char (+3 开销),但读取始终完成 97+3 char一次。解决这个问题的一些方法是:

1) 填充传出,使其始终为固定数字 char .

2) 接收器寻找可变数量的 char接收带有前导长度值或特殊终止符,例如 \0 .

为了最初的简单性,建议用 \0 填充。

完成后,使用第二种方法。

<小时/>

其他小问题:

3) 在附加 CRC 时,每个字节的 CRC 值范围为 0 到 255。这意味着发送/接收方法是二进制的而不是文本的。然而要发送的数据文件是以文本模式“r”打开的。需要小心潜在的 EOL 文本/二进制转换问题。例如。使用strlen(msg) 。如果msg是整个前导字节+文本+CRC,这将是一个问题。

4)避免魔数(Magic Number)。而是 self 记录它们。

// dlinits("spirit.cba.csuohio.edu", 43520);
// dlinitr(43520);

// In some common header file
#define DATA_LAYER_PORT 43520
dlinits("spirit.cba.csuohio.edu", DATA_LAYER_PORT);
dlinitr(DATA_LAYER_PORT);

5) MAXFRAMEBUFMAX不应独立定义。

// In some common header file
#define MAXFRAME 100
#define BUFMAX (MAXFRAME - 3)

关于c - 在 C 中一次从文件中读入 X 个字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22594107/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com