gpt4 book ai didi

c - 使用 strtok 时出现段错误

转载 作者:行者123 更新时间:2023-11-30 18:28:22 24 4
gpt4 key购买 nike

我正在尝试从如下所示的输入中读取每个数字:

179    2358 5197    867 
5541 172 4294 1397
2637 136 3222 591

...并逐行获取每行的最小值/最大值,然后使用 strtok 获取该行中的每个数字,但我在第二个 while 循环中收到段错误错误。

代码:

#include "header.h"
#include <string.h>

int main(void) {
int8_t* line = malloc(sizeof(int8_t) * 75);
int8_t* temp;

int32_t minim, maxim;
int32_t current;

stdin = fopen("stdin", "r");
while (fgets(line, 75 * sizeof(int8_t), stdin)) {
printf("%s", line);
temp = strtok(line," \n\t");
maxim = minim = atoi(temp);

while (temp != NULL) {
current = atoi(temp);
temp = strtok(NULL, " \n\t");

if (current < minim)
minim = current;
if (current > maxim)
maxim = current;

}

printf("\nMin and max: %d %d\n", minim, maxim);
}

printf("\n");
return 0;
}

标题:

#ifndef HEADER_H
# define HEADER_H

# include <stdio.h>
# include <stdlib.h>
# include <stdbool.h>

typedef char int8_t;
typedef signed short int int16_t;
typedef signed int int32_t;
typedef signed long long int int64_t;

typedef unsigned char uint8_t;
typedef unsigned short int uint16_t;
typedef unsigned int uint32_t;
typedef unsigned long long int uint64_t;

#endif

我只是不明白这可能是错误的。谢谢。

最佳答案

好的,所以...

  1. 您的<header.h>我的编译器崩溃了,因为它重新定义了标识符 int8_t等人。由标准保留。 (编译器的提示不太正确,因为未包含相关 header ,但你确实是这样。)如果定义自己的类型,请使用自己的标识符。或者包括<stdint.h>对于“真正的”intX_t 类型(是的,我知道某个“大”编译器没有该 header )。

  2. 最简单的当然是使用char *对于字符串而不是 int8_t * 。 (char 的有符号性是实现定义的,无论如何,一个好的编译器会大声提示不使用 char * 涉及的许多隐式转换。)

  3. 您使用精确宽度类型 int32_t为您minimmaxim ,但忽略使用正确的 printf()宽度说明符( PRId32 ,在 <inttypes.h> 中指定)。由于我看不到对精确宽度类型的任何特定需求,我将它们替换为普通的 int (仅仅 %d 就足够了)。

  4. 完成此操作后,删除提及 sizeof( char ) ,因为这就是定义 1 .

  5. 检查函数的返回值。 fopen()可能会失败。确保没有。

  6. 不要“重复使用”stdin文件描述符。一切都令人困惑。无论您的stdin来自终端、文件或管道取决于您如何调用可执行文件。如果打开文件,请使用 FILE * handle_name ,不是stdin 。除此之外,stdin 是一个打开的文件描述符(即程序的标准输入),因此您必须使用 freopen() ,不是fopen() ,让它从不同的来源正确接收。最简单的事情就是实际使用 stdin按原样(这也为您使用程序提供了最大的灵 active )。

  7. 务必释放您获得的资源。 free()内存。 fclose()文件。许多操作系统可以保护您免受此类疏忽的影响,并在您之后进行清理。有些则不然。

一旦你解决了那些你在评论中打喷嚏的问题......

#include <stdlib.h>                           // <-- added
#include <stdio.h> // <-- added
#include <string.h>

int main() {
char* line = malloc(75); // <-- turned to char *
char* temp; // <-- turned to char *

int minim, maxim; // <-- turned to int
int current; // <-- turned to int

while (fgets(line, 75, stdin)) { // <-- using stdin directly
printf("%s", line);
temp = strtok(line," \n\t");
maxim = minim = atoi(temp);

while (temp != NULL) {
current = atoi(temp);
temp = strtok(NULL, " \n\t");

if (current < minim)
minim = current;
if (current > maxim)
maxim = current;

}

printf("\nMin and max: %d %d\n", minim, maxim);
}

printf("\n");
free( line ); // <-- releasing memory
return 0;
}

...你的程序可以运行:

./testme.exe < file.dat                      # feeding file.dat to stdin
179 2358 5197 867

Min and max: 179 5197
5541 172 4294 1397

Min and max: 172 5541
2637 136 3222 591

Min and max: 136 3222

这里的教训:干净的代码。启用最严格的编译器警告,并注意它们。

至于覆盖stdin结果为fopen()正如您所做的那样,标准规定(强调我的):

229) [...] [stderr, stdin, or stdout] need not be modifiable lvalues to which the value returned by the fopen function may be assigned.

关于c - 使用 strtok 时出现段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47643694/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com