英文:
C user input sporadic invalid results
问题
在使用C语言编码时,我遇到了用户输入的问题。有时候输入有效,有时候无效。
从终端输出的示例运行结果来看:
dennis@MBP2021 Encryption % make
cc Gronsfeld_Cipher.c /Users/dennis/Documents/C/programs/DJSlibrary/cs50.c -o cipher
dennis@MBP2021 Encryption % ./cipher
Please enter line to be encoded: This, however, is not the time.
You entered: This, however, is not the time. Length: 31
The message is: This, however, is not the time.
The cipher is: EZNH, SCVKOHZ, KF UFP GXF ONWX.
The decipher is: THIS, HOWEVER, IS NOT THE TIME.
dennis@MBP2021 Encryption % ./cipher
Please enter line to be encoded: This, however, is not the time, nor the place to have fun.
You entered: This, however, is not the time, nor the place to have fun. Length: 58
zsh: trace trap ./cipher
dennis@MBP2021 Encryption %
代码片段:
char data[] = "Dummy";
int keys[] = {11, 7, 13, 10, 25, 9, 8, 3};
// Do the stuff
int main(void)
{
// Get input from user
char *string_input = NULL;
size_t string_size = 250;
ssize_t bytes_read;
printf("\nPlease enter line to be encoded: ");
string_input = (char *) malloc(string_size);
bytes_read = getline(&string_input, &string_size, stdin);
if(bytes_read < 2)
{
printf("\n\nNo input was entered. Program terminating.\n");
printf("Error Code: 1\n\n");
free(string_input);
return 1;
}
string_input[bytes_read-1] = 0; // Clear out the new line.
printf("\nYou entered: %s Length: %ld\n", string_input, bytes_read-1);
strcpy(data, string_input);
free(string_input);
// Get sizes
int DataLen = strlen(data);
int ks = sizeof(keys) / sizeof(keys[0]);
printf("\nThe message is: %s", data);
根据我在终端上看到的情况,它在strcpy处崩溃(我猜测)。代码中的这一行 string_input[bytes_read-1] = 0; // Clear out the new line 试图删除字符串输入末尾的换行符。它从未执行到最后一个printf语句。我不明白为什么它可以处理一个较短的行(31个字节),而不能处理58个字节的行。我尝试在VSCode中使用调试功能,但它不允许我输入一行输入。我还需要找出其他原因。另外,我是C编程的新手。提前感谢您的帮助。
英文:
Coding in C, I'm having an issue with user input. Sometimes, the input works and sometimes it doesn't.
Example run output from terminal:
dennis@MBP2021 Encryption % make
cc Gronsfeld_Cipher.c /Users/dennis/Documents/C/programs/DJSlibrary/cs50.c -o cipher
dennis@MBP2021 Encryption % ./cipher
Please enter line to be encoded: This, however, is not the time.
You entered: This, however, is not the time. Length: 31
The message is: This, however, is not the time.
The cipher is: EZNH, SCVKOHZ, KF UFP GXF ONWX.
The decipher is: THIS, HOWEVER, IS NOT THE TIME.
dennis@MBP2021 Encryption % ./cipher
Please enter line to be encoded: This, however, is not the time, nor the place to have fun.
You entered: This, however, is not the time, nor the place to have fun. Length: 58
zsh: trace trap ./cipher
dennis@MBP2021 Encryption %
code snippet:
char data[] = "Dummy";
int keys[] = {11, 7, 13, 10, 25, 9, 8, 3};
// Do the stuff
int main(void)
{
// Get input from user
char *string_input = NULL;
size_t string_size = 250;
ssize_t bytes_read;
printf("\nPlease enter line to be encoded: ");
string_input = (char *) malloc(string_size);
bytes_read = getline(&string_input, &string_size, stdin);
if(bytes_read < 2)
{
printf("\n\nNo input was entered. Program terminating.\n");
printf("Error Code: 1\n\n");
free(string_input);
return 1;
}
string_input[bytes_read-1] = 0; // Clear out the new line.
printf("\nYou entered: %s Length: %ld\n", string_input, bytes_read-1);
strcpy(data, string_input);
free(string_input);
// Get sizes
int DataLen = strlen(data);
int ks = sizeof(keys) / sizeof(keys[0]);
printf("\nThe message is: %s", data);
According to what I see on the terminal, it dies at the strcpy (I guess) The line string_input[bytes_read-1] = 0; // Clear out the new line is trying to remove the line feed at the end of the string input. It never gets to the last printf. I don't understand why it worked with a small line (31 bytes) and not the 58 byte line. I tried to use debug in vscode, but it doesn't allow me to enter an input line. Something else I need to find out. Also, I'm new to C programming. Thanks in advance.
答案1
得分: 1
问题很可能出在这个语句上:
strcpy(data, string_input);
在这里,你将数据复制到一个只能容纳六个字符(包括空字符)的数组中。
当你创建和初始化data
数组时,只为字符串"Dummy"
分配了空间,没有更多的空间。
如果输入的长度超过五个字符,那么就会越界访问data
,导致未定义行为。
为data
数组设置一个特定的大小,足够容纳最大的输入。
或者继续使用string_input
,根本不使用data
。
英文:
The problem is very likely this statement:
strcpy(data, string_input);
Here you copy data into an array that can only hold six characters, null-terminator included.
When you create and initialize the data
array you only make space for the string "Dummy"
, nothing more.
If the input it longer than five characters, then that means you will write out of bounds of data
, and have undefined behavior.
Set a specific size for the data
array, one that is big enough to hold the largest input.
Or keep on using string_input
, and don't use data
at all.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论