- 論壇徽章:
- 0
|
以下代碼來自《C語言庫函數(shù)使用大全》,我在帶有g(shù)cc編譯器的IDE上編譯通過,但在運行時要么發(fā)生segmentation error,要么發(fā)生內(nèi)存讀錯誤。在帶有其他編譯器的IDE上編譯通過,運行時也沒有明顯錯誤,只是在屏幕上顯示failed to set up buffer for input file和failed to set up buffer for output file。看來還都應(yīng)該是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;
- }
復制代碼 |
|