-8

I want to run a function over a loop and I want to store the outputs in different files, such that the filename contains the loop variable. Could you please advise me?for example as follows:

for(i==1;i==N;i++) 

    fp = fopen ("file_(NUMBER OF LOOP(I)).txt", "w+");
jepio
  • 2,276
  • 1
  • 11
  • 16
amin bk
  • 194
  • 1
  • 1
  • 12

5 Answers5

1

First off:

for(i==1;i==N;i++) 

Is incorrect on two levels. Firstly, you haven't declared i anywhere else, so its an undefined "thing" and not the int i = 1; I presume you want it to be. secondly, doing the comparison i == N while at the same time having i increment but N not will ensure your loop will only be run once, eliminating the need for a loop to begin with.

corrected, your loop statement would look something like this:

for(int i = 1; i < somevalue; i++)

Just built a string by concatenating the name prefix, i converted to a string, and the .txt suffix and then call the fopen with that string as first argument.

Magisch
  • 7,312
  • 9
  • 36
  • 52
  • 1
    classic case are `i = 1` and `i <= n` or `i = 0` and `i < n` – Ôrel Oct 13 '15 at 14:25
  • if you use `i = 1` you **have** to have declared `int i` beforehand., the comparison operators are entirely dependant on what he wants to do with the for loop. – Magisch Oct 13 '15 at 14:27
1

Firstly: your loop is all wrong. With the code you've written above, you're testing whether i==1, not setting i to 1. And then your condition for continuing the loop is i==N, which means it will only continue if that is true. You're presumably aiming for:

for(int i=1; i<=N; i++)

This would operate N times, with values of i running from 1 to N, inclusive.

One way to do this would be to use sprintf from the C library.

This requires you to create a char array, which will then be filled by sprintf. It takes the array as the first argument, then the string you want to fill it with. You can place tokens in the string indicating the type of variables to be included, and add those variables as subsequent arguments.

For example:

int x=5;
char buffer[50];
sprintf(buffer,"file%d.txt",x);

after the sprintf line is executed, the variable buffer will hold "file5.txt".

If you place the sprintf line within your loop, then you can of course use the loop index to get what you want.

A fuller tutorial on the usage of sprintf can be found here.

Chris H
  • 790
  • 9
  • 19
0

Nothing is easier than that:

for (long long int i = 0; i < N; ++i) {
    std::string filename("file_");
    filename += std::to_string(i) + ".txt";
    std::ofstream f(filename.c_str(), std::ios_base::out | std::ios_base::ate);
}
SergeyA
  • 61,605
  • 5
  • 78
  • 137
0

So you want:

file_0.txt

file_1.txt

etc.... Right?

You need to have the filename "file_i.txt" as a string. To do this, you're basically casting the integer i to a string, then concatenating that string of the iteration, with "file_" + "i's value" + ".txt" as your filename, and passing that variable to your last line posted.

So, you need to convert an int to string is the main crux of your problem. :)

http://www.cplusplus.com/articles/D9j2Nwbp/ or Easiest way to convert int to string in C++

Community
  • 1
  • 1
Adolphin
  • 62
  • 4
0

What you need is to format the filename using a call to snprintf and the appropriate format string. Below is a minimal example with error handling.

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

int main(void)
{
    int N = 100;
    const int buf_size = 20;
    char buf[buf_size];

    for (int i = 0; i < N; ++i) {
        int ret = snprintf(buf, buf_size, "file_%d.txt", i);
        if (ret < 0 || ret >= buf_size) {
            fprintf(stderr, "snprintf: error at iteration %d: return value was %d\n", i, ret);
            exit(1);
        }

        FILE *fp = fopen(buf, "w+");
        if (fp == NULL) {
            perror("couldn't open file for writing");
            continue;
        }
        /* 
         * do your stuff here 
         */
        fclose(fp);
    }
}
jepio
  • 2,276
  • 1
  • 11
  • 16