home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c
- 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
- From: chans@marsh.cs.curtin.edu.au (Sean Chan)
- Subject: Copying file of int to 2-d array
- Message-ID: <chans.715161329@marsh>
- Sender: news@cujo.curtin.edu.au (News Manager)
- Organization: Curtin University of Technology
- Date: Sun, 30 Aug 1992 07:55:29 GMT
- Lines: 66
-
- Gidday all,
-
- I'm trying to read a file, containing integers into a 2-D array that has
- been dynamically allocated. However, I think I'm doing it wrong. When I
- print out my array to screen, the values differ...yes, I am a beginner
- in C :)
-
- Here my code :-
-
- #include <stdio.h>
- #include <stdlib.h>
-
- main ()
- {
- int **array, counter, r, c;
- FILE *infile;
-
- infile = fopen ("elevatio", "r"); /* open file */
-
- /* allocate space for array */
-
- array = (int **) malloc (98 * sizeof (int *));
- for (counter = 0; counter < 98; ++counter);
- array = (int *) malloc (98 * sizeof (int *));
-
- /* read each integer from file into array */
-
- for (r = 0; r < 98; ++r)
- {
- for (c = 0; c < 98; ++c)
- {
- fscanf (infile, "%d", &array[r][c]);
- }
- }
-
- /* check if values read are correct */
-
- for (r = 0; r < 98; ++r)
- {
- for (c = 0; c < 98; ++c)
- {
- printf ("%d ", array[r][c]);
- }
- }
- fclose (infile);
- }
-
- /* End of code */
-
- I like to add that this code seems to work perfectly fine ALL THE TIME
- on UNIX, but works at random on BORLAND C++ Ver 2.0
-
- Thank you in advance.
-
- Sean
- ***********************************************************************
- * * *
- * Sean Chan * Inside every microchip *
- * Student Number : 892561F * there is a little man *
- * 3rd Yr. Computer Science * running around doing *
- * Curtin University of Technology * everything you have *
- * * asked him to do... *
- * E-Mail : chans@marsh.cs.curtin.edu.au * *
- * pchans@cc.curtin.edu.au ******************************
- * *
- ******************************************
-