In this program I'm parsing a csv file with fgets
, and based on my knowledge on c prog, it turns the file into an array.
在这个程序中,我使用fget解析csv文件,根据我对c prog的了解,它将文件转换为数组。
So when I print it out with printf("%s",input)
I get this 10,20,30 for example, but when i include printf("%s",input[0])
the program stops working. This is the program i am working on:
所以当我用printf(“%s”,input)打印出来的时候,我得到了这个10,20,30,例如,但是当我包含printf(“%s”,输入[0])时,程序就停止工作了。这是我正在进行的项目:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct xa
{
int x;
int y;
int z;
} xo;
int main()
{
FILE *dudufile;
char filename[]="dodo.dat";
char input[1679];
dudufile=fopen(filename,"r");
while ( fgets(input,1679, dudufile ) != NULL )
{
printf("%s\n",input);
printf("%s\n",input[0]);
struct xa;
xo.y=input[1];
printf("%d",xo.y);
}
return 0;
}
4 个解决方案
#1
5
Compile with warnings:
编译警告:
warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘int’ [-Wformat]
警告:格式' %s '要求参数类型为' char * ',但参数2的类型为' int ' [-Wformat]
Should be:
应该是:
printf("%c\n",input[0]);
#2
4
This here is wrong:
这是错误的:
printf("%s\n",input[0]);
Here you tell printf
to print a string, but give a single character as argument. This leads to undefined behavior and the crash.
这里你告诉printf打印一个字符串,但是给一个字符作为参数。这导致了未定义的行为和崩溃。
If the compiler doesn't already warn about it, you might want to enable more warnings. While warning messages are not errors from the compilers point of view, they often point out problems in your code, problems that often cause undefined behavior.
如果编译器还没有对此发出警告,您可能需要启用更多的警告。虽然从编译器的角度来看,警告消息并不是错误,但是它们经常指出代码中的问题,这些问题常常导致未定义的行为。
#3
2
input[0]
is not a string (what %s
expects), but a char
. If you want to print that character, you need to use the %c
format specifier.
输入[0]不是一个字符串(%s所期望的),而是一个char。如果要打印该字符,需要使用%c格式说明符。
#4
2
Well, input[0]
is not a pointer to a string as %s
wants it to be.
输入[0]不是一个指向字符串的指针,正如%s所希望的那样。
If you want to print out one single character, you should use %c
.
如果想打印出一个字符,应该使用%c。
#1
5
Compile with warnings:
编译警告:
warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘int’ [-Wformat]
警告:格式' %s '要求参数类型为' char * ',但参数2的类型为' int ' [-Wformat]
Should be:
应该是:
printf("%c\n",input[0]);
#2
4
This here is wrong:
这是错误的:
printf("%s\n",input[0]);
Here you tell printf
to print a string, but give a single character as argument. This leads to undefined behavior and the crash.
这里你告诉printf打印一个字符串,但是给一个字符作为参数。这导致了未定义的行为和崩溃。
If the compiler doesn't already warn about it, you might want to enable more warnings. While warning messages are not errors from the compilers point of view, they often point out problems in your code, problems that often cause undefined behavior.
如果编译器还没有对此发出警告,您可能需要启用更多的警告。虽然从编译器的角度来看,警告消息并不是错误,但是它们经常指出代码中的问题,这些问题常常导致未定义的行为。
#3
2
input[0]
is not a string (what %s
expects), but a char
. If you want to print that character, you need to use the %c
format specifier.
输入[0]不是一个字符串(%s所期望的),而是一个char。如果要打印该字符,需要使用%c格式说明符。
#4
2
Well, input[0]
is not a pointer to a string as %s
wants it to be.
输入[0]不是一个指向字符串的指针,正如%s所希望的那样。
If you want to print out one single character, you should use %c
.
如果想打印出一个字符,应该使用%c。