Thread: Read file, Create struct 5x5 numbers

  1. #1
    Registered User
    Join Date
    Jul 2006
    Posts
    46

    Read file, Create struct 5x5 numbers

    Dear Friends
    I try to make a program that will read from txt file 5x5= 25 numbers and save them in array again 5x5. My numbers are positive integers. I want to ask here instead of save them in a array 5x5 (wich i have done) can i use a struct that will save seperate dozens and seperate units of each number. How i can do that? Any ideas

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    I'm not sure what you mean by "save seperate dozens and seperate units of each number" - can you elaborate?

    Struct variables, like scalar-type variables, can be declared as arrays.

    Code:
    #define MAX_ROWS  5
    #define MAX_COLS  5
    
    // ...
    
    struct point_s
    {
        int data;
        int unit;
    };
    
    // ...
    
    struct point_s point[MAX_ROWS][MAX_COLS];  /* 5 x 5 array of struct */

  3. #3
    Registered User
    Join Date
    Jul 2006
    Posts
    46
    [I mean if my first nymber of txt file will be for example 15 then in the first position of array [1][1] i must save
    1 dozen
    5 units (15)

    So in the struct i need i guess 2 arrays in first array i will save the dozens to each place and in the second i will save units in each place

  4. #4
    Registered User
    Join Date
    Jul 2006
    Posts
    46
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    #define MAX_ROWS 5
    #define MAX_COL 5
    
    struct dozens
    {
      int arraydozens[MAX_ROWS][MAX_COL];
    
    };
    
    struct units
    {
       int arrayunits[MAX_ROWS][MAX_COL];
    } units[MAX_ROWS][MAX_COL];
    
    int main()
    {
    
       //int array[5][5];
       int i,j;
       struct dozens dozens1 [MAX_ROWS][MAX_COL];
    
       FILE
       *myfile;  //Δημιουργία δείκτη file για το άνοιγμα αρχείου
       myfile=fopen("c:\\treasure.txt","r");  //’νοιγμα του αρχείου
    
    
       if (myfile==NULL)
        {
        printf("File doesnt exist");
        exit(0);
        }
       else
        for(i=0;i<5;i++)
    
         for(j=0;j<5;j++)
    
         fscanf(myfile,"%d %d", &dozens1[i][j].arraydozens[i][j]);
    
       for(i=0;i<5;i++)
    
         for(j=0;j<5;j++)
        printf("array[%d][%d]=%d\n",i,j,dozens1[i][j].arraydozens[i][j]);
        fclose(myfile);
    
         return 0;
         }
    I try to do someting like the above but its wrong. I need to read a file with 25 numbers. Then make a struct that will have an array with dosen of the numbers and the other with the unit of the numbers

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 03-10-2013, 09:47 PM
  2. Replies: 12
    Last Post: 06-18-2012, 08:23 AM
  3. How to read numbers out of a file in c
    By muskateer1 in forum C Programming
    Replies: 3
    Last Post: 04-05-2012, 02:29 PM
  4. Read in numbers from file
    By ElNino in forum C Programming
    Replies: 1
    Last Post: 02-29-2012, 01:47 PM
  5. Replies: 3
    Last Post: 03-02-2008, 12:33 PM