scanning file and printing an array

This is a discussion on scanning file and printing an array within the C Programming forums, part of the General Programming Boards category; hi, i am a new member here and first semester as a student using programming for my major (Geomatics Engineering). ...

  1. #1
    Registered User
    Join Date
    Nov 2012
    Posts
    2

    scanning file and printing an array

    hi, i am a new member here and first semester as a student using programming for my major (Geomatics Engineering). any feedback would be immensely appreciated. here it goes:
    (also, pardon my probable terrible formatting, first post).

    i am to write a program that reads a file that reads 3 arrays (essentially a 3x3 matrix) and i will end up printing them into a new file along with computing the averages of the values (column 1 avg,...)

    however i am stuck far from that.

    Code:
    /*
    * Programmer:        Jeff Clay
    * Class:            GME 61
    * Date:            8-7-12
    * Program:            READING IN ARRAYS
    *
    * Purpose:            READ IN ARRAYS, COMPUTE AVERAGE
    *
    */
    
    
    #include <stdio.h>
    #include <stdlib.h>
    #define maxRow 2
    #define maxCol 3
    #define _CRT_SECURE_NO_WARNINGS
    
    
    
    
    void printMatrix(double array[maxRow][maxCol], int row, int column);
    void add_matrices(double xArray[maxRow][maxCol], double yArray[maxRow][maxCol], double zArray[maxRow][maxCol], int row, int column);
    
    
    main()
    {
        double xArray[1][3], yArray[1][3], zArray[1][3];
        char fileName[256];
        int i,j;
        FILE *fileIn, *fileOut;
    
    
        i=0;
        j=0;
    
    
    
    
        printf("Enter input file path:\n");
        gets(fileName);
        fileIn=fopen(fileName, "r");
    
    
        if (fileName==NULL)
        {
            printf("Filepath DNE");
            exit(-1);
        }
    
    
        /*printf("Enter output file path:\n");
        gets(fileName);
        fileOut=fopen(fileName, "w");
    
    
        if (fileName==NULL)
        {
            printf("Filepath DNE");
            exit(-1);
        }*/
    
    
        while(fscanf_s(fileIn,"%lf", &xArray[3][1])!=EOF)
            /*add_matrices(xArray,yArray,zArray,3,1);*/
        {
            printMatrix(xArray,3,1);
            /*printf("+ \n");*/
        }
    
    
    
    
    
    
        /*double a[2][3]={{1.0,  2.0,  3.5},{4.0,  5.0,  6.0}};
        double b[2][3]={{-6.0,  -5.0,  -4.0},{  -3.0,  -2.1,  -1.5}};
        double c[2][3];
    
    
        add_matrices(a,b,c,3,2);
        {
            printMatrix(a,3,2);
            printf("+ \n");
            printMatrix(b,3,2);
            printf("= \n");
            printMatrix(c,3,2);
        }*/
    }
    void printMatrix(double array[maxRow][maxCol], int row, int column)
    {
        int i, j;
        for (i=0; i<column; i++)
            {
            for (j=0; j<row; j++)
                printf("%.2lf\t", array[i][j]);
                printf("\n");
            }
    }

    i'll be kind of honest, i am pretty lost on what i am doing and what to do. i understand the concept of an array, however the coding is not friendly to me.

    thank you
    Last edited by Jeff Clay; 11-25-2012 at 09:50 PM.

  2. #2
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,303
    Hey there - Welcome to the forum

    A few things...

    "main" should be declared as int and returning a value - If you want to use argv/argc, you will need to put them into main's parameters.


    Never use gets(). It's a dangerous function which is not even in the new C standard.


    You should be checking to see if fileIn is NULL, not fileName.


    I'll stop there
    Fact - Beethoven wrote his first symphony in C

  3. #3
    Registered User
    Join Date
    Nov 2012
    Posts
    2
    thank you for replying, however: our teacher has only taught us main(), gets(). and my mistake on the NULL, however that does not help my situation. we have used main() on over 10 programs thus far with no issues, along with gets(). i dont think it is imperative that i need to use argv/argc (whatever that is, has not been explained to us). i am using visual express if that makes any difference.

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,334
    > any feedback would be immensely appreciated.
    ...

    > thank you for replying, however: ......

    Sorry, but that just didn't sound like "immensely appreciated".

    > i'll be kind of honest, i am pretty lost on what i am doing and what to do.
    Maybe it's because your teacher doesn't know enough about C. The gets() thing is a real red flag marking out poor teachers.

    > using programming for my major (Geomatics Engineering)
    So is programming something you really want to learn, or just a hurdle to jump over, only to be forgotten as soon as you've got your passing grade for this module?

    > i am to write a program that reads a file that reads 3 arrays (essentially a 3x3 matrix)
    #define maxRow 2
    #define maxCol 3
    Why is maxRow not 3?

    > double xArray[1][3], yArray[1][3], zArray[1][3];
    Why are these not using your #define names?

    > while(fscanf_s(fileIn,"%lf", &xArray[3][1])!=EOF)
    This does NOT fill the whole array with numbers.
    You need a loop to assign each array[row][col] individually.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help scanning input file into queue array
    By krazzyjman in forum C Programming
    Replies: 10
    Last Post: 11-01-2012, 06:25 PM
  2. Scanning(copying) a .txt file to struct array
    By cagurtay in forum C Programming
    Replies: 10
    Last Post: 08-08-2012, 02:32 PM
  3. Scanning txt file values into an array
    By skmightymouse in forum C Programming
    Replies: 16
    Last Post: 04-28-2012, 01:38 PM
  4. scanning and printing a string
    By Prestige in forum C Programming
    Replies: 5
    Last Post: 12-16-2010, 03:35 AM
  5. Replies: 1
    Last Post: 04-25-2006, 12:14 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21