473,402 Members | 2,072 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,402 software developers and data experts.

problem with fscanf for reading matrix from text file.

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
1 2826
weaknessforcats
9,208 Expert Mod 8TB
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

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

Similar topics

2
by: Suchi | last post by:
Hi all: I want to read a textfile from an ASP program. My program is like this: Set fso = CreateObject("Scripting.FileSystemObject") workFile=http://localhost/Readme.txt) Set textFile =...
5
by: Lenard Gunda | last post by:
hi! I have the following problem. I need to read data from a TXT file our company receives. I would use StreamReader, and process it line by line using ReadLine, however, the following problem...
1
by: rodchar | last post by:
hey all, is there a quick way to read.all the contents of a text file, search for the text, and if it finds that text to read the entire line? thanks, rodchar
1
by: mart2006 | last post by:
Hi, I'm currently reading a text file via PHP which, in itself, is very easy. However I want to specifically get one word from the text file and assign it as a variable and I'm struggling like...
5
by: Z.K. | last post by:
In C#, using the StreamReader, how do I detect when you get to the end of line. I am reading a text file using the Read() function and I need to detect the \n\r, but everything I try does not...
2
by: bambataa | last post by:
Hellloww...i am new to Java and i am facing a problem, i am reading a text file, i am using string tokenizer class,..now at some point when i read a first token i have to check it if its an integer...
1
by: engggirl3000 | last post by:
Another question I have, what is the difference between reading a text file to a program and opening a text file in the program? A sample of one of the text files is formatted like this: 3 ...
0
by: Sharmi | last post by:
i have a problem in reading a pdf file.... my problem is to read a pdf file line by line and store the necessary fields to the database......i have properly added the reference..... here is my...
2
by: thanawala27 | last post by:
Hi, I'm facign a strange problem in reading a text file. The contents of my text file is: A1;B1;C1;D1 A2;B2;C2;D2 A3;B3;C3;D3
2
by: friend.blah | last post by:
i have a text file lets say in this format abc abs ajfhg agjfh fhs ghg jhgjs fjhg dj djk djghd dkfdf .... .... ...... i want to read the first line at certain time for eg : at 10clk
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.