gpt4 book ai didi

c - 如何在VS中使用fgets

转载 作者:行者123 更新时间:2023-11-30 16:24:36 25 4
gpt4 key购买 nike

我一直在尝试在 VS2017 上运行此代码。代码正在编译并运行,但不是以我想要的方式运行。因此,我尝试使用调试器,它显示:

Debug Assertion Failed! Program:

File: minkernel\crts\ucrt\src\appcrt\stdio\fgets.cpp

Line:33

Expression: stream.valid()

从过去的问题中,我了解到这可能是由于文件打开处理不当而发生的,但我认为我的代码确实可以解决这个问题。

任何帮助将不胜感激!

(我的相关代码):

int main(int argc, char *argv[]) {
int i, count_commands, PC_A, lastLine;
int *PC = &PC_A;
FILE *memin;
FILE *memout;
FILE *regout;
FILE *trace;
FILE *count;
assert(argc == 6);
*PC = 0;
count_commands = 0;
//allocationg memory for registers content
char **regs = (char **)(malloc(sizeof(char *) * 16));
for (i = 0; i < 16; i++) {
regs[i] = (char *)(malloc(sizeof(char) * 9));
for (int j = 0; j < 8; j++) {
regs[i][j] = '0';
}
regs[i][8] = '\0';
}
//allocationg memory for the memory image we have
char **memory = (char **)(malloc(sizeof(char *) * 4096));
for (i = 0; i < 4096; i++) {
memory[i] = (char *)(malloc(sizeof(char) * 9));
memory[i][0] = '\0';
}

//load memin image into memory
char *line = (char *)malloc(sizeof(char) * 8);
memin = fopen(argv[1], "r");
if (memin != NULL) {
perror(strerror(errno));
}
int j = 0;
while ((line = fgets(line, 10, (FILE *)memin)) != NULL) {
strcpy(memory[j], line);
memory[j][8] = '\0';
j++;
}

最佳答案

打开文件后,在OP的代码中有这样的检查:

if (memin != NULL) {
perror(strerror(errno));
}

因此,如果打开成功,则会打印错误字符串。在我的实现中,它报告:

Success: Success

No action is taken if it fails to open the file.

When it comes to the actual reading of all the lines in the file, there are some other issues. A buffer (char array) named line of size 8 is dinamically allocated and passed to fgets:

while ((line = fgets(line, 10, (FILE *)memin)) != NULL) {
// ^^

请注意,10 也作为缓冲区的大小传递,这是错误的,因为它允许 fgets 写入已分配数组的范围之外。

此外,鉴于 OP 的编译器是 MSVC 2017,我假设此代码在 Windows 上运行,因此文件中的行很可能以 "\r\n" 序列终止,而不是单个 '\n'。即使 OP 确信每行都是 8 个字符的字符串,fgets 也需要一个至少大小为 8 + 3 (8 + '\r' + '\n ' + '\0') 以安全地读取它们。

考虑如何在此代码段中实现这些建议:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>

#define MEM_SIZE 1024u
#define LINE_SIZE 128u
#define STR_SIZE 8u

int main(int argc, char *argv[])
{
// Try to open the input file
if (argc < 2) {
fprintf(stderr, "Missing file name in command line.\n");
return EXIT_FAILURE;
}
FILE *memin = fopen(argv[1], "r");
if (memin == NULL) {
fprintf(stderr, "Unable to open file [%s].\n", argv[1]);
return EXIT_FAILURE;
}
// I'd use plain arrays to store the lines
char memory[MEM_SIZE][STR_SIZE + 1] = {{'\0'}};
char line[LINE_SIZE] = {'\0'};
size_t count = 0;
while ( count < MEM_SIZE && fgets(line, LINE_SIZE, memin) ) {
size_t length = strcspn(line, "\r\n");
if (length > STR_SIZE) {
fprintf(stdout, "Warning, line too long: %zu.\n", count);
length = STR_SIZE;
}
memcpy(memory[count], line, length);
memory[count][STR_SIZE] = '\0';
++count;
}

for ( size_t i = 0; i < count; ++i ) {
printf("[%s]\n", memory[i]);
}
}

关于c - 如何在VS中使用fgets,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53600838/

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