0

I am quite new to programming, I need some advice on how to automatically name a file using C. Since I need to run the same source code for several times on a cluster computer. To prevent overwriting on the same output file, I have to make each output file has a different name.

void RecordNumber()
{
  srand(time(NULL));
  FILE* fopen(), * fp;
  char name[64];
  int x, y;
  long num[256];
  int na = rand();

  sprintf(name, "num[%d].dat", na);
  if (first_num) {
    first_num = 0;
    fp = fopen(name, "w");
  }
  else
    fp = fopen(name, "a");
  
  for (x = 0; x <= species; ++x)
    num[x] = 0;
  
  for (x = 1; x <= xfield; ++x)
    for (y = 1; y <= yfield; ++y)
      ++num[state[x][y]];
  
  fprintf(fp, "\n%d", thetime);
  
  for (x = 0; x < species; ++x)
    fprintf(fp, "\t%d", num[x]);
 
  fclose(fp);
}

I write an int "na" to store a random number, but the random number does not show in the file name (the file name is still "dat").

void RecordNumber() is stored in the library. In one of my code, I use void Report(){} to call the RecordNumber(0) function to record all the numbers in the simulations


void Report()

 {

for (int j=j=thetime;j<=10000;j=j+1)
      {
          if (j%100==0)
       {EchoTime(0);
       CountSpecies(0);
           RecordNumber(0);
       }
           else
               break;
       }
      
}

I want the file name looks like "dat_%d", what should I do?

  • 1
    This code should create the file as named. What exactly is the name of the file that is created? If you print `name` what do you get? Also, using square braces in filenames is not a good idea since they may be used by the shell. Consider using underscores instead, i.e. `num_%d.dat`. – dbush Sep 18 '20 at 13:21
  • I reformatted your code so it is readable. It should work though. – Jabberwocky Sep 18 '20 at 13:31
  • Oh: what is this: `FILE* fopen(), * fp;`?? it should be `FILE* fp;` – Jabberwocky Sep 18 '20 at 13:33
  • 1
    Also you need to check if `fopen` succeeds, and if not, you need to print an error message and abort the program. – Jabberwocky Sep 18 '20 at 13:34
  • `void RecordNumber()` is either C++, or from 1987. Use `void RecordNumber(void)` – William Pursell Sep 18 '20 at 13:43
  • @dbush I will get a file called "num.dat" – Margaret Yang Sep 18 '20 at 13:52
  • @MargaretYang I don't believe this. Either the code you run is not the code you show in the question, or the file it not where you think it is, or there is something else you're not telling us. Add `printf("opening file %s\n", name);` before each of your `fopen`s and tell us what happens. Or even better: use your debugger, step through your code and inspect the content of the relevant variables. – Jabberwocky Sep 18 '20 at 13:54
  • @Jabberwocky thanks! I have called the function and I do have numbers in my output file, so I think fopen did work? – Margaret Yang Sep 18 '20 at 13:59
  • Do what I've told you, otherwise we can't help. Adding the printf as I suggested shouldn't be a big deal. – Jabberwocky Sep 18 '20 at 14:00
  • @Jabberwocky ok, I will do that! – Margaret Yang Sep 18 '20 at 14:02
  • If you run your program multiple times within the same second, you'll get the same random seed and hence the same filename each time, so the files will overwrite one another. There is also the possibility that you'll later overwrite a file when getting the same number just by chance. Is that really acceptable in your application? – Nate Eldredge Sep 18 '20 at 14:22
  • @NateEldredge that's indeed another problem, but for the moment we're trying to get more information from the OP (see my previous comments). – Jabberwocky Sep 18 '20 at 14:28
  • @Jabberwocky what I will get is a line says opening file num.dat – Margaret Yang Sep 18 '20 at 15:14
  • @MargaretYang what is displayed if you just put `printf("filename = num[%d].dat", na);` right before `sprintf(name, "num[%d].dat", na);`? – Jabberwocky Sep 18 '20 at 15:16
  • BTW: you write _"what I **will** get..._. That's not what I was asking, I was asking _"what **do** you get...."_ – Jabberwocky Sep 18 '20 at 15:17
  • @Jabberwocky still "opening file num.dat". sorry my expression was not clear enough, what I meant was " what I got" – Margaret Yang Sep 18 '20 at 15:26
  • @MargaretYang I'm giving up. I have no idea what could be wrong in your code. Maybe some other part of your program exhibits undefinedd behaviour and messes something up that causes sprintf and printf no longer to work anymore for some reason. Who knows. – Jabberwocky Sep 18 '20 at 17:30

1 Answers1

0

I ran this modified piece of your code and it works fine, so it is possible that other parts of your code can be malfunctioning.

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

void RecordNumber()
{
    srand ( time(NULL) );
    FILE *fp;
    char name[64];
    int x,y;
    long num[256];
    int na=rand();

    sprintf(name,"num[%d].dat",na);
    fp = fopen(name,"w");
    // fp = fopen(name,"a");
    fclose(fp);
 }

int main(){
    RecordNumber();
    return 0;
}

Every time I run this code I get random file names like:
'num[1087620761].dat'     // single quotes are included in file name
'num[1518719602].dat'

  • thank you very much for helping! what file name did you get? is it still num.dat? – Margaret Yang Sep 18 '20 at 14:18
  • @MargaretYang he wrote that this piece of code works. But __you__ should tell us what the actual name is when you put `printf("opening file %s\n", name);` before each of your `fopen`. – Jabberwocky Sep 18 '20 at 14:25