Files

Hi people,

My problem is the following: I would like to create N (let's say N=50) files with the same name but a number, that is to say: file1.txt, file2.txt, file3.txt, etc., and then write on then. For one file, it would be the following:
FILE* Example;
Example = fopen("file1.txt", "w");
and to write there:
for(int i; i<r; i++)
fprintf(Example, "%d", 5*i); // for example
and close:
fclose(Example);

How can I create this N files and then write on them??

Thanks very much!
If you were on UNIX/LINUX, you could write a script to do it. If not, you might want to write a program to do it. Explain your constraints and I'll try to help you.
Hi,
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <stdio.h>
#include <stdlib.h>
#include <string>
using namespace std;
int main ()
{
  FILE * pFile;
  string str;
  char  chr[2];
  for (int k = 1; k <= 50; k++)
  {
  	str = "file.";
  	itoa(k, chr, 10);//wasn't found.
  	str.append(chr);
        str.append(".txt");
  	pFile = fopen (str.data(),"w");
  	if (pFile!=NULL)
  	{
    	fputs ("fopen example",pFile);
    	fclose (pFile);
  	}
  }
  return 0;
}

I gave a try as my very first exercise in C++ (just edited from the lib.)
There was still an error: the function "itoa()" (to convert an integer to a char) was not found, but it is in the library stdlib.h's description. Could someone please help explain this too? Thanks.
Thanks for your answer, but with "fputs" I can only write a constant character on the files. And I also get only 9 files, only up to 9, I do not know why.

What I need is the following: I have got a pointer, x, which size is N, and I need one file for x[1], called for example file_1.txt, another one for x[2], called file_2.txt, etc., to save the values that each x[i] takes, because it changes(I need the file to save each value on a row). The data I need to save is of type "double". (I mean: double* x; x=new double[N])

Can someone help me?

Thanks!!
asdf

Sample code for creating and writing to multiple files. Save inside whatever you like.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include<stdio.h>
#include<string.h>

#define Number_Of_Files 20

// floor(log10(Number_Of_Files))+1
// or just a big number
#define Length_of_Number_part 2

//10+Length_Of_Number_part
#define Max_Length_of_File_Name 12

int main(){

  char number[Length_of_Number_part];
  char file[]="file_";
  char ext[]=".txt";
  char temp[Max_Length_of_File_Name];

  int y;
  for (y=1; y<Number_Of_Files; y++){
    sprintf (number,"%d", y);

    int x;

    for(x=0; x<(Max_Length_of_File_Name); x++)
      temp[x]=0;
    strcat(temp,file);
    strcat(temp,number);
    strcat(temp,ext);
    
    FILE * Example;
    Example = fopen(temp, "w");


    fprintf(Example, "This is file number %d", y);

    fclose(Example);
  }

  return 0;
}
Last edited on
madredcake:

Thanks very much!! But I still have a little problem, I do not know why. Here is the code you passed me, with some modifications:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include<stdio.h>
#include<string.h>
#include <stdlib.h>

int main(){

  system("mkdir datos");
  int Number_of_Files=12;
  int Max_Lenght_of_File_Name=10;
  int Length_of_Number_part=8;
  char number[Length_of_Number_part];
  char file[]="datos/file_";
  char ext[]=".txt";
  char temp[Max_Lenght_of_File_Name];

 // here, I define a pointer to save it on the files:

   double* X;
   X = new double[Number_of_Files];
  for(int T=0; T<10; T++){ // I want the program to do everything 9 times.

 for(int k=0; k<(Number_of_Files); k++)
    X[k] = -10.0 + 0.7*k*T;

  int y;
  for (y=0; y<(Number_of_Files); y++){
    sprintf (number,"%d", y);

    int x;

    for(x=0; x<(Max_Lenght_of_File_Name); x++)
    temp[x]=0;
    strcat(temp,file);
    strcat(temp,number);
    strcat(temp,ext);

    FILE * Example;
    Example = fopen(temp, "w");

    fprintf(Example, "X[%d] = %lf\n", y, X[y]);

    fclose(Example);
  }
}
  return 0;
}


The problem is that I have to save the data for each T on a different row, and using this code, on the files only appear the value of each X[y] that corresponds to T=9, that is to say, the biggest value of T.
Do you know what is happening?
Ok, now I know what is happening: introducing the T, for each value of it, I am creating a new file "file_number.txt", "destroying" the file I obtained for the previous T, and that is the reason why I can only see the X values that correspond to the last value of T, and not all of them; but I do not know how can I avoid this, because the "y" loop must be inside of the "T" loop... Could you please help me?
for(int T=0; T<10; T++){ // I want the program to do everything 9 times
That loop is iterating 10 times T=0,1,2,3,4,5,6,7,8,9;

I am not sure if I get what output you want.

But that might be one solution

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include<stdio.h>
#include<string.h>
#include <stdlib.h>

int main(){

  system("mkdir datos");
  int Number_of_Files=12;
  int Max_Lenght_of_File_Name=10;
  int Length_of_Number_part=8;
  char number[Length_of_Number_part];
  char file[]="datos/file_";
  char ext[]=".txt";
  char temp[Max_Lenght_of_File_Name];
  int y,z;
  FILE * Example;

  //creating files
  for (y=0; y<(Number_of_Files); y++){
    sprintf (number,"%d", y);

    for(z=0; z<(Max_Lenght_of_File_Name); z++)
    temp[z]=0;
    strcat(temp,file);
    strcat(temp,number);
    strcat(temp,ext);

    Example = fopen(temp, "w");  //Notice to "w" write-mode -> creating new file or overwriting old one
    fclose(Example);
  }



 // here, I define a pointer to save it on the files:

   double* X;
  X = new double[Number_of_Files];
  for(int T=0; T<9; T++){ // I want the program to do everything 9 times.

   for(int k=0; k<(Number_of_Files); k++)
     X[k] = -10.0 + 0.7*k*T;


  //writing to files
  for (y=0; y<(Number_of_Files); y++){
    sprintf (number,"%d", y);


    for(z=0; z<(Max_Lenght_of_File_Name); z++)
    temp[z]=0;
    strcat(temp,file);
    strcat(temp,number);
    strcat(temp,ext);

    Example = fopen(temp, "a"); //notice "a" mode -> opening file to write at the end

    fprintf(Example, "X[%d] = %lf\n", y, X[y]);

    fclose(Example);
  }
}
  return 0;
}


But for that kind of input you don't have to keep loop Y inside "T' loop
This code do exacly the same (output) as the previous one, also looks and works better ( the first is constantly jumping between files)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include<stdio.h>
#include<string.h>
#include <stdlib.h>

int main(){

  system("mkdir datos");
  int Number_of_Files=12;
  int Max_Lenght_of_File_Name=10;
  int Length_of_Number_part=8;
  char number[Length_of_Number_part];
  char file[]="datos/file_";
  char ext[]=".txt";
  char temp[Max_Lenght_of_File_Name];
  int y,z;
  FILE * Example;


  double X;

  //writing to files
  for (y=0; y<(Number_of_Files); y++){
    sprintf (number,"%d", y);


    for(z=0; z<(Max_Lenght_of_File_Name); z++)
    temp[z]=0;
    strcat(temp,file);
    strcat(temp,number);
    strcat(temp,ext);

    Example = fopen(temp, "w");

    for(int T=0; T<9; T++){
      X = -10.0 + 0.7*y*T;
      fprintf(Example, "X[%d] = %lf\n", y, X);
    }

    fclose(Example);
  }

  return 0;
}
Last edited on
Thanks very very much! now it works correctly.
thanks again!
Topic archived. No new replies allowed.