home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #19 / NN_1992_19.iso / spool / comp / lang / c / 12959 < prev    next >
Encoding:
Text File  |  1992-08-30  |  2.2 KB  |  77 lines

  1. Newsgroups: comp.lang.c
  2. Path: sparky!uunet!mcsun!Germany.EU.net!math.fu-berlin.de!Sirius.dfn.de!darwin.sura.net!wupost!cs.utexas.edu!uwm.edu!rpi!batcomputer!munnari.oz.au!uniwa!cujo!marsh!chans
  3. From: chans@marsh.cs.curtin.edu.au (Sean Chan)
  4. Subject: Copying file of int to 2-d array
  5. Message-ID: <chans.715161329@marsh>
  6. Sender: news@cujo.curtin.edu.au (News Manager)
  7. Organization: Curtin University of Technology
  8. Date: Sun, 30 Aug 1992 07:55:29 GMT
  9. Lines: 66
  10.  
  11. Gidday all,
  12.  
  13. I'm trying to read a file, containing integers into a 2-D array that has
  14. been dynamically allocated. However, I think I'm doing it wrong. When I
  15. print out my array to screen, the values differ...yes, I am a beginner
  16. in C :)
  17.  
  18. Here my code :-
  19.  
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22.  
  23. main  ()
  24. {
  25.    int **array, counter, r, c;
  26.    FILE *infile;
  27.  
  28.    infile = fopen ("elevatio", "r");       /* open file */
  29.  
  30.    /* allocate space for array */
  31.  
  32.    array = (int **) malloc (98 * sizeof (int *));
  33.    for (counter = 0; counter < 98; ++counter);
  34.        array = (int *) malloc (98 * sizeof (int *));
  35.  
  36.    /* read each integer from file into array */
  37.  
  38.    for (r = 0; r < 98; ++r)
  39.    {
  40.        for (c = 0; c < 98; ++c)
  41.        {
  42.            fscanf (infile, "%d", &array[r][c]);
  43.        }
  44.    }
  45.   
  46.    /* check if values read are correct */
  47.  
  48.    for (r = 0; r < 98; ++r)
  49.    {
  50.        for (c = 0; c < 98; ++c)
  51.        {
  52.            printf ("%d ", array[r][c]);
  53.        }
  54.    }
  55.    fclose (infile);
  56. }
  57.  
  58. /* End of code */
  59.  
  60. I like to add that this code seems to work perfectly fine ALL THE TIME
  61. on UNIX, but works at random on BORLAND C++ Ver 2.0
  62.  
  63. Thank you in advance.
  64.  
  65. Sean
  66. ***********************************************************************
  67. *                             *                            *
  68. * Sean Chan                         *   Inside every microchip   *
  69. * Student Number : 892561F             *   there is a little man    *
  70. * 3rd Yr. Computer Science             *   running around doing     *
  71. * Curtin University of Technology        *   everything you have      *
  72. *                                        *   asked him to do...       *
  73. * E-Mail : chans@marsh.cs.curtin.edu.au     *                            *
  74. *          pchans@cc.curtin.edu.au     ******************************
  75. *                         *
  76. ******************************************
  77.