读取到TXT文本如何分割然后赋值于另一个数组
1个回答
展开全部
#include <stdio.h>#define MAX_SIZE 3int main(){FILE *fp1,*fp2;fp1 = fopen("./file1","w+");int i,j;int arr[MAX_SIZE][MAX_SIZE];int arr2[MAX_SIZE][MAX_SIZE];//1.向文件中写入数据for(i = 1;i <= MAX_SIZE * 3;i++){fwrite(&i,sizeof(int),1,fp1);}fclose(fp1);fp1 = fopen("./file1","r");//2.读取数据到数组for(i = 0;i < MAX_SIZE;i++){for(j = 0;j < MAX_SIZE;j++){fread(&arr[i][j],sizeof(int),1,fp1);}}fclose(fp1);//3. 排序int tmp,k;for(i = 0;i < MAX_SIZE;i++){for(j = 0;j < MAX_SIZE - 1;j++){for(k = MAX_SIZE - 1;k > j ; k--){if(arr[i][k] > arr[i][k-1]){tmp = arr[i][k-1];arr[i][k-1] = arr[i][k];arr[i][k] = tmp;}}}}for(i = 0;i < MAX_SIZE;i++){for(j = 0;j < MAX_SIZE;j++){printf("val:%x ",arr[i][j]);}printf("\n");}printf("\n");//4.输出到另一个文件fp2 = fopen("./file2","w+");for(i = 0;i < MAX_SIZE;i++){for(j = 0;j < MAX_SIZE;j++){fwrite(&arr[i][j],sizeof(int),1,fp2);}}fclose(fp2);}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询