Advertisement
nguyenhappy92

Nhập xuất mảng 2 chiều bằng file

Oct 31st, 2015
747
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.08 KB | None | 0 0
  1. // Nhap xuat mang bang file
  2. // Khai bao cac ham thu vien neu co
  3. #include<stdio.h>
  4. #include<conio.h>
  5. #include<string.h>
  6. #include<stdlib.h>
  7. #define SIZE 100
  8. #define input "nhap.txt"
  9. #define output "xuat.txt"
  10. void nhap(int A[][SIZE], int &m, int &n);
  11. void xuat(int A[SIZE][SIZE], int m, int n);
  12. void main()
  13. {
  14. int A[SIZE][SIZE],m,n;
  15. nhap(A,m,n);
  16. xuat(A,m,n);
  17. }
  18. // Ham nhap mang 2 chieu
  19. void nhap(int A[][SIZE], int &m, int &n)
  20. {
  21. FILE *fi;
  22. fi = fopen(input, "rt");
  23.  
  24. if (fi == NULL)
  25. {
  26. printf("Khong tim thay file can doc!!!");
  27. exit(1);
  28. }
  29.  
  30. fscanf(fi, "%d%d", &m, &n);
  31. for (int i = 0; i<m; i++)
  32. for (int j = 0; j<n; j++)
  33. fscanf(fi, "%d", &A[i][j]);
  34.  
  35. fclose(fi);
  36. }
  37. // Ham xuat mang 2 chieu
  38. void xuat(int A[SIZE][SIZE], int m, int n)
  39. {
  40. FILE *fo;
  41. fo = fopen(output, "wt");
  42. if (fo == NULL)
  43. {
  44. printf("Khong ghi duoc file!!!");
  45. exit(1);
  46. }
  47. for (int i = 0; i<m; i++)
  48. for (int j = 0; j<n; j++)
  49. fprintf(fo, "%d ", A[i][j]);
  50. fclose(fo);
  51. }
  52. // bay gio chung ta vao noi chua code de tao file nhap.txt.
  53. // nhan Ctrl + F7 bien dich chuong trinh
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement