C - 将用户输入存储在动态char数组中?

时间:2023-01-11 02:10:16

I want to read some string input from the user and write it to a file. Right now I'm doing

我想从用户读取一些字符串输入并将其写入文件。现在我正在做

char name[25];
scanf("%s", name);
int handle = open("./visitors.txt", O_RDWR|O_CREAT, S_IRUSR|S_IWUSR);
if (handle < 0){
   printf("File error.\n");
   return;
}
lseek(handle, -sizeof(name), SEEK_END);
write(handle, name, sizeof(name));

This, of course is not right, since most of the time the user doesn't write 25 characters, only less, so when the user inputs 5 characters, the other 20 will be empty, and I end up having 5 chars the user put in and 20 chars gibberish in my output file. How can I make sure only the user input is being written to the file?

这当然是不对的,因为大多数时候用户不写25个字符,只有更少,所以当用户输入5个字符时,其他20个将是空的,我最终有5个字符,用户放在我的输出文件中和20个字符乱码。如何确保只将用户输入写入文件?

2 个解决方案

#1


1  

You are using raw system calls to handle the file operations. Unless your intention is specifically to learn about system calls, you should not do that. The write call writes a number of bytes specified by the third parameter. sizeof(name) is wrong in this case. It will not return the length of the string, it will return the length of the entire buffer.

您正在使用原始系统调用来处理文件操作。除非您的目的是专门了解系统调用,否则您不应该这样做。写调用写入第三个参数指定的字节数。在这种情况下,sizeof(name)是错误的。它不会返回字符串的长度,它将返回整个缓冲区的长度。

The standard strlen function gives you the string length. So running strlen(name) will give you 5 if the user enters 5 characters, unlike the sizeof you are using.

标准的strlen函数为您提供字符串长度。因此,如果用户输入5个字符,则运行strlen(name)将为您提供5个字符,这与您使用的sizeof不同。

Unless you want to learn about system calls specifically, you should probably handle file operations with the C standard library, using fprintf to output to file, and functions such as fopen and fclose. Take a look at the fprintf docs that also link to the other functions.

除非您想要专门了解系统调用,否则您应该使用C标准库处理文件操作,使用fprintf输出到文件,以及fopen和fclose等函数。看一下同样链接到其他功能的fprintf文档。

#2


1  

I'm not sure the reason you want to use low level system calls open, lseek, write. Usually it's a lot more convenient to use standard C fopen, fscanf, fprintf functions for such tasks.

我不确定你想要使用低级系统调用open,lseek,write的原因。通常,使用标准C fopen,fscanf,fprintf函数来执行此类任务会更方便。

In this case, you may try fgets to get string input from stdin, then use fprintf with %s to write to file. You can still use buffer name[25] or name[100] (envision for largest input from stdin). Just initialise it properly to make sure it's NULL terminated. Then fprintf with %s would write the string properly (ie without gibberish ending) to file.

在这种情况下,您可以尝试fgets从stdin获取字符串输入,然后使用fprintf和%s写入文件。您仍然可以使用缓冲区名称[25]或名称[100](设想来自stdin的最大输入)。只需正确初始化它以确保它的NULL终止。然后fprintf与%s将正确写入字符串(即没有乱码结束)到文件。

#1


1  

You are using raw system calls to handle the file operations. Unless your intention is specifically to learn about system calls, you should not do that. The write call writes a number of bytes specified by the third parameter. sizeof(name) is wrong in this case. It will not return the length of the string, it will return the length of the entire buffer.

您正在使用原始系统调用来处理文件操作。除非您的目的是专门了解系统调用,否则您不应该这样做。写调用写入第三个参数指定的字节数。在这种情况下,sizeof(name)是错误的。它不会返回字符串的长度,它将返回整个缓冲区的长度。

The standard strlen function gives you the string length. So running strlen(name) will give you 5 if the user enters 5 characters, unlike the sizeof you are using.

标准的strlen函数为您提供字符串长度。因此,如果用户输入5个字符,则运行strlen(name)将为您提供5个字符,这与您使用的sizeof不同。

Unless you want to learn about system calls specifically, you should probably handle file operations with the C standard library, using fprintf to output to file, and functions such as fopen and fclose. Take a look at the fprintf docs that also link to the other functions.

除非您想要专门了解系统调用,否则您应该使用C标准库处理文件操作,使用fprintf输出到文件,以及fopen和fclose等函数。看一下同样链接到其他功能的fprintf文档。

#2


1  

I'm not sure the reason you want to use low level system calls open, lseek, write. Usually it's a lot more convenient to use standard C fopen, fscanf, fprintf functions for such tasks.

我不确定你想要使用低级系统调用open,lseek,write的原因。通常,使用标准C fopen,fscanf,fprintf函数来执行此类任务会更方便。

In this case, you may try fgets to get string input from stdin, then use fprintf with %s to write to file. You can still use buffer name[25] or name[100] (envision for largest input from stdin). Just initialise it properly to make sure it's NULL terminated. Then fprintf with %s would write the string properly (ie without gibberish ending) to file.

在这种情况下,您可以尝试fgets从stdin获取字符串输入,然后使用fprintf和%s写入文件。您仍然可以使用缓冲区名称[25]或名称[100](设想来自stdin的最大输入)。只需正确初始化它以确保它的NULL终止。然后fprintf与%s将正确写入字符串(即没有乱码结束)到文件。