View Single Post
Old 03-23-2005, 09:58 PM   #1 (permalink)
JaySun
Registered User
 
Join Date: Mar 2005
Posts: 7
JaySun is on a distinguished road
File I/O and arrays

Having some difficulties with concept of moving strings from a file into an array in C.
The txt file my program is reading from looks like this:
Karen Smith 100 100 100 100 100
John Oliver 78 90 64 51
Henry Green 44 55 44 44

The goal is to read the first names and store in array, read last names store in a second array and then read the scores and store in a third array. The first two arrays are to have 50 columns and N rows. I created this code to just handle the first name array (fname) and to print the values of array on the screen so I can ensure that the array is taking in the proper values.

Code:
#include <stdio.h>
#include <stdlib.h>

int main (void)

{
	int N, i, j;
	FILE *ifp, *ofp;
	char **fname, **lname, *grades;
	float **scores;
	
	printf ("How many records do you have?\n");
	scanf  ("%d", &N);
	
	ifp = fopen ("input.txt", "r");
	if (ifp == 0)
		{
			printf ("\nNot able to open file!");
			return 0;
		}
	else
		  for (i = 0; i < N; ++i)
			 for (j = 0; j <50; ++j)
		   	fscanf (ifp, "%s", fname);
		
		
		for (i = 0; i < N; ++i)
			for (j = 0; j < 50; ++j)
			printf ("%s", fname[i][j]);
			printf ("\n");
	
		
	return 0;
}
During runtime I get the following errors that occur after user is prompted to enter number of records.
0 [main] a 3000 handle_exceptions: Exception: STATUS_ACCESS_VIOLATION
6148 [main] a 3000 stackdump: Dumping stack trace to a.exe.stackdump

Very confused on whole topic of scanning characters from a file into an array. If anyone can comment on code or provide link for online info in general I would apreciate it.

Last edited by JaySun; 03-23-2005 at 09:59 PM. Reason: Left out language
JaySun is offline   Reply With Quote