C语言读取txt文档中的数据
生活随笔
收集整理的這篇文章主要介紹了
C语言读取txt文档中的数据
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
1.說(shuō)明
???txt文檔中的數(shù)據(jù)格式:前后數(shù)據(jù)用空格隔開;
???數(shù)據(jù)來(lái)源:matlab讀取彩圖的R、G、B三層的像素值,分別存放在三個(gè)txt文檔中,用C讀取到一維數(shù)組。
???動(dòng)態(tài)申請(qǐng)數(shù)組,還是需要預(yù)先知道數(shù)組的大小,比靜態(tài)好的地方是可以釋放內(nèi)存。
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; ? } ?
總結(jié)
以上是生活随笔為你收集整理的C语言读取txt文档中的数据的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 高斯卷积核如何生成 C语言实现
- 下一篇: malloc申请一维动态数组的错误