By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,952 Members | 1,725 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,952 IT Pros & Developers. It's quick & easy.

problem with fscanf for reading matrix from text file.

P: 1
I was trying to read a matrix in .txt file into 2d array by fscanf function but the output was wrong.

int main()
{
int r, c, graph[V][V];
FILE *fp;
fp = fopen("AdjacencyMatrix_of_mat_G_N.txt","r");
for(r=0;r<V;r++) {
for(c=0; c<V; c++) {
if (!fscanf(fp, "%lf", &graph[r][c]))
break;
}

}
fclose(fp);
primMST(graph);

input text file
0 2 0 6 0
2 0 3 8 5
0 3 0 0 7
6 8 0 0 9
0 5 7 9 0
Jul 9 '16 #1
Share this Question
Share on Google+
1 Reply


weaknessforcats
Expert Mod 5K+
P: 9,197
Are those individual records for each row of your array?

If so, you haven't allowed for them.

02060\n


Or is the file layout:

0 2 0 6 0 2 0 3 8 5 0 3 0 0 7 6 8 0 0 9

where the numbers are separated by spaces?

Or a combination of the two?

For now I would forget about the array and see if you can read the file correctly by displaying the numbers read.

Once you can read the file correctly, then you can store the numbers as you like.
Jul 9 '16 #2

Post your reply

Sign in to post your reply or Sign up for a free account.