1.在用C語言中,如何將一個二維數組存儲到文本文件中fwrite用于寫2進制文件。用fprintf就可以了。inta[4][5]={
fwrite 用于寫 2 進制文件。用 fprintf 就可以了。
int a[4][5]={1 ,2,2,3,4,2,4,7,9,0,5,6,7,8,3,5,8,6,2,1};
FILE *fout;
int i,j;
fout = fopen("abc.txt","w"); // 打開文件,文本輸出文件
for (j=0;j<4;j++){
for (i=0;i<5;i++){ fprintf(fout,"%d ",a[j][i]);} // 寫
fprintf(fout,"\n");
}
fclose(fout); //關閉
//---------------------------------------------------------------------------#include
printf("%d\n",b[i]); } return 0;}//---------------------------------------------------------------------------。
int main()
{
int a[2][3] = {2,4,4,2,3,4};//比如說你要把它存到一個文件中。
//現在你要現在你的源程序的目錄下建立一個比如說是
data.txt文件
//現在開始寫程序
FILE *fp;
if ((fp = fopen("data.txt","w"))==NULL)
{
printf("the file can not open..");
exit(0);
}//出錯處理
for(int i = 0;i < 2;i++)
for(int j= 0;j < 3;j++)
fprintf(fp,"%d",a[i][j]);//把每個數組元素以十進制的方式存入data.txt中
fclose(fp);//OK就這么簡單
return 0;
}
1. fopen函數打開需要讀取的文本,獲取該文件的文件描述符
2. 使用fscan()函數對文件進行讀取。
3. 放到二維數組其實就是讀取相應格式的數據,然后對應二維數組的每個位置
例如
#include "stdio.h"
#define M 300
#define N 50
肇慶學院void main()
{
int i,j;
float a[M][N]={0};
FILE *fp;
if((fp=fopen("test.txt","rt"))==NULL)
{
printf("cannot open file\n");
return;
}
for(i=0;i
用for循環寫入每個元素,
/*寫了個實例參考下,這是個字符串數組其他的同樣道理*/ #include"stdio.h" #define MAX 1000 void main() {FILE *fp; int i=0; char s[MAX]; puts("please input:"); scanf("%s",s); fp=fopen("001.txt","w"); while(s[i]!='\0') {fprintf(fp,"%c",s[i]) ; i++; } fclose(fp); }
不管什么數組道理是一樣的不過在加個循環用2個for循環而已,你意思從文本中讀還是寫?
#include <stdio.h>
main()
{
int i;
char ss[2][4]={"abc","def"};
FILE *fp=fopen("data.txt","w");
for (i=0;i<2;i++)
{
fputs(ss[i],fp);
}
fclose(fp);
}
標簽:端州新聞,in
本文僅代表作者觀點,不代表陽光網立場。
如轉載內容涉及版權、名譽權等,請盡快與本網聯系
上一篇:12萬的積蓄,買什么車比較好?
下一篇:css如何將文字拉伸