#include <stdio.h>
#include <iostream>
using namespace std;
int main(){
char *a[20];
FILE * fin = fopen("testtest.txt","r");
int i;
fscanf(fin,"%s",a);
for(i=0;i<20;i++)
{
printf("%c\n",a[i]);
}
system("pause");
}
Run Code Online (Sandbox Code Playgroud)
在这个程序中,我想打印数组中的每个元素,它应该是ABCDE但实际上它打印:
似乎每个元素都已经过,我应该如何正确打印?
A
E
?
?
?
?
?
·
?
8
?
p
?
?
x
3
?
Run Code Online (Sandbox Code Playgroud)
类型a是一个数组char*,而不是一个数组char.改成:
char a[20];
Run Code Online (Sandbox Code Playgroud)
建议在最高警告级别进行编译,并将警告视为错误.例如:
$ gcc -Wall -Werror -pedantic main.c main.c: In function ‘main’: main.c:9:5: error: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘char **’ [-Werror=format] main.c:10:5: error: format ‘%c’ expects argument of type ‘int’, but argument 2 has type ‘char *’ [-Werror=format] cc1: all warnings being treated as errors
在尝试使用变量之前,检查结果fopen()并fscanf()确保文件已打开并读入数据a.
| 归档时间: |
|
| 查看次数: |
447 次 |
| 最近记录: |