-2

I am trying to copy 65,536 lines from a file to an int array of the same size using a function.
each line contains four hexadecimal digits.

I also added _CRT_SECURE_NO_WARNINGS in properies => c/c++ => preprocessor definitions because i kept getting warnings because i was using f_gets and not f_gets_s to read from the file.

the error I keep getting now is:

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

when trying to print the array I see that all the lines are copied but the last line is copied twice or maybe copied once but is printed twice.
I don't understand what I'm doing wrong.

Thanks for the help.

#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;
 }
NadavB
  • 11
  • 5
  • 1
    Post a [mcve], this code is incomplete. – vgru May 03 '18 at 07:34
  • 2
    https://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong – Mat May 03 '18 at 07:34
  • Welcome to Stack Overflow! Your code is incomplete; in particular, it seems to be missing a `main()` function and at least one `#include`. Please [edit] your code so it's a [mcve] of your problem (including any necessary inputs, but preferably not needing any), then we can try to reproduce and solve it. You should also read [ask]. – Toby Speight May 03 '18 at 07:46

1 Answers1

1

Your buffer has a length of CHARS_IN_LINE:

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

But upon calling fgets you provide a buffer length of NUMBER_OF_LINES_MEMO:

  fgets(temp, NUMBER_OF_LINES_MEMO, copyCreateFromFile);

You should provide the actual length of the temp buffer to fgets.

  fgets(temp, CHARS_IN_LINE, copyCreateFromFile);

or even better

  fgets(temp, sizeof temp, copyCreateFromFile);

Furthermore the length of the lines in your file is not 4 but 5, because fgets appends a \n at the end of the line. Therefore CHARS_IN_LINE should be at least 5.


Not directly related:

You can drop this line:

FILE *copyCreateFromFile = fPtrReadMemin;

and use directly fPtrReadMemin instead of copyCreateFromFile.


Your test of end of file is not correct, you should rather test if fgets returns NULL.

The strcmp is useless, you can drop it.

Overall corrected and simplified function:

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++;
  }
}

And you forgot to include following:

#include <string.h>
#include <stdlib.h>
Jabberwocky
  • 48,281
  • 17
  • 65
  • 115
  • I tried using the code suggested but now i am getting another error: "Exception thrown: write access violation. meminLines was 0x1D20112." – NadavB May 03 '18 at 09:08
  • @NadavB That probably means that there are other bugs elsewhere in the code you didn't show. You need to provide a __[MCVE]__ – Jabberwocky May 03 '18 at 09:39
  • i added the minimal main to my question. the debugger points out to a problem in the line "meminLines[i] = (int)strtol(temp, NULL, 16)" in the function. I think that the error pops up also at the end of printing of the array. – NadavB May 03 '18 at 11:48
  • @NadavB you didn't apply what I suggested in line 4 of my my answer. You need `fgets(temp, CHARS_IN_LINE, copyCreateFromFile);` – Jabberwocky May 03 '18 at 13:27
  • @NadavB forget my previous comment. I modified the answer according to your edit. Use the __Overall corrected and simplified function__ in my answer and it will work. – Jabberwocky May 03 '18 at 13:35