c - 如何编写一个c程序来创建一个文件?

标签 c file structure

我想通过导入用户的文件名来创建文件,请帮忙

int file_name;
printf("Enter ID NUMBER : ");
scanf("%d",&file_name);
FILE *fin;
fin = fopen(&file_name , "w");

最佳答案

这里

FILE *fin;
fin = fopen(&file_name , "w"); /* this is wrong, since &file_name is of int* type */

fopen() 需要 char* 类型的第一个参数,但您提供的 int* 类型是错误的,并且有被编译器正确报告为

error: incompatible pointer types passing 'int *' to parameter of type 'const char *' [-Wincompatible-pointer-types]

如果你可以用像这样的标志编译 -Wall -Wpedantic -Werror。来自 fopen() 的手册页

FILE *fopen(const char *pathname, const char *mode);

file_name 声明为字符数组 并将文件名存储到其中。

char file_name[1024]; /* take a char array to store file name */
/* @TODO : store actual file name into file_name array */
FILE *fin = fopen(file_name , "w");
if(fin == NULL) {
  /* @TODO : error handling */
}

关于c - 如何编写一个c程序来创建一个文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55835510/

相关文章:

c++ - 结构中指针数组的析构函数

c++ - 使用 Win32 执行命令提示符的功能

c - JNA 映射到 C

javascript - 图库图像捕获在某些设备上不返回数据

database - 什么是更快的数据库查询或文件写入/读取

delphi比较文本文件内容

string - R "str"等效于 Perl

c - 结构体指针和内存位置

c - 如何获取用户输入以在 C 中打开文件?

structure - 结构内部的签名