C语言读取txt文档中的数据
生活随笔
收集整理的這篇文章主要介紹了
C语言读取txt文档中的数据
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1.說明
???txt文檔中的數據格式:前后數據用空格隔開;
???數據來源:matlab讀取彩圖的R、G、B三層的像素值,分別存放在三個txt文檔中,用C讀取到一維數組。
???動態申請數組,還是需要預先知道數組的大小,比靜態好的地方是可以釋放內存。
2.源程序
#include #include
int main(void) {? ????int *img_r = (int*)malloc(352160*sizeof(int)); //要知道圖片大小 ????int i; ????FILE *fpRead = fopen("C:\\Users\\Administrator\\Desktop\\floor_r.txt", "r"); //路徑為桌面 ????if(fpRead == NULL) ? ????{ ? ????????return 0; ? ????} ?
????for(i=0; i<352160; i++) ? ????{ ? ????????fscanf(fpRead, "%d ", &img_r[i]); ? ????????printf("%d ", img_r[i]); ? ????} ? printf("success\n"); ????free(img_r);
????return 1; ? } ?
int main(void) {? ????int *img_r = (int*)malloc(352160*sizeof(int)); //要知道圖片大小 ????int i; ????FILE *fpRead = fopen("C:\\Users\\Administrator\\Desktop\\floor_r.txt", "r"); //路徑為桌面 ????if(fpRead == NULL) ? ????{ ? ????????return 0; ? ????} ?
????for(i=0; i<352160; i++) ? ????{ ? ????????fscanf(fpRead, "%d ", &img_r[i]); ? ????????printf("%d ", img_r[i]); ? ????} ? printf("success\n"); ????free(img_r);
????return 1; ? } ?
總結
以上是生活随笔為你收集整理的C语言读取txt文档中的数据的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 高斯卷积核如何生成 C语言实现
- 下一篇: malloc申请一维动态数组的错误