Google Answers Logo
View Question
 
Q: Storing data from a text file into an array ( Answered,   0 Comments )
Question  
Subject: Storing data from a text file into an array
Category: Computers > Programming
Asked by: perry1872-ga
List Price: $10.00
Posted: 14 Apr 2006 08:18 PDT
Expires: 14 May 2006 08:18 PDT
Question ID: 718864
Hello there,

We are using a optimization tool called CPLEX and the inputs for this
program is fed from a text file which contains a matrix with rows and
colums (max rows and colums will be 100 X 100).

I know how to call a C program into this optimization tool.

 I want to know how to store this data in array [i][j] from the text
file, the delimiter used is comma ",".

The text file say network.txt looks something like this.

1,0,0,1,0
0,1,1,0,1
0,0,0,1,0
1,1,0,1,0

I need  this to be stored in array [i][j]. The program should strictly
be in C only.

Thanks.
Perry

p.s: Bonus will be given too
Answer  
Subject: Re: Storing data from a text file into an array
Answered By: hammer-ga on 14 Apr 2006 09:15 PDT
 
Perry1872,

This C program will do what you want. You can change the variable
declaration to expand the matrix beyond 100 elements. You cn also
change the filename from network.txt to whatever you need it to be.

NOTE: You may need to unwrap lines wrapped by the answer box.

// ------------- CODE BEGIN -------------

#include <stdio.h>

void	main()
{
int	n[100][100];
int	i;
int	j;
int	a;
int	di;
int	dj;
int	quit;
FILE	*fp;
char	line[1024];
char	*cp;

	i = 0;
	quit = 0;
	fp = fopen("network.txt", "r");
	if(fp != NULL)
	{
		while((fgets(line, 1024, fp)) && (quit == 0))
		{
			j = 0;
			cp = line;
			while((*cp != '\0') && (quit == 0))
			{
				a = atoi(cp);
				if((i < 100) && (j < 100))
				{
					n[i][j] = a;
					j++;
					while((*cp != ',') && (*cp != '\0')) cp++;
					if(*cp == ',') cp++;
				}
				else
				{
					quit = 1;
					fprintf(stderr, "Error: Rows or columns has exceeded 100. Exiting
program.\n");
				}
			}
			i++;
		}
		fclose(fp);
		
		// DEBUG loop to print out array contents
		if(quit == 0)
		{
			for(di = 0; di < i; di++)
			{
				printf("ROW %d : ", di);
				for(dj = 0; dj < j; dj++)
				{
					printf("%d	", n[di][dj]);
				}
				printf("\n");
			}
			
		}
	}
	else
	{
		fprintf(stderr, "Error: Unable to open network.txt.\n");
	}
}

// ------------- CODE END -------------



Good luck with CPLEX!

- Hammer


Search strategy
---------------
None. Wrote a C program per stated requirements.
Comments  
There are no comments at this time.

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy