Pushok

Untitled

May 13th, 2014
437
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.04 KB | None | 0 0
  1. bool TryReadFile(int**m, int &sizea, int &sizeb, char* path)
  2. {
  3. FILE* f = fopen(path, "r");
  4. if (f)
  5. {
  6. fscanf(f, "%d\n", &sizea);
  7. if (sizea <= MAX_S)
  8. {
  9. fscanf(f, "%d\n", &sizeb);
  10. if(sizeb <= MAX_S)
  11. {
  12. int counter = 0;
  13. for (int i = 0; i < sizea && !feof(f); ++i)
  14. {
  15. for (int j = 0; j < sizeb && !feof(f); ++j)
  16. {
  17. fscanf(f, "%d", &m[i][j]);
  18. ++counter;
  19. }
  20.  
  21. fscanf(f, "\n");
  22. }
  23.  
  24. fclose(f);
  25. return counter == sizea*sizeb;
  26. }
  27. }
  28.  
  29. fclose(f);
  30. }
  31. return false;
  32. }
  33.  
  34. bool WriteMatrixToFile(int **m, int &sizea, int& sizeb, char* path)
  35. {
  36. FILE *f = fopen(path, "w");
  37. if (f)
  38. {
  39. fprintf(f, "%d\n", sizea);
  40. if (sizea <= MAX_S)
  41. {
  42. fprintf(f, "%d\n", sizeb);
  43. if(sizeb <= MAX_S)
  44. {
  45. int counter = 0;
  46. for (int i = 0; i < sizea && !feof(f); ++i)
  47. {
  48. for (int j = 0; j < sizeb && !feof(f); ++j)
  49. {
  50. fprintf(f, "%d", m[i][j]);
  51. ++counter;
  52. }
  53.  
  54. fprintf(f, "\n");
  55. }
  56. }
  57. }
  58.  
  59. fclose(f);
  60. }
  61. return false;
  62. }
Advertisement
Add Comment
Please, Sign In to add comment