fork download
  1. //main.c
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include "functions.h"
  5.  
  6. int main(int argc, char** argv){
  7. FILE* pfile = NULL;
  8. FILE* csvpfile = NULL;
  9. char* csvFileName;
  10.  
  11. if (argc != 2){
  12. printf ("File name error! Enter full path!\n");
  13. exit (INPUT_FILE_NAME_ERROR);
  14. }
  15.  
  16. pfile = fileOpen(argv [1]);
  17. csvFileName = csvCreateFileName (argv [1]);
  18. csvpfile = fopen (csvFileName, "w");
  19.  
  20. textToCSVConverter(pfile, csvpfile);
  21.  
  22. fclose (pfile);
  23. fclose (csvpfile);
  24. return EXIT_STATUS_OK;
  25. }
  26.  
  27.  
  28. //functions.c
  29. #include "functions.h"
  30.  
  31. FILE* fileOpen(char* filePath){
  32. FILE* pfile = NULL;
  33. if ((pfile = fopen(filePath, "r")) == NULL){
  34. printf ("Error opening file!\n");
  35. exit (FILE_OPEN_ERROR);//Could not open stream
  36. }
  37. return pfile;
  38. }
  39.  
  40. FILE* fileCreate(char* filePath){
  41. FILE* pfile = NULL;
  42. if ((pfile = fopen(filePath, "w")) == NULL){
  43. printf ("Error creating file!\n");
  44. exit (FILE_CREATE_ERROR);//Could not create stream
  45. }
  46. return pfile;
  47. }
  48.  
  49. void textToCSVConverter (FILE* txtpfile, FILE* csvpfile){
  50. char tmpChar;
  51. unsigned short int check = 0;
  52.  
  53. while ((tmpChar = fgetc (txtpfile)) != EOF){
  54. if ((char) tmpChar == ';'){
  55. strcpy(&tmpChar, ",");
  56. fwrite (&tmpChar, 1, 1, csvpfile);
  57. putc(tmpChar, stdout);
  58. }
  59. if (!(check = fwrite (&tmpChar, 1, 1, csvpfile)))
  60. printf ("No byte written!\n");
  61. }
  62. }
  63.  
  64. char* csvCreateFileName (char* FileName){
  65. size_t pathLen = strlen (FileName);
  66. char fileExt [4] = {'c', 's', 'v', '\0'};
  67. unsigned short int i, j;
  68.  
  69. printf ("TXT File Name: %s\n", FileName);
  70. for (i = pathLen - 3, j = 0; i < pathLen; i++, j++)
  71. FileName [i] = fileExt [j];
  72. printf ("CSV File Name: %s\n", FileName);
  73.  
  74. return FileName;
  75. }
  76.  
  77. //functions.h
  78. #include <stdio.h>
  79. #include <string.h>
  80. #include <stdlib.h>
  81.  
  82. #define BUFFER 128
  83.  
  84. #define INPUT_FILE_NAME_ERROR -3
  85. #define FILE_OPEN_ERROR -2
  86. #define FILE_CREATE_ERROR -1
  87. #define EXIT_STATUS_OK 0
  88.  
  89. FILE* fileOpen (char* filePath);
  90. FILE* fileCreate (char* filePath);
  91. void textToCSVConverter (FILE* txtpfile, FILE* csvpfile);
  92. char* csvCreateFileName (char* txtFileName);
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.c:4:23: fatal error: functions.h: No such file or directory
compilation terminated.
stdout
Standard output is empty