Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 无法打印字符*字符串_C_String_Printf - Fatal编程技术网

C 无法打印字符*字符串

C 无法打印字符*字符串,c,string,printf,C,String,Printf,我不清楚我的程序出了什么问题,这是一个简单的代码,打开一个文件,从中读取第一行,然后打印出来。但程序一直在崩溃。我的文本文件的实际内容是一句话:测试我的代码 int main(void) { FILE *stream; char *s; stream = fopen("input.txt", "r"); fscanf(stream, " %s", &s); printf("%s", s); fclose(stream); return 0; } 我被指示不要使用s中的库函数是未初

我不清楚我的程序出了什么问题,这是一个简单的代码,打开一个文件,从中读取第一行,然后打印出来。但程序一直在崩溃。我的文本文件的实际内容是一句话:测试我的代码

int main(void)
{
FILE *stream;
char *s;
stream = fopen("input.txt", "r");
fscanf(stream, " %s", &s);

printf("%s", s);


fclose(stream);
return 0;
}

我被指示不要使用

s
中的库函数是未初始化的指针。您需要为要写入的
fscanf
分配一些内存。

s
是未初始化的指针。您需要分配一些内存,以便
fscanf
写入。

代码中缺少的东西很少

// Uninitialise pointer, you need to allocate memory dynamically with malloc before you use it. That is
char *s;
int size = 20;  // size of the string you want
s = malloc(size * sizeof(char));

// Or you can use a VLA and with this you don't have to use free(s)

char s[20];


// fopen could fail, always check the return value before using it.
stream = fopen("input.txt", "r"); 

if(stream == NULL){
  perror("File opening failed");
        return EXIT_FAILURE;
}
fscanf(stream, "%s", s);


//Don't forget to do free(s) to free memory allocated with malloc .  when you are done with it

代码中缺少的东西很少

// Uninitialise pointer, you need to allocate memory dynamically with malloc before you use it. That is
char *s;
int size = 20;  // size of the string you want
s = malloc(size * sizeof(char));

// Or you can use a VLA and with this you don't have to use free(s)

char s[20];


// fopen could fail, always check the return value before using it.
stream = fopen("input.txt", "r"); 

if(stream == NULL){
  perror("File opening failed");
        return EXIT_FAILURE;
}
fscanf(stream, "%s", s);


//Don't forget to do free(s) to free memory allocated with malloc .  when you are done with it
分配保存内存地址所需的字节数(在大多数系统上为32/64位)。 但由于您没有初始化指针,因此其值(指针指向的地址)未定义

因此:fscanf尝试写入未定义的内存地址

我将其初始化为char*s=NULL

是的,指针现在已初始化(耶!),但现在不指向任何地方

因此:fscanf将尝试不写任何东西

解决方案是分配一些fscanf可以使用的内存。 fscanf不会神奇地为您分配内存

您可以使用堆栈内存或动态分配内存(堆)

堆栈内存更易于管理,但比堆小得多

这里有一个使用堆栈上内存的解决方案:

// Allocates 10 bytes on the stack
// Given 1 Character = 1 Byte the
// buffer can hold up to 9 characters.
char myBuffer[10];

// Initialize s with the address of myBuffer
char *s = myBuffer;

// Call fscanf
fscanf(stream, "%9s", s);
您可能想知道为什么我使用
%9s
而不是
%s

原因是为了防止缓冲区溢出:

fscanf不知道缓冲区有多大,所以您需要告诉它

否则,fscanf将在分配的内存之外写入数据

我建议您通读C字符串和内存管理

分配保存内存地址所需的字节数(在大多数系统上为32/64位)。 但由于您没有初始化指针,因此其值(指针指向的地址)未定义

因此:fscanf尝试写入未定义的内存地址

我将其初始化为char*s=NULL

是的,指针现在已初始化(耶!),但现在不指向任何地方

因此:fscanf将尝试不写任何东西

解决方案是分配一些fscanf可以使用的内存。 fscanf不会神奇地为您分配内存

您可以使用堆栈内存或动态分配内存(堆)

堆栈内存更易于管理,但比堆小得多

这里有一个使用堆栈上内存的解决方案:

// Allocates 10 bytes on the stack
// Given 1 Character = 1 Byte the
// buffer can hold up to 9 characters.
char myBuffer[10];

// Initialize s with the address of myBuffer
char *s = myBuffer;

// Call fscanf
fscanf(stream, "%9s", s);
您可能想知道为什么我使用
%9s
而不是
%s

原因是为了防止缓冲区溢出:

fscanf不知道缓冲区有多大,所以您需要告诉它

否则,fscanf将在分配的内存之外写入数据


我建议您通读C字符串和内存管理方面的知识。

1。您不检查
fopen
的结果。2.
s
是一个未初始化的指针-你认为数据去哪里?
char s[16];fgets(s、s的尺寸、溪流)而不是
char*s;。。。fscanf(流、%s、&s)如果您使用的是GCC,请始终使用
GCC-Wall-Werror
char*s;编译。。。fscanf(流、%s、&s)-->
chars[100];fgets(s、s的尺寸、溪流)1。您不检查
fopen
的结果。2.
s
是一个未初始化的指针-你认为数据去哪里?
char s[16];fgets(s、s的尺寸、溪流)而不是
char*s;。。。fscanf(流、%s、&s)如果您使用的是GCC,请始终使用
GCC-Wall-Werror
char*s;编译。。。fscanf(流、%s、&s)-->
chars[100];fgets(s、s的尺寸、溪流)我将其初始化为:char*s=NULL;但该程序仍然崩溃,无法提供任何写入内存。您可以使用堆栈上的内存(
char s[10]
在堆栈上分配10个字符),也可以使用malloc动态内存分配。我将其初始化为:char*s=NULL;但程序仍然崩溃,无法提供任何写入内存。您可以使用堆栈上的内存(
char s[10]
在堆栈上分配10个字符),也可以使用malloc动态内存分配。
fscanf(流,“%9s”,s)无法很好地读取输入行<代码>%s
不保存空白。像
“1 2 3”
这样的行不会像
“1 2 3”
@chux一样被读入
s
。不知道:)
fscanf(流,“%9s”,s)无法很好地读取输入行<代码>%s
不保存空白。像
“1 2 3”
这样的行不会像
“1 2 3”
@chux一样被读入
s
。我不知道:)