StatMech
Loading...
Searching...
No Matches
ConvertDataFormat.cpp File Reference

Functions

int main (int argc, char **argv)
 

Function Documentation

◆ main()

int main ( int  argc,
char **  argv 
)
7 {
8 if(argc != 2) {
9 printf("Usage: 1.This 2.Data File\n");
10 exit(EXIT_FAILURE);
11 }
12 Integer_t const NUMDATA=5; // number of column in the output datafile.
13 char* data[NUMDATA];
14
15 //******************** Checking the filename and opening input/output files ********************
16 Integer_t const BUFMAX=1000;
17 const char* DATANAME="Operators_";
18 const char NAME_SEPERATOR='_';
19 const char* DATA_SEPERATOR=" ";
20 char suffix[BUFMAX], InFilename[BUFMAX], OutFilename[BUFMAX], stemp[BUFMAX], stemp2[BUFMAX];
21 char* OutFilenamePointer = OutFilename;
22 char* strptemp;
23
24 strcpy(InFilename, argv[1]);
25 if(strstr(InFilename, DATANAME) == NULL) {
26 printf("Error: Input \"%s\" does not contain \"%s\".\n", InFilename, DATANAME);
27 exit(EXIT_FAILURE);
28 }
29 strcpy(OutFilename, InFilename);
30 strptemp = strrchr(OutFilename, NAME_SEPERATOR);
31 if( (strptemp = strrchr(OutFilename, NAME_SEPERATOR)) == NULL) {
32 printf("Error: Intput \"%s\" does not contain \"%c\".\n", OutFilename, NAME_SEPERATOR);
33 exit(EXIT_FAILURE);
34 }
35 strptemp[0] = '\0';
36 sprintf(suffix, "_Raw_%s", strptemp+1);
37 strptemp = NULL;
38 strcat(OutFilename, suffix);
39 printf("Output file: %s.\n", OutFilename);
40
41 FILE *Infp;
42 Infp = fopen(InFilename, "r");
43 if(Infp == NULL) {
44 fprintf(stderr, "Can't open a file %s\n", InFilename);
45 exit(EXIT_FAILURE);
46 }
47 FILE *Outfp;
48 Outfp = fopen(OutFilenamePointer, "w");
49 if(Outfp == NULL) {
50 fprintf(stderr, "Can't open a file %s.\n", OutFilenamePointer);
51 exit(EXIT_FAILURE);
52 }
53 //******************** (END)Checking the filename and opening input/output files ********************
54
55 //******************** Copy headers to new file ********************
56 const char* DESCRIPTION = "1.repetition";
57 while( fgets(stemp, BUFMAX, Infp) ) {
58 if(stemp[0] == '\n') break;
59 if(stemp[0] != '#') {
60 fprintf(Outfp, "#%s", stemp);
61 } else if( strstr(stemp, DESCRIPTION) ) {
62 fprintf(Outfp, "#1.repetition, 2.System size, 3.Energy density, 4.Relative Pos, 5.Exp value\n");
63 } else {
64 fprintf(Outfp, "%s", stemp);
65 }
66 }
67 fprintf(Outfp, "\n");
68 //******************** (END)Copy headers to new file ********************
69
70 while( fgets(stemp, BUFMAX, Infp) ) {
71 strptemp = strchr(stemp, '\n');
72 if(strptemp==NULL) {
73 fprintf(stderr, "Warning: There is no Newline in \"%s\".\n", stemp);
74 continue;
75 }
76 if(stemp[0] == '\n') {
77 fprintf(Outfp, "\n");
78 continue;
79 }
80
81 strcpy(stemp2, stemp);
82 data[0] = strtok(stemp, DATA_SEPERATOR);
83 for(Integer_t i = 1;i < NUMDATA; ++i) {
84 data[i] = strtok(NULL, DATA_SEPERATOR);
85 if( data[i]==NULL ) {
86 fprintf(stderr, "Error: There is not enough columns in Input file.\n");
87 fprintf(stderr, "Error: Input data \"%s\".\n", stemp2);
88 exit(EXIT_FAILURE);
89 }
90 }
91 for(Integer_t i = 0;i < NUMDATA; ++i) fprintf(Outfp, "%s ", data[i]);
92 fprintf(Outfp, "\n");
93 }
94
95 fclose(Infp);
96 fclose(Outfp);
97
98 return 0;
99}
MKL_INT Integer_t
Definition mytypes.hpp:359