如何遍历字符串的每个字符?

时间:2022-11-29 14:27:53

What I am trying to do , is take a name as an output file and then a string from whic I want to fprintf each character one at a time into the output file.

我要做的是,取一个名称作为输出文件,然后从whic中获取一个字符串,我想要将每个字符一个一个地输出到输出文件中。

#include <stdio.h>

int main(void){
char* outputName;
char* input;
printf("Type in the name of the output file: ");
scanf("%s",outputName);
FILE *outputFile = fopen(outputName , "w");
printf("type something: ");
scanf("%s", input);
int i;
for( i = 0; i < sizeof(input)/sizeof(input[0]) ; i++ ){
     fprintf(outputFile,"%c" ,input[i]);
   }
   fclose(outputFile);
   return 0;
}

When I run it asks for the output file name and then it crashes. What am I doing wrong?

当我运行它时,它要求输出文件名,然后它崩溃。我做错了什么?

By the way I am completely new to C . Any help appreciated.

顺便说一下,我对C完全陌生。任何帮助表示赞赏。

2 个解决方案

#1


2  

sizeof(input)/sizeof(input[0]) won't work here because input is a pointer, so sizeof(input) will just give the size of a pointer, not the size of the array being pointed to.

由于输入是一个指针,所以sizeof(输入[0])不会在这里工作,所以sizeof(input)只会给出一个指针的大小,而不是指向的数组的大小。

Use strlen, which calculates the numbers of characters until a null terminator:

使用strlen,它计算字符数,直到一个空终止符:

for( i = 0; i < strlen(input) ; i++ ){

Btw you also forgot to allocate memory to input and outputName. To fix it:

顺便说一下,您也忘记了将内存分配给输入和输出名称。修复:

char outputName[n];
char input[n];

where n is some magic number denoting the number of characters you can store in the C-string including the terminating null character.

其中n是一个神奇数字,表示可以存储在c字符串中的字符数,包括终止null字符。

Note that this approach (using arrays instead of pointers) makes sizeof(input)/sizeof(input[0]) work as you intended.

注意,这个方法(使用数组而不是指针)使sizeof(输入)/sizeof(输入[0])按照您的意愿工作。

#2


1  

char* outputName and char* input are just pointers, that do not point to any valid memory.

char* outputName和char* input只是指针,它不指向任何有效内存。

Instead you should have a character array where you can store your characters.

相反,你应该有一个字符数组,在那里你可以存储你的字符。

example:

例子:

char outputName[100];
char input[100];

#1


2  

sizeof(input)/sizeof(input[0]) won't work here because input is a pointer, so sizeof(input) will just give the size of a pointer, not the size of the array being pointed to.

由于输入是一个指针,所以sizeof(输入[0])不会在这里工作,所以sizeof(input)只会给出一个指针的大小,而不是指向的数组的大小。

Use strlen, which calculates the numbers of characters until a null terminator:

使用strlen,它计算字符数,直到一个空终止符:

for( i = 0; i < strlen(input) ; i++ ){

Btw you also forgot to allocate memory to input and outputName. To fix it:

顺便说一下,您也忘记了将内存分配给输入和输出名称。修复:

char outputName[n];
char input[n];

where n is some magic number denoting the number of characters you can store in the C-string including the terminating null character.

其中n是一个神奇数字,表示可以存储在c字符串中的字符数,包括终止null字符。

Note that this approach (using arrays instead of pointers) makes sizeof(input)/sizeof(input[0]) work as you intended.

注意,这个方法(使用数组而不是指针)使sizeof(输入)/sizeof(输入[0])按照您的意愿工作。

#2


1  

char* outputName and char* input are just pointers, that do not point to any valid memory.

char* outputName和char* input只是指针,它不指向任何有效内存。

Instead you should have a character array where you can store your characters.

相反,你应该有一个字符数组,在那里你可以存储你的字符。

example:

例子:

char outputName[100];
char input[100];