[英]Reading in from a file X characters at a time in C
I'm attempting to write a program that allows me to read data from a file, then, using a data layer, sends it to another program that writes it to another file. 我正在尝试编写一个程序,该程序允许我从文件中读取数据,然后使用数据层将其发送到另一个程序,该程序将其写入另一个文件。 The problem I am having is that I am limited to a frame size of 100 characters, including a 1 character header, and a 2 character CRC value that I will add in later. 我遇到的问题是,我被限制为100个字符的帧大小,包括一个1个字符的标题和一个稍后将添加的2个字符的CRC值。 This means that I am reading in 97 characters at a time, then sending it off, but I'm not sure how to only read in that man characters than clear the char and start again. 这意味着我一次读取97个字符,然后发送出去,但是我不确定如何只读取该字符而不是清除字符并重新开始。 I will post the code I have, and any help is appreciated. 我将发布我拥有的代码,感谢您的帮助。
Send File 发送文件
#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;
}
}
Receive File 接收档案
#include <string.h>
char* dlrecv();
main(){
char* test[100];
dlinitr(43520);
strcpy(test,dlrecv());
printf("%s\n", test);
}
Data Layer 资料层
#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;
}
The core issue is the the data going out is not always 97 char
(+3 overhead), yet the reading is always done 97+3 char
at a time. 核心问题是数据输出不总是97个char
(+3开销),但是每次读取总是完成97 + 3个char
。 Some approaches to solve this are: 解决此问题的一些方法是:
1) Pad the outgoing so it is always a fixed number of char
. 1)填充输出,因此它始终是固定数量的char
。
2) Receiver looks for a variable number of char
to receive either with a lead length value or special terminator like \\0
. 2)Receiver寻找可变数量的char
来接收,该char
具有超前长度值或特殊的终止符,例如\\0
。
For initial simplicity, suggest padding with \\0
. 为了简单起见,建议使用\\0
填充。
After you get things going, use the 2nd method. 一切顺利之后,请使用第二种方法。
Other minor issues: 其他小问题:
3) In appending a CRC, the values of the CRC range from 0 to 255 per each byte. 3)在附加CRC时,每个字节的CRC值范围从0到255。 This implies the sending/receiving method is binary and not text. 这意味着发送/接收方法是二进制而不是文本。 Yet the file of data to send is opened in text mode "r". 然而,要发送的数据文件以文本模式“ r”打开。 Need to be careful about potential EOL text/binary conversion issues. 需要注意潜在的EOL文本/二进制转换问题。 Eg using strlen(msg)
. 例如使用strlen(msg)
。 If msg
is the entire lead byte + text + CRC, this will be a problem. 如果msg
是整个前导字节+文本+ CRC,这将是一个问题。
4) Avoid magic numbers. 4)避免魔术数字。 Rather self document them. 而是自我记录下来。
// 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) MAXFRAME
and BUFMAX
should not be independently defined. 5) MAXFRAME
和BUFMAX
不应独立定义。
// In some common header file
#define MAXFRAME 100
#define BUFMAX (MAXFRAME - 3)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.