Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/66.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Dev-C++;文件处理_C_File_File Handling_Dev C++ - Fatal编程技术网

Dev-C++;文件处理

Dev-C++;文件处理,c,file,file-handling,dev-c++,C,File,File Handling,Dev C++,我正在使用Dev-C++IDE,现在我正在尝试进行文件处理。以下是我的代码: int main(){ FILE *fp; int b = 10; int f; fp = fopen("data.txt", "w"); //checking if the file exist if(fp == NULL){ printf("File does not exist,please check!\n"); }e

我正在使用Dev-C++IDE,现在我正在尝试进行文件处理。以下是我的代码:

int main(){
     FILE *fp;
     int b = 10;
     int f;
      fp = fopen("data.txt", "w");
      //checking if the file exist
      if(fp == NULL){
        printf("File does not exist,please check!\n");
      }else{
      printf("we are connected to the file!\n");
      }
      fprintf (fp, " %d ", b) ;
      fclose(fp);
      printf("Reading from the file \n");

      FILE *fr;
      fr=fopen("data.txt","r");
      fscanf (fr, " %d ", &f) ;
      fclose(fr);
      printf("the data from the file %d \n", f);
    return 0;

}

这段代码在NetBeans中工作,但在Dev-C++中,我只是得到了“我们已连接到该文件”的消息,但它没有将“10”的值放入该文件中。请你知道答案让我知道,我该怎么办?

我看不出你的代码有什么问题,但这里有一些提示

一个好习惯是创建函数并调用它们,而不是让所有函数都内联

#define FILENAME "data.txt"

void writeFile()
{
     FILE *fp;
     int b = 10;
      fp = fopen(FILENAME, "w");
      //checking if the file exist
      if (fp == NULL)
      {
        perror("File could not be opened for writing\n");
      }
      else
      {
        printf("File created\n");
      }
      fprintf (fp, " %d ", b) ;
      fclose(fp);
}

void readFile()
{
     int f;
     printf("Reading from the file \n");

     FILE *fr;
     fr=fopen(FILENAME,"r");
     fscanf (fr, " %d ", &f) ;
     fclose(fr);
     printf("the data from the file %d \n", f);
}

int main()
{
  writeFile();
  readFile();
}
那么,当从文件中读取时,我建议您使用fgets 因为它使用起来更安全,因为fscanf有导致内存覆盖的趋势 如果值是意外的

<- fscanf(fp," %d ", &f );

-> char buf[16]; // some buffer
-> fgets( fp, buf, 10 ); // read as string
-> f = atoi(buf); // convert to int
charbuf[16];//一些缓冲区
->fgets(fp,buf,10);//读作字符串
->f=原子(buf);//转换为整数

它工作得很好。IDE的代码没有问题。请在windows中将代码移入“我的文档”。试试看。。。。我认为这是许可问题。

@user261002;你看到没有内容的文件了吗?或者根本没有文件?你应该做的第一件事就是切换IDE。DevC++已经死了,有更好的替代品(visualstudioexpress、Code::Blocks等),它创建了文件,但没有写入文件。