FILE input/output and ARRAY

This is a discussion on FILE input/output and ARRAY within the C Programming forums, part of the General Programming Boards category; Hello Why my program can't display a table??? what's wrong in my program? thnak TABLE: 0123 1.23 23 20 20 ...

  1. #1
    Registered User
    Join Date
    Feb 2002
    Posts
    11

    FILE input/output and ARRAY



    Hello


    Why my program can't display a table???
    what's wrong in my program?
    thnak


    TABLE:

    0123 1.23 23 20 20
    0234 2.34 34 50 25
    3456 34.56 56 50 10
    4567 45.67 7 10 5
    5678 6.78 75 75 25



    #include<stdio.h>
    #include<stdlib.h>

    #define ROW 5
    #define COL 5


    void open_file(int[][COL]);
    void display_table(int[][COL]);

    int main(void)
    {
    int table[ROW][COL];

    open_file(table);
    display_table(table);

    return 0;
    }

    /****************************** OPEN FILE ******************/

    void open_file(int table[ROW][COL])
    {
    FILE *fp;

    int row = 0;

    if(!(fp=fopen("kk7.txt", "r")))
    {
    printf("\nCANNOT opening file kk7.txt\n");
    exit(EXIT_FAILURE);
    }

    while(fscanf(fp, "%d%f%d%d%d", &table[row][0], &table[row][1], &table[row][2],
    &table[row][3], &table[row][4]
    )!=EOF)

    {row++;}

    fclose(fp);
    }

    /****************************** DISPLAY TABLE ******************/

    void display_table(int table[ROW][COL])
    {
    int row, col;

    for(row=0; row<ROW; row++)
    {
    for(col=0; col<COL; col++)
    {
    printf("%d ", table[row][col]);
    }
    printf("\n");
    }
    return;
    }

  2. #2
    Registered User
    Join Date
    Jan 2002
    Location
    Vancouver
    Posts
    2,220
    If you want the function to give you the table, you probably need pointers [or a global variable]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. dump array into file in "hex format"
    By pic-o-matic in forum C Programming
    Replies: 5
    Last Post: 01-30-2009, 08:44 AM
  2. segmentaion fault with File Input/Output
    By sara.stanley in forum C Programming
    Replies: 7
    Last Post: 04-04-2006, 04:57 AM
  3. Read file in 2D array
    By Chook in forum C Programming
    Replies: 1
    Last Post: 05-08-2005, 01:39 PM
  4. File Input/Output ... Problems
    By FAMOUS in forum C++ Programming
    Replies: 3
    Last Post: 11-10-2003, 02:00 PM
  5. read from .txt file & put into array?
    By slow brain in forum C Programming
    Replies: 6
    Last Post: 02-25-2003, 05:16 AM

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