c - 无法写入文件

标签 c arrays file file-io

这是在文件中写入内容的代码。

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
   FILE *fp;
   char ch(90);
   fp = fopen("books.txt","w");
   if(fp == NULL)
   {
        puts("Cannot open file");
   }
   printf("Enter lines of text:");
   while(strlen(gets(ch)) > 0)
   {
       fputs(ch, fp);
   }
   fclose(fp);
}

我收到 4 个错误。这些是:

  1. Cannot convert int to char * in function main().
  2. Type mismatch in parameter __s in call to gets(char *) in function main().
  3. Cannot convert int to const char * in function main().
  4. Type mismatch in parameter __s in call to fputs(const char *,FILE *) in function main().

最佳答案

我相信你对 char 数组的定义是错误的:

char ch(90);

必须是

char ch[90];

关于c - 无法写入文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30343598/

相关文章:

Python:将大矩阵存储到文本文件中供以后使用

c - 我怎样才能再次这样做?在C中

java - 文件未找到异常,相同目录存在

c# - 无法删除由 SharpZipLib (FastZip) 创建的 Zip 文件

c - scanf: "%[^\n]"跳过第二个输入但 "%[^\n]"没有。为什么?

c - 从返回指针的函数读取值

c++ - spidermonkey 1.8.5 在 Debug模式下崩溃

c - 当我在 Eclipse Indigo CDT 中运行代码时输出错误

java - 打印 String[] 产生随机空值,不确定为什么会发生

python - readlines 给了我额外的换行符 python2.6.5