C 读取文件并将其复制到阵列:运行时检查失败#2
我正在尝试使用函数将65536行从文件复制到相同大小的int数组。C 读取文件并将其复制到阵列:运行时检查失败#2,c,arrays,file,C,Arrays,File,我正在尝试使用函数将65536行从文件复制到相同大小的int数组。 每行包含四个十六进制数字 我还在properies=>c/c++=>预处理器定义中添加了_CRT\u SECURE\u NO_警告,因为我一直收到警告,因为我使用f_gets而不是f_gets来读取文件 我现在经常遇到的错误是: 运行时检查失败#2-变量“temp”周围的堆栈被删除 已损坏。 当尝试打印阵列时,我看到所有行都被复制,但最后一行被复制了两次,或者可能复制了一次,但被打印了两次。 我不明白我做错了什么 谢谢你的帮助
每行包含四个十六进制数字 我还在properies=>c/c++=>预处理器定义中添加了_CRT\u SECURE\u NO_警告,因为我一直收到警告,因为我使用f_gets而不是f_gets来读取文件 我现在经常遇到的错误是: 运行时检查失败#2-变量“temp”周围的堆栈被删除 已损坏。
当尝试打印阵列时,我看到所有行都被复制,但最后一行被复制了两次,或者可能复制了一次,但被打印了两次。
我不明白我做错了什么 谢谢你的帮助
#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;
}
#包括
#定义行数备忘录65536
#定义寄存器16的数量
#在第5行中定义字符
#定义即时5中的字符
#定义\u CRT\u安全\u无\u警告
void createFromFile(文件*fPtrReadMemin,int*meminLines){
//创建一个名为meminLines的新整数数组,其中包含文本文件中的memin行
//获取文件memin和数组meminline的指针
FILE*copyCreateFromFile=fPtrReadMemin;
int i=0;
char temp[CHARS_IN_LINE];//用于复制到内存阵列
int temp2;
而(!feof(copyCreateFromFile))
{
fgets(临时、行数、备忘录、copyCreateFromFile);
如果(strcmp(temp,“”==0)
{
打破
}
temp2=(int)strtol(temp,NULL,16);
meminLines[i]=temp2;
printf(“%04x\n”,temp2);
i++;
}
}
int main(int argc,char*argv[])
{
文件*fPtrReadMemin;
fPtrReadMemin=fopen(argv[1],“r”);//打开Memin进行读取
int meminLines[行数_MEMO];//内存
如果(fPtrReadMemin==NULL){//检查文件是否正确打开
printf(“使用文件时出错\n”);
出口(1);
}
createFromFile(fPtrReadMemin,meminLines);//创建内存
系统(“暂停”);
fclose(fPtrReadMemin);//关闭所有文件
返回0;
}
您的缓冲区的长度为字符行
:
char temp[CHARS_IN_LINE]; //used for copying to the memory array
但是在调用fgets
时,您提供了一个缓冲区长度行数\u 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
。因此,行中的字符数应至少为5
不直接相关:
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>
您可以删除此行:
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>
而您忘了包括以下内容:
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>
#包括
#包括
Post a,此代码不完整。欢迎使用堆栈溢出!您的代码不完整;特别是,它似乎缺少一个main()
函数和至少一个#include
。请输入您的代码,使其成为您问题的一部分(包括任何必要的输入,但最好不需要任何输入),然后我们可以尝试复制并解决它。您还应该阅读。我尝试使用建议的代码,但现在我遇到另一个错误:“引发异常:写入访问冲突。meminLines是0x1D20112。”@NadavB这可能意味着代码中其他地方还有其他错误,您没有显示。你需要提供一个答案,我在我的问题中添加了最小的main。调试器指出函数中“meminLines[i]=(int)strtol(temp,NULL,16)”行中存在问题。我认为在数组打印结束时也会弹出错误。@NadavB您没有应用我在回答的第4行中建议的内容。您需要fgets(临时、字符行、copyCreateFromFile)
@NadavB忘记我之前的评论。我根据你的编辑修改了答案。在我的答案中使用整体更正和简化功能,它将起作用。