使用 c 读取 xdr 编写的文件时出现问题
我使用的是 Ubuntu 10.4,有两个(长)C 程序,一个使用 XDR 写入文件,另一个使用该文件作为输入。然而,第二个程序无法读取写入的文件。一切看起来都很好,但就是行不通。更具体地说,它在此处添加的最后一行失败,并显示错误消息 xdr_string(),这表明它无法读取输入文件的第一行。我没有看到任何明显的错误。输入文件已写出,有内容,我可以使用 stings -a -n 2 "inputfile" 看到正确的字符串。任何人都知道出了什么问题吗?
程序 1(编写器)的相关部分:
/**
* create compressed XDR output stream
*/
output_file=open_write_pipe(output_filename);
xdrstdio_create(&xdrs, output_file, XDR_ENCODE);
/**
* print material name
*/
if( xdr_string(&xdrs, &name, _POSIX_NAME_MAX) == FALSE )
xdr_err("xdr_string()");
程序 2(读取器)的相关部分:
/**
* open data file
*/
input_file=open_data_file(input_filename, "r");
if( input_file == NULL ){
ERROR(input_filename);
exit(EXIT_FAILURE);
}
/**
* create input XDR stream
*/
xdrstdio_create(&xdrs, input_file, XDR_DECODE);
/**
* read material name
*/
if(xdr_string(&xdrs, &name, _POSIX_NAME_MAX) == FALSE)
XDR_ERR("xdr_string()");
I am using Ubuntu 10.4 and have two (long) C programs, one that writes a file using XDR, and one that uses this file as input. However, the second program does not manage to read in the written file. Everything looks perfectly fine, it just does not work. More spesifically it fails at the last line added here with the error message xdr_string(), which indicates that it can not read in the first line of the input file. I do no see any obvious errors. The input file is written out, have a content and I can see the right strings using stings -a -n 2 "inputfile". Anyone have any idea what is going wrong?
Relevant parts of program 1 (writer):
/**
* create compressed XDR output stream
*/
output_file=open_write_pipe(output_filename);
xdrstdio_create(&xdrs, output_file, XDR_ENCODE);
/**
* print material name
*/
if( xdr_string(&xdrs, &name, _POSIX_NAME_MAX) == FALSE )
xdr_err("xdr_string()");
Relevant parts of program 2 (reader):
/**
* open data file
*/
input_file=open_data_file(input_filename, "r");
if( input_file == NULL ){
ERROR(input_filename);
exit(EXIT_FAILURE);
}
/**
* create input XDR stream
*/
xdrstdio_create(&xdrs, input_file, XDR_DECODE);
/**
* read material name
*/
if(xdr_string(&xdrs, &name, _POSIX_NAME_MAX) == FALSE)
XDR_ERR("xdr_string()");
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不知道
open_write_pipe
或open_data_file
是如何工作的,但是您是否尝试过以二进制模式写入/读取文件? 适用于 Windows 的便携式 XDR 就是这种情况。诚然,该示例是针对整数的,但对于字符串来说应该是相同的。I don't know how
open_write_pipe
oropen_data_file
works, but have you tried writing/reading the file in binary mode? This was the case for Portable XDR for Windows. Admittedly, the sample is for integers but it should be the same for strings.