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??
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.
#include <stdio.h>
#include <stdlib.h>
#include <string>
usingnamespace 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])
#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 = newdouble[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?
#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 = newdouble[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)