fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <stdbool.h>
  4. #include <string.h>
  5.  
  6. int main()
  7. {
  8. char script_path[] = "/home/anonimas/test.c";
  9.  
  10. FILE *script_file;
  11. script_file = fopen(script_path, "r");
  12. if (script_file == NULL)
  13. {
  14. printf("Can't open file");
  15. return 0;
  16. }
  17.  
  18. //Динамический двумерный массив
  19. char **script_inmem = NULL;
  20. unsigned int let = 0; //Прядковый номер знака
  21. unsigned int str = 0; //Номер строки
  22. script_inmem = (char**)malloc(sizeof(char*));
  23. script_inmem[0] = (char*)malloc(sizeof(char));
  24. if (!script_inmem)
  25. {
  26. printf ("Malloc: error allocate memory");
  27. return 0;
  28. }
  29. //Цикл считывания файла
  30. while (!feof(script_file))
  31. {
  32. char buffer = NULL;
  33. //Цикл считывания строки из файла
  34. while (!(buffer == '\n'))
  35. {
  36. buffer = getc(script_file);
  37. script_inmem[str][let] = buffer;
  38. printf("%c", script_inmem[str][let]);
  39. let++;
  40. script_inmem[str] = (char*)realloc(script_inmem[str], (let+1)*sizeof(char)); // Добавляем памяти для знака в строке
  41. if (!script_inmem)
  42. {
  43. printf ("Realloc: error allocate memory");
  44. return 0;
  45. }
  46. }
  47. str++;
  48. script_inmem = (char**)realloc(script_inmem, (str+1)*sizeof(char*)); //Добовляем памяти для строки
  49. if (!script_inmem)
  50. {
  51. printf ("Realloc: error allocate memory");
  52. return 0;
  53. }
  54. let = 0;
  55. }
  56.  
  57. fclose(script_file);
  58. free(script_inmem);
  59. return 1;
  60. }
Success #stdin #stdout 0s 2244KB
stdin
Standard input is empty
stdout
Can't open file