c - 从文件读取并将其复制到数组 : Run-Time Check Failure #2

标签 c arrays file

我正在尝试使用函数将 65,536 行从文件复制到相同大小的 int 数组。
每行包含四个十六进制数字。

我还在属性 => c/c++ => 预处理器定义中添加了 _CRT_SECURE_NO_WARNINGS,因为我不断收到警告,因为我使用 f_gets 而不是 f_gets_s 从文件中读取。

我现在不断收到的错误是:

Run-Time Check Failure #2 - Stack around the variable 'temp' was corrupted.

当尝试打印数组时,我看到所有行都被复制,但最后一行被复制两次,或者可能复制一次但被打印两次。
我不明白我做错了什么。

感谢您的帮助。

#include <stdio.h>
#define NUMBER_OF_LINES_MEMO 65536
#define NUMBER_OF_REGISTERS 16
#define CHARS_IN_LINE 5  
#define CHARS_IN_IMMEDIATE 5 
#define _CRT_SECURE_NO_WARNINGS

void createFromFile(FILE *fPtrReadMemin, int *meminLines){
  //create a new array of int numbers named meminLines, with the lines of memin text file
  //gets pointers for the file memin and for the array meminLines
    FILE *copyCreateFromFile = fPtrReadMemin;
    int i = 0;
    char temp[CHARS_IN_LINE]; //used for copying to the memory array
    int temp2;

    while (!feof(copyCreateFromFile))
    {
      fgets(temp, NUMBER_OF_LINES_MEMO, copyCreateFromFile);
      if (strcmp(temp, "")==0)
      {
            break;
      }
      temp2 = (int)strtol(temp, NULL, 16);
      meminLines[i] = temp2;
      printf("%04x\n", temp2);
      i++;
   }
}

int main(int argc, char* argv[]) 
{
    FILE*fPtrReadMemin;
    fPtrReadMemin = fopen(argv[1], "r"); //open Memin to read
    int meminLines[NUMBER_OF_LINES_MEMO]; // the memory  
    if (fPtrReadMemin == NULL) { //check if the files were open correctly
        printf("There was error using files\n");
        exit(1);
    }
    createFromFile(fPtrReadMemin, meminLines); //create the memory
    system("pause");
    fclose(fPtrReadMemin);//close all files
    return 0;
 }

最佳答案

您的缓冲区长度为CHARS_IN_LINE:

char temp[CHARS_IN_LINE]; //used for copying to the memory array

但是在调用fgets时,您提供了NUMBER_OF_LINES_MEMO的缓冲区长度:

  fgets(temp, NUMBER_OF_LINES_MEMO, copyCreateFromFile);

您应该向 fgets 提供 temp 缓冲区的实际长度。

  fgets(temp, CHARS_IN_LINE, copyCreateFromFile);

甚至更好

  fgets(temp, sizeof temp, copyCreateFromFile);
<小时/>

此外,文件中行的长度不是 4,而是 5,因为 fgets 在行末尾附加了 \n。因此 CHARS_IN_LINE 应至少为 5。

<小时/>

不直接相关:

您可以删除此行:

FILE *copyCreateFromFile = fPtrReadMemin;

并直接使用fPtrReadMemin而不是copyCreateFromFile

<小时/>

您对文件结尾的测试不正确,您应该测试 fgets 是否返回 NULL

strcmp没用,你可以放弃它。

整体修正和简化的功能:

void createFromFile(FILE *fPtrReadMemin, int *meminLines) {
  //create a new array of int numbers named meminLines, with the lines of memin text file
  //gets pointers for the file memin and for the array meminLines
  int i = 0;
  char temp[100]; // not using CHARS_IN_LINE but 100 which is a reasonable
                  // maximal file length.

  while (fgets(temp, sizeof temp, fPtrReadMemin) != NULL)
  {  
    meminLines[i] = (int)strtol(temp, NULL, 16);
    printf("%04x\n", meminLines[i]);
    i++;
  }
}
<小时/>

您忘记添加以下内容:

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

关于c - 从文件读取并将其复制到数组 : Run-Time Check Failure #2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50149288/

相关文章:

c - 如何输入和比较字符串

c++ - 使用模板实现排序类

java - 将文本文件标签解析为 xml - Java

c++ - 使用 lex 和 yacc 构建 C++ 配置文件解析器

c - Makefile 拼图 : Multiple Programming Languages

c - 如何在C++Builder项目的Delphi单元中使用Crtl? (或链接到 C++Builder C 运行时库)

c - 将 float 组传递给 (void*) 函数参数

arrays - 如何在不同数据上使用 NSPredicate 和 NSPredicateEditor(多个谓词?)

如果文件存在,调用 open() 创建文件失败

Javascript从本地文件写入本地文件