Thread: Problem with file and array

  1. #1
    Registered User paok's Avatar
    Join Date
    Nov 2007
    Posts
    8

    Problem with file and array

    Hi! I've a problem with my code, please help me to undestand what is going on...
    I open a file and I save all it's characters in an array, which I call "instruction_name". Then I close the file. Finally, I open a new file and I save all it's characters in an another array (register_name), and after this I close the file, as before. The problem is that after the open/read/close of the second file, the first two elements of the "instruction_name" array are being missed! For example let's say that the "instruction_name" array is like this: "NOPADDINC ...". After the second file the "instruction_name" array is "ασPADDINC ...". What is going on?

    My code is like this
    Code:
         FILE *readinstructions;
         printf("Loading the file which contains the instructions... ");
         if ((readinstructions=fopen("instructions.txt","r"))==NULL){
            printf("\nError: Cannot open the instructions.txt");
            }
         else printf("successful");
         
         i=0;
         while ((character=fgetc(readinstructions))!=EOF){
               instruction_name[i]=character;
               i++;
               }
         
         fclose(readinstructions);
         
    
         FILE *readregisters;
         printf("\nLoading the file which contains the registers...");
         if ((readregisters=fopen("registers.txt","r"))==NULL){
            printf("\nError: Cannot open the registers.txt");
            }
         else printf(" successful");
         
         i=0;
         while ((character=fgetc(readregisters))!=EOF){
               register_name[i]=character;
               i++;
               }
         
         fclose(readregisters);
    PS: I use DEV C++

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    1. when you failed to open file - you just print message and continue to use null pointer
    2. where is declaration of register_name, instruction_name, character?
    3. don't you need to prevent memory overrun?
    4. don't you need to nul-terminate the string read from file?
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Registered User paok's Avatar
    Join Date
    Nov 2007
    Posts
    8
    1. When fail happens the program is not running. In my original code, I've a "check" boolean variable which turns to FALSE when the program fails to open the file. I thought that it wasn't needed.
    2.
    Code:
     
    char instruction_name[36],register_name[24];
    int character;
    I can't understand why the elements of the array change. They are saved in the array, aren't they?

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    The easiest and most likely way it's happening, is that you're running outside the array boundaries, (in one or both arrays), and simply over-writing the array's contents.

    Print the first array, like this:

    Index Number===>Value
    ==================
    0. First Array element value
    1. Second array element value

    etc.

    See if your index's are what they should be, and your values are what you think they are.

    Then do the same thing with your second array. I know you'll be surprised. The wrong values, and it's index number, should give you a huge clue about what's going wrong with your program.

  5. #5
    Registered User paok's Avatar
    Join Date
    Nov 2007
    Posts
    8
    I see that the address of the instruction_name starts at 2358912 and the address of the register_name ends at 2358911

  6. #6
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    If you write into the instruction_name more than the 36 chars - you will overwrite the register_name...
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File transfer- the file sometimes not full transferred
    By shu_fei86 in forum C# Programming
    Replies: 13
    Last Post: 03-13-2009, 12:44 PM
  2. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  3. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM
  4. what does this mean to you?
    By pkananen in forum C++ Programming
    Replies: 8
    Last Post: 02-04-2002, 03:58 PM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM