Advertisement
Guest User

Untitled

a guest
May 20th, 2019
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.13 KB | None | 0 0
  1.  
  2. #define _CRT_SECURE_NO_WARNINGS
  3. #define EXIT 4
  4. #define SIZE_STR 20
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <time.h>
  8. #include <string.h>
  9. void copyTextFile(FILE* dst, FILE* src);
  10. int printMenu(void);
  11. int searchFile(char str[], char** data_file, int colsize, int rowsize);
  12. int main()
  13. {
  14. FILE* csvfile;
  15. int c,choice=0, countRow=0, countcol=0,flag=0,row,col;
  16. char str[SIZE_STR] = {0};
  17. char** data;
  18. char* ptr;
  19. csvfile = fopen("example.csv", "r");
  20. c = fgetc(csvfile);
  21. while (c != EOF)
  22. {
  23. //printf("%c",(char)c);
  24. c = fgetc(csvfile);
  25. if (c == ',')
  26. {
  27. countcol++;
  28. }
  29. else if (c == '\n')
  30. {
  31. countRow++;
  32. }
  33. else
  34. {
  35. ptr = (char*)malloc(sizeof(char));
  36. ptr = c;
  37. data[countRow][countcol] = ptr;
  38. }
  39. }
  40. for (row = 0; row < countRow; row++)
  41. {
  42. for (col = 0; col < countcol; col++)
  43. {
  44. printf("%c", data[row][col]);
  45. }
  46. }
  47.  
  48. countcol = countcol / countRow;
  49. printf("\n %d-cols", countcol);
  50. printf("\n %d-rows", countRow);
  51. while (choice!=EXIT)
  52. {
  53. choice = printMenu();
  54. switch (choice)
  55. {
  56. case 1:
  57. {
  58. flag=searchFile(str, data,countcol,countRow);
  59. if (flag !=0)
  60. {
  61. printf("Value was found in row %d", flag);
  62. }
  63. else
  64. {
  65. printf("Value Wasn't Found");
  66. }
  67. break;
  68. }
  69. case 2:
  70. {
  71. printf("");
  72. break;
  73. }
  74. case 3:
  75. {
  76. printf("");
  77. break;
  78. }
  79. default:
  80. {
  81. printf("Try agian.");
  82. break;
  83. }
  84. }
  85. }
  86. fclose(csvfile);
  87. getchar();
  88. }
  89. int printMenu(void)
  90. {
  91. int choice = 0;
  92. printf("Please enter your choice:\n1 - Search a term in the document.\n2 - change a value in a specific place.\n3 - copy a value from one place to another");
  93. scanf("%d",&choice);
  94. return choice;
  95. }
  96. int searchFile(char str[], char** data_file,int colsize,int rowsize)
  97. {
  98. int col=0, row=0;
  99. int indexRowOrFlag=0;
  100. printf("Enter value to search: ");
  101. fgets(str, SIZE_STR, stdin);
  102. str[strlen(str) - 1] = "\0";
  103. for (row = 0; row < rowsize; row++)
  104. {
  105. for (col = 0; col < colsize; col++)
  106. {
  107. if (strcmp(str, data_file[row][col]) == 0)
  108. {
  109. return row;
  110. }
  111. }
  112. }
  113. return indexRowOrFlag;
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement