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

Posted on 2006-03-23
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
    LVL 45

    Accepted Solution

    >//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

    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.

    LVL 8

    Expert Comment

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

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


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Suggested Solutions

    An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
    Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
    The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
    Video by: Grant
    The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.

    761 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

    Need Help in Real-Time?

    Connect with top rated Experts

    12 Experts available now in Live!

    Get 1:1 Help Now