473,387 Members | 1,501 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,387 software developers and data experts.

fscanf .txt file to 2d matrix

Hi,

I'm new to C (and programming in general), and I have to say that this site has helped me learn a great deal about C (thanks everybody!!). I've looked through several discussions specifically about reading a matrix from a file, but I think (based on my admittedly small amount of knowledge about things like this) that my problem is slightly different. My for-loops for fscanf-ing values from a user-specified file seems to be ignored completely when the program below is run. Ignore void chol() and the declared values that are not used in main() - they refer to chol(), which I've left out of this post. After the filename is scanned, the program goes to the printf statement (printf("Hello");). I've been testing this program on a file test.txt that contains:

1. 2.
2. 1.

Note that, since **a is meant to be a matrix, the number of rows/columns n in this case is 2. Since this program is meant for a Cholesky decomposition, the numbers in the file are assumed to come from an nxn symmetric positive-definite matrix that, in this case, is entered by row (which is not horribly important yet since I can't read the matrix...). The block of comments before the for-loops was me checking to see if my logic in the for-loops was correct. Reading the values individually as in the comments works and gives the desired result when used on test.txt, but the for-loops seem to be skipped entirely. Now-deleted printf statements confirmed that the value of n was correctly 2 right before the for-loops, but the loop behaves as if l=n even though l is initialized at 0. The relevant loop part is in bold.

#include <stdio.h>
#include <limits.h>

main(){
char filename[FILENAME_MAX];
FILE *file;
double **a, inparen;
long n, l, m, r, s, i, j, k;
void Chol(double **, long);

printf("\nHow many columns/rows does your matrix have? ");
scanf("%lf", &n);

a=(double **)malloc(n*sizeof(double *));
*a=(double *)malloc(n*sizeof(double));

printf("\nWhat is the name of the file in which your matrix is stored? ");
scanf("%s", &filename);
file=fopen(filename, "r");
if (file==NULL){
printf("\nYour file does not exist!");
exit(1);
}

/*
fscanf(file, "%lf", &a[0][0]);
printf("%lf", a[0][0]);
fscanf(file, "%lf", &a[0][1]);
printf("%lf", a[0][1]);
*/

for (l=0;l<n;l++){
for (m=0;m<n;m++){
printf("l is %ld", l);
printf("m is %ld", m);
fscanf(file, "%lf", &a[l][m]);
printf("a[%ld][%ld] is %lf.\n", l, m, a[l][m]);
}
}


printf("\nHello");

/*
Chol(a,n);

for (r=0;r<n;r++){
for (s=r;s<n;s++){
printf("u[%li][%li] is %lf", r, s, a[r][s]);
}
}*/
}

Any helpful comments would be much appreciated :-). Thank you!!

- Joanne
Nov 26 '06 #1
2 9325
DeMan
1,806 1GB
I suspect (but I'm not certain) that the wierd behaviour may be caused by the loop counters being initialised to Long Integers. C has some issues with casting, and I suspect in your initialisation l=0 is casting the long back to an int (why that's a problem I'm not exactly sure). if your lower loops have worked at some stage this is not the case, but it may be worth trying to declare l & m as int l,m (or if you only use them as loop counters even 'for(int l=0; l<n; l++)').
Nov 28 '06 #2
DeMan
1,806 1GB
I retract my earlier post....

I think the problem may be that you are reading in %lf (ie a floating point value) when you would rather read an integer %ld.
Nov 28 '06 #3

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

Similar topics

3
by: Benedicte | last post by:
Hi, I'm getting some problems when using fscanf to read a file. This is a piece of the program code: main () { /*** Variable declaration ***/ FILE *vpfile; /*** Data file ***/
4
by: Psibur | last post by:
Hello, trying to get back into c and was having issue with reading a simple text file with an aribtrary # of lines with 3 int's per line, with the eventual purpose of putting each int into an...
5
by: learner | last post by:
I have datafiles like this: 0 1941 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.02 0.00 0.00 1 0 1941 0.00 0.03 0.00 0.03 0.04 0.02 0.00 0.00 0.00 0.00 2 0 1941 0.00 0.00 0.00 0.00 0.52...
1
by: siliconwafer | last post by:
Hi All, here is one code: int main() { FILE*fp; unsigned long a; fp = fopen("my_file.txt","w+"); a = 24; fprintf(fp,"%ld",a); while(fscanf(fp,"%ld",&a) == 1) {
37
by: PeterOut | last post by:
I am using MS Visual C++ 6.0 on Windows XP 5.1 (SP2). I am not sure if this is a C, C++ or MS issue but fscanf has been randomly hanging on me. I make the call hundreds, if not thousands, of...
5
by: a | last post by:
After reading FAQ comp.lang.c section 12 and googling again, still there is no threads talking about reading a series of numbers. The input files, somehow structured, is exemplified below: ...
1
by: dn6326 | last post by:
Hi, basically im trying to read in a file that represents a matrix, where the first line is in the format of <number of rows> <number of collumns> then all lines after that follow the format <row>...
42
by: Bill Cunningham | last post by:
I'm doing something wrong and all I know to do is turn to clc. I have a text file containing 2 doubles separated by a tab. ..26 0 Is the text. I want to read the two double and printf them...
1
by: itmfl | last post by:
I am working in C. I'm trying to copy values from one file to another new file minus the first line. Then I am setting the values in the new file to a matrix. The first file contains: Matrix1 2...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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?
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.