2016-11-07 125 views
-1

我已经运行在用Borland C下面的代码并得到了以下错误:在fprintf中:错误故障访问冲突地址0x000000处,读地址为0x00

Thread stopped. j:\bc5\bin\file\pro001.exe:fault access violation at
0x4043cc : read of address 0x12.

#include <stdio.h> 
#include <stdlib.h> 
main()         
{ 
FILE *fp; 
fp=fopen ("C:\Users\MEYSAM\Desktop\1.txt","w+"); 
fprintf (fp,"This is testing for fprintf ...\n"); 
fputs ("that is output filename by reference.",m); 
fclose (fp); 
} 
+2

检查'(fp == NULL)'是否失败并且试图从'fp'读取失败.. –

+2

什么是'm'?它在哪里定义? –

+1

我认为\应该被转义为\\ – Danh

回答

1

也许fopen()失败,返回NULL文件指针。

您必须始终检查I/O操作是否成功。

+0

@Someprogrammerdude是的,当然你是对的。大脑故障,陈述删除。谢谢。 – unwind

1

你需要这样的:

#include <stdio.h> 
#include <stdlib.h> 

int main() 
{ 
    FILE *fp; 
    fp=fopen ("C:\\Users\\MEYSAM\\Desktop\\1.txt","w+"); 
      //^using \\ instead of \, you need to escape the \ character 

    if (fp == NULL) // << checking if file could not be opened 
    { 
    printf("Could not open file.\n"); 
    return 1; 
    }  

    fprintf (fp,"This is testing for fprintf ...\n"); 
    fputs ("that is output filename by reference.", fp); 
              //^replaced m by fp 
    fclose (fp); 
} 
+0

Tnx。太好了。 –

0

首先,如果fopen已经成功了,你没有检查。另一个问题是,在你的代码中写道:

其中
fputs ("that is output filename by reference.",m); 

m是不确定的,应该是文件名,所以它应该是fp

Here你可以找到fputs的用法。