HAVE A PROBLEM!! 2D arrays reading from file function

This is a discussion on HAVE A PROBLEM!! 2D arrays reading from file function within the C Programming forums, part of the General Programming Boards category; Hey everyone, here is my code, I'm trying to read a .txt file into a 2D array...can anyone tell me ...

  1. #1
    Registered User
    Join Date
    Jan 2013
    Location
    San Jose, CA
    Posts
    53

    Exclamation HAVE A PROBLEM!! 2D arrays reading from file function

    Hey everyone,

    here is my code, I'm trying to read a .txt file into a 2D array...can anyone tell me what's wrong with my code? Thanks!!!!

    Code:
    #include <stdio.h>
    
    #define INPUT_FILE_GET_NAMES "names.txt"
    #define INPUT_FILE_GET_SALES "sales.txt"
    #define MAX_ROWS_NAMES 25
    #define MAX_COLS_NAMES 20
    #define MAX_ROWS_SALES 25
    #define MAX_COLS_SALES 6
    
    //Function Declarations
    char getNames(char namesArray[][MAX_COLS_NAMES], char filename[]);
    
    
    
    int main()
    
    {
    
    //Local Declarations
    
       char namesArray[MAX_ROWS_NAMES][MAX_COLS_NAMES] = {0};
       int i = 0;
       int j = 0;
       int row = 25;
       int col = 20;
       getNames (namesArray[]);
    
       return 0;
    }
    
    char getNames(char namesArray[][MAX_COLS_NAMES])
    {
        //Statements
    FILE *fp = fopen("names.txt", "r");
    int i, j;
    int col=20;
    int row=25;
    if ( fp )
       {
          for ( ;; )
          {
             int c = getc(fp);
             if ( c == EOF )
             {
                break;
             }
             if ( c != '\n' && c != '\r' )
             {
                namesArray[i][j] = c;
                if ( ++j >= col )
                {
                   j = 0;
                   if ( ++i >= row )
                   {
                      break;
                   }
                }
             }
          }
          fclose(fp);
       }
    
    
    
       for ( i = 0; i < row; i++ )
       {
          for ( j = 0; j < col; j++ )
          {
             putchar(namesArray[i][j]);
          }
          putchar('\n');
       }
    
    
    
    
    
    }

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,566
    What's wrong with your other thread? It looks like the same exact problem. One problem, one thread.

    A brief summary of problems or potential problems I see:
    1. You are using uninitialized variables (e.g. i and j in your getNames function)
    2. You have no bounds checking, so you will overrun the boundaries of namesArray
    3. You check if names.txt opens successfully before reading from it, which is good, but if it doesn't open, there is no error message, and you try to print the contents of namesArray, which is full of garbage data, because you never read anything into it.

    I wont respond to this thread anymore, please use your old one.

    Mods, please close this as a duplicate.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. reading file and storing to arrays
    By dayknight in forum C Programming
    Replies: 4
    Last Post: 04-27-2006, 05:17 AM
  2. reading from file to arrays
    By supernormal2 in forum C Programming
    Replies: 1
    Last Post: 12-08-2002, 02:58 PM
  3. Reading 2d arrays from file?
    By Ringhawk in forum C++ Programming
    Replies: 3
    Last Post: 01-17-2002, 08:05 PM
  4. Reading from a .txt file or use arrays.
    By []--JOEMAN--[] in forum C++ Programming
    Replies: 3
    Last Post: 12-07-2001, 06:55 AM
  5. File reading and arrays
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 10-25-2001, 01:54 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21