NAVIGATION - HOME |  | |
Storing data from a text file into an array | | Published by: jack 2009-01-09 |
Persistent data (feb 99):: then to save the data pointed to by $data , open a text file and write the results One of the downside of Data::Dumper is that it cannot store coderefs , http://www.stonehenge.com/merlyn/UnixReview/col24.htmlHOME | 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 Reading a Delimited File using ASP.NET and VB.NET:: Oct 5, 2003 Data is directly read from the delimited text file into arrays and manipulated. now we need a place to store the contents for the file. http://www.theukwebdesigncompany.com/articles/article.php?article=151HOME |
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
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 != ' |
|