Hello, first post here so i hope i don't break any rules.
I need to read some data from several consecutively named data files. The thing is a) the data files are gonna be named something like: datafile0001.asc, datafile0002.asc, etc, that is the way they're named and b) they will have random names, say one set will start with 820nm500mw0001.asc , another set will start with 830nm500uw, another will start with 780100mw0001.asc, i.e. their names are random.
This program below works if the file name is inputted as datafile, without the 0001.asc, i.e. if only the prefix is inputted. For 780100mw0001.asc, i need to input 780100mw when the program asks for the original filename.
So, i'd want the program to work by inputting the datafile name completely i.e. 780100mw0001.asc.
Any ideas on how to do this?
TIA
Code:#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char* argv[])
{
FILE *data,*loco;
char buffer[1001],original_name[60],final_name[60],temporary_name[60],final_temp_name[60];
float wavelength, signal, reference,normalized;
int i,number_of_files;
printf("Enter the number of files\n");
scanf("%d",&number_of_files);
printf("original filename?\n");
scanf("%s",&original_name);
printf(" destination filename?\n");
scanf("%s",&final_name);
strcpy(temporary_name,original_name);
strcpy(final_temp_name,final_name);
for(i=1;i<=number_of_files;i++)
{
if(i<10){
sprintf(original_name,"%s000%d.asc",temporary_name,i);
data = fopen(original_name,"r");
puts(original_name);
sprintf(final_name,"%s%d.dat",final_temp_name,i);
loco = fopen(final_name,"w");
}
else
{
sprintf(original_name,"%s00%d.asc",temporary_name,i);
data = fopen(original_name,"r");
puts(original_name);
sprintf(final_name,"%s%d.dat",final_temp_name,i);
loco = fopen(final_name,"w");
}
fgets(buffer, 1000, data);
while(feof(data) == 0)
{ /*read some data, lets process it.*/
/*extract individual values from the buffer*/
sscanf(buffer, "%f%f%f", &wavelength, &signal, &reference);
/*calculate the normalized spectrum*/
normalized = signal/reference;
/*outputs data to data file*/
fprintf(loco,"%f %f\n",wavelength,normalized);
/*read the next line from the file*/
fgets(buffer, 1000, data);
}
fclose(loco);
fclose(data);
}
return 0;
}