0

I have huge amount of txt files which contains 64x64 matrices consisting of integers. txt files has names like:

mat_1.txt, mat_2.txt, mat_3.txt, mat_4.txt, .... , mat_n.txt.

I have to create a variable, allocate space on host and device, read txt file and copy to device. is it possible to do it all in one loop?

I know how to create a string with sprintf but do not know how to use this string for example for declaring variables.

char fname[10]; 
for( int k=1; k<=n; k++ )
{
    sprintf( fname, "mat_%d", k );
    int *fname;    // how to say to compiler that insted of `fname` there 
                   // should be `mat_1` and in next cycle `mat_2`?
}
6
  • The code snippet you supply, if you remove the int pointer declaration, already does what you want. It will update fname to be "mat_1" the first time, "mat_2" the second time, etc. Commented Mar 29, 2012 at 12:03
  • Why would anyone do something like this? Dynamic variable names are already a mess in scripting languages, nobody needs them in compiled languages! @JoachimPileborg: I think he wants to create variables named mat_X dynamically Commented Mar 29, 2012 at 12:06
  • If it's like @ThiefMaster says, then no it's not possible. C has no such features, not even in a library. Commented Mar 29, 2012 at 12:12
  • i have 22500 matrices so your answer ist to write int* mat_1, mat_2, ... , mat_22500 ? Commented Mar 29, 2012 at 12:21
  • 1
    @user1281071 Do you need all the variables in memory at one time? If so, then an array and a subscript, i.e., mat[22500] is your friend. If not, why are you unable to wrap your head around "when I read in this matrix, my pointer named mat points to the matrix I just read in"? Commented Mar 29, 2012 at 12:45

2 Answers 2

3

You can't create a variable name at runtime. Variable names are for compiler and only compiler to know and cannot be generated on the fly.

What you need is an array. Since the data already need to be stored in an array, you need to add 1 dimension to your array.

For example, if data in mat_1.txt is a 1 dimensional array, you can have:

int **mat;                        // 2D array
int k;
mat = malloc(n * sizeof(*mat));   // get an array of pointers (add error checking)
for (k = 1; k <= n; ++k)
{
    char fname[20]; 
    FILE *fin;
    sprintf(fname, "mat_%d.txt", k);
    fin = fopen(fname, "r");
    if (fin == NULL)
        /* Handle failure */
    /* read number of items */
    mat[k-1] = malloc(number of items * sizeof(*mat[k-1]));  // allocate an array for each file
    /* read data of file and put in mat[k-1] */
    fclose(fin);
}
/* At this point, data of file mat_i.txt is in array mat[i-1] */
/* When you are done, you need to free the allocated memory: */
for (k = 0; k < n; ++k)
    free(mat[k]);
free(mat);
Sign up to request clarification or add additional context in comments.

1 Comment

I want data form file store in variable mat_k, where k is 1,2,3, ... problem is how to create a variable in cycle?
1

What computer are you using?

A 64x64 array of int, where int is 4 bytes, is array of 16,386 bytes, 22,500 files with 1 matrix/file would be 368,640,000 bytes.

That works fine on my 5 year old laptop:

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


#define MAX_FILES (22500)
#define MAX_J (64)
#define MAX_K (64)

int a[MAX_FILES][MAX_J][MAX_K];
const char namefmt[] = "mat_%d";

int main (int argc, const char * argv[]) {
    char fname[strlen(namefmt)+10+1];  // an int has 10 or less digits
    for (int fileNumber=0; fileNumber<MAX_FILES; ++fileNumber) {
        sprintf(fname, namefmt, fileNumber);
        FILE* fp = fopen(fname, "r");
        if (fp == NULL) {
            fprintf(stderr, "Error, can't open file %s\n", fname);
            exit(1);
        }
        for (int j=0; j<MAX_J; ++j) {
            for (int k=0; k<MAX_K; ++k) {
                //... read the value
            }
        }
        fclose(fp);
    }
    return 0;
}

It should work okay (though may become painfully slow) on a modern computer running an operating system with virtual memory, and enough swap space. Declaring it as an array, rather than using malloc will save a miniscule amount of space, but otherwise is the same.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.