prompt user for file, scan #s in file to matrix, print matrix

Posted on 2006-03-23
Medium Priority
Last Modified: 2010-04-15
not sure how to scan a file's contents (numbers) into a matrix then print
after reading in file.

//scanning in #s//
   fscanf(filename, "%f", a[i][j]);

//how do i bring the file into the program? do i need a pointer?//
Question by:cooling9999
  • 2
LVL 45

Accepted Solution

sunnycoder earned 500 total points
ID: 16277356
>//how do i bring the file into the program? do i need a pointer?//
Yes ... a FILE * ... use fopen to get a FILE *. fopen returns NULL if it could not open the file.

FILE * fptr =  fopen (filename,  "r");
if (!fptr)
      //error handling here
     //read from file here

LVL 45

Expert Comment

ID: 16277374
About the reading code you have shown here

1. fscanf(filename, "%f", a[i][j]);
a[i][j] should be &a[i][j] ... you need to provide the address

2. make sure your data is floats and there are no field separators since your fscanf is reading only floats. If there are some other characters in the file, you need to handle them too!!
In my opinion it is much better to read in as string and then validate and convert it to desired format.

3. It would be a good idea to check for EOF in the inner loop. That way you would be able to gracefully handle incorrect input files which have lesser amount of data.

4. It is always safe to check return values of all system calls for error.

If you need any further clarifications, feel free to ask.


Expert Comment

by:deepu chandran
ID: 16277411
sunnycoder's answer is currect,
you have to rewrite the fscanf like this,

FILE * fptr =  fopen (filename,  "r");
fscanf(fptr, "%f", a[i][j]);


Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.

622 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question