#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <conio.h>
#include <iostream>
#include <string>
#include "stdafx.h"
#define MAXNAME 16
void main(int argc, char *argv[])
{
int c, i, j, f;
char lib[MAXNAME] = "lib\0 ";
FILE *in, *out;
char name[MAXNAME];
/* open input file */
i = 0;
while ((name[i] = *argv[1]) != '\0') {
i++;
argv[1]++;
}
if ((in = fopen(&name, "r")) == NULL) {
printf("Can't open file\n");
goto end;
}
/* open output file. Same name as input but with .lib extension */
while (name[i] != '.') i--;
j = 0;
i++;
while (MAXNAME >= (i + j)) {
name[i + j] = lib[j];
j++;
}
if ((out = fopen(&name, "w")) == NULL) {
printf("Can't open file\n");
goto end;
}
我正在尝试用 C 打开文件。我使用的是 Visual Studio 2015。当我编译之前的代码时,出现以下错误。
char (*)[16]
类型的参数与以下参数不兼容 类型const char *
FILE *fopen(const char *,const char *)
: 无法将参数 1 从char (*)[16]
转换为const char *
我从评论中了解到,我试图将一个数量分配给另一个数量的两个数量中的一些数量格式不同,但是我无法修复我的错误。有人可以帮我吗?
最佳答案
您正在尝试传递指向 char name[16] 的指针,但 fopen 需要“const char *”。
您的解决方案应该直接在 fopen 中使用 argv[1]。
fopen(argv[1], "r");
关于c - 类型 "char (*)[16]"的参数与类型 "const char *"的参数不兼容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37280062/