fork download
  1. #include <iostream>
  2. #include <fstream>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <vector>
  6. #include <string>
  7. #include <string.h>
  8.  
  9. //#define DEBUG
  10.  
  11. using namespace std;
  12.  
  13. /// File type fpr function:readPath
  14. typedef struct ftype {
  15. char** file_name; // File name array
  16. int line_cnt; // Return file counts of file_name
  17. } ftype;
  18.  
  19. enum SelfFile {
  20. FILE_POS = 0,
  21. FILE_NEG = 1,
  22. FILE_NEG_HARD = 2
  23. };
  24.  
  25. /// File class for ftype
  26. class fileClass {
  27.  
  28. private:
  29. void initial();
  30. vector<int> file_num; // File numbers for each fileArr
  31. vector<ftype> fileArr; // POS, NEG, HARD txt array
  32.  
  33. public:
  34.  
  35.  
  36. void loadFile(ftype src, SelfFile type);
  37. void loadFile(char* txtPath, SelfFile type);
  38. void loadFile(char* , char* );
  39.  
  40.  
  41. void setNum(int num, SelfFile type);
  42. int getNum(SelfFile type);
  43. char* getName(int num, SelfFile type);
  44.  
  45.  
  46. fileClass() {
  47. initial();
  48. }
  49.  
  50. /* Two text path*/
  51. fileClass(char* pos_path, char* neg_path) {
  52. initial();
  53. loadFile(pos_path, FILE_POS);
  54. loadFile(neg_path, FILE_NEG);
  55. }
  56.  
  57. /* Three text path*/
  58. fileClass(char* pos_path, char* neg_path, char* hard_path) {
  59. initial();
  60. loadFile(pos_path, FILE_POS);
  61. loadFile(neg_path, FILE_NEG);
  62. loadFile(hard_path, FILE_NEG_HARD);
  63. }
  64.  
  65. };
  66.  
  67. /// Read "file path" from File and convert into array
  68. ftype readPath(char* filePath_in) {
  69.  
  70. ftype fr;
  71. fr.line_cnt = 0;
  72. fr.file_name = NULL;
  73.  
  74. char filePath[100];
  75. sprintf(filePath,"%s", filePath_in);
  76.  
  77. FILE* fp = fopen(filePath, "r+");
  78. if (fp == NULL) {
  79. fprintf(stderr, "Error Reading File : %s\n", filePath);
  80. }
  81. else {
  82. fprintf(stderr,"Reading File : %s\n", filePath);
  83. }
  84.  
  85. char tmp = 0;
  86. int s_cnt = 0;
  87. int pos = 0, pos_eof = 0;
  88.  
  89.  
  90. while (1) {
  91. if (feof(fp)) break;
  92.  
  93. tmp = fgetc(fp);
  94.  
  95. /// Replace '\' in the file
  96. if (tmp == '\\') {
  97. fseek(fp, ftell(fp) - 1, SEEK_SET);
  98. fputc('/', fp);
  99. fflush(fp);
  100. }
  101.  
  102. /// Check how many lines(\n) in the file
  103. if (tmp == '\n') {
  104. if ((ftell(fp) - pos) == 2) // Sequential '/n'
  105. fprintf(stderr, "Warning : Sequential shift line on : %d\n", fr.line_cnt);;
  106. fr.line_cnt++;
  107. pos = ftell(fp);
  108. }
  109. }
  110.  
  111. #ifdef DEBUG
  112. printf("\nStep1..");
  113. #endif
  114.  
  115. /// Check exact line count
  116. fseek(fp, 0, SEEK_END);
  117. pos_eof = ftell(fp); // Last position of the file
  118. fr.line_cnt = (pos == pos_eof) ? fr.line_cnt : (fr.line_cnt + 1); // Check if last symbol is "\n"
  119.  
  120. rewind(fp); // Return file pointer to start
  121.  
  122. #ifdef DEBUG
  123. printf("...[OK]\n");
  124. printf("Step2..");
  125. #endif
  126.  
  127. /// Allocate memory for file name pointer array (double pointer)
  128.  
  129. //fr.file_name = (char**)malloc(sizeof(char*)*fr.line_cnt);
  130. fr.file_name = new char*[fr.line_cnt];
  131.  
  132. int f_ret; // fscanf flag
  133.  
  134. /// Scan file name into pointer array
  135.  
  136. #ifdef DEBUG
  137. printf("...[OK]\n");
  138. printf("Step3..");
  139. #endif
  140.  
  141. while (1) {
  142. if (feof(fp)) break;
  143.  
  144. //fr.file_name[s_cnt] = (char*)malloc(sizeof(char) * 100);
  145. fr.file_name[s_cnt] = new char[100];
  146. f_ret = fscanf(fp, "%s", fr.file_name[s_cnt]);
  147.  
  148. if (f_ret != 1)
  149. sprintf(fr.file_name[s_cnt], "Empty");
  150.  
  151. s_cnt++;
  152.  
  153. }
  154.  
  155. #ifdef DEBUG
  156. printf("...[OK]\n");
  157. printf("Step4..");
  158. #endif
  159.  
  160. /// Let other empty pointer array shows "Empty"
  161. for (int i = s_cnt; i<fr.line_cnt; i++) {
  162. //fr.file_name[i] = (char*)malloc(sizeof(char) * 100);
  163. fr.file_name[i] = new char[100];
  164. sprintf(fr.file_name[i], "Empty");
  165. }
  166.  
  167. #ifdef DEBUG
  168. printf("...[OK]\n");
  169. #endif
  170.  
  171. fclose(fp);
  172.  
  173. return fr;
  174. }
  175.  
  176. /* Initial FileClass */
  177. void fileClass::initial(){
  178.  
  179. /// Erase
  180. fileArr.clear();
  181. file_num.clear();
  182.  
  183. /// Resize
  184. file_num.resize(3);
  185. fileArr.resize(3);
  186. }
  187.  
  188.  
  189. /* Load <ftype>file into <class>fileclass */
  190. void fileClass::loadFile(ftype src, SelfFile type){
  191.  
  192. fileArr.at(type) = src;
  193. file_num.at(type) = src.line_cnt;
  194.  
  195. }
  196.  
  197. /* Load <ftype>file into <class>fileclass by <char*>text path */
  198. void fileClass::loadFile(char *txtPath, SelfFile type){
  199.  
  200. printf("\nLoad Test: %s, type = %d\n",txtPath,type);
  201. fileArr.at(type) = readPath(txtPath);
  202. file_num.at(type) = fileArr.at(type).line_cnt;
  203. printf("Load Test [done]\n");
  204. }
  205.  
  206.  
  207. void fileClass::loadFile(char * posPath, char * negPath)
  208. {
  209. loadFile(posPath, FILE_POS);
  210. loadFile(negPath, FILE_NEG);
  211. }
  212.  
  213. /* Set load number amount for data
  214. / int num : Only indicate a number, doensn't affect the actual data
  215. */
  216. void fileClass::setNum(int num, SelfFile type){
  217.  
  218. file_num.at(type) = num;
  219. }
  220.  
  221. /* Get load number amount for data
  222. / @return file_num : Only indicate a number, doensn't affect the actual data
  223. */
  224. int fileClass::getNum(SelfFile type)
  225. {
  226. return file_num.at(type);
  227. }
  228.  
  229.  
  230. void freeTest(ftype *test){
  231. for (int i =0; i<test->line_cnt;i++)
  232. delete test->file_name[i];
  233. delete test;
  234. //free(test);
  235. }
  236.  
  237. void testResult(ftype test){
  238.  
  239. for (int i=0; i < test.line_cnt; i++){
  240. printf("test[%d]=%s",i, test.file_name[i]);
  241. }
  242.  
  243.  
  244. }
  245. int main()
  246. {
  247. fileClass dataset(
  248.  
  249. "Path/train_Pos.txt",
  250. "Path/train_Neg_12180.txt",
  251. "Path/train_Neg_I.txt"
  252. );
  253.  
  254. fileClass dataset2(
  255.  
  256. "Path/train_Pos.txt",
  257. "Path/train_Neg_12180.txt",
  258. "Path/train_Neg_I.txt"
  259. );
  260.  
  261. /*ftype test1 = readPath("Path/train_Pos.txt");
  262. ftype test2 = readPath("Path/train_Pos.txt");
  263. ftype test3 = readPath("Path/train_Pos.txt");
  264. ftype test4 = readPath("Path/train_Pos.txt");
  265. ftype test5 = readPath("Path/train_Pos.txt");
  266. ftype test6 = readPath("Path/train_Pos.txt");*/
  267.  
  268. printf("\n\n\n[ALL DONE]\n");
  269. system("PAUSE");
  270. return 0;
  271. }
  272.  
Runtime error #stdin #stdout #stderr 0s 15240KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
Error Reading File : Path/train_Pos.txt