请教:setvbuf()运行时错误
以下代码来自《C语言库函数使用大全》,我在带有gcc编译器的IDE上编译通过,但在运行时要么发生segmentation error,要么发生内存读错误。在带有其他编译器的IDE上编译通过,运行时也没有明显错误,只是在屏幕上显示failed to set up buffer for input file和failed to set up buffer for output file。看来还都应该是setvbuf()的问题。但具体是哪里出了问题就不得而知了。
请问具体哪里出了问题?谢谢!
#include <stdio.h>
int main(void)
{
FILE *input, *output;
char bufr[512];
input = fopen("file.in", "r+b");
output = fopen("file.out", "w");
/* set up input stream for minimal disk access,
using our own character buffer */
if (setvbuf(input, bufr, _IOFBF, 512) != 0)
printf("failed to set up buffer for input file\n");
else
printf("buffer set up for input file\n");
/* set up output stream for line buffering using space that
will be obtained through an indirect call to malloc */
if (setvbuf(output, NULL, _IOLBF, 132) != 0)
printf("failed to set up buffer for output file\n");
else
printf("buffer set up for output file\n");
/* perform file I/O here */
/* close files */
fclose(input);
fclose(output);
return 0;
}
[解决办法]
我这儿测试得没问题,,是不是文件太大呀
[解决办法]
第一个可能是因为file.in文件不存在。
第二个就不知道了
[解决办法]
input = fopen("file.in", "r+b");
文件都没创建,你怎么读打开,input返回NULL。
[解决办法]
//加点判断,确定每步操作是否成功
input = fopen("file.in", "r+b");
if(input < 0)
{
printf("error open input\n");
}
output = fopen("file.out", "w");
if(output < 0)
{
printf("error open output\n");
}
[解决办法]
用别人的代码前最好先看看