|

楼主 |
发表于 2020-2-24 11:57:37
|
显示全部楼层
终于搞定了!谢谢你们!!!
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- #define LINE_SIZE 100 //行存储大小
- FILE *fp(char *,const char *);
- int check_width(FILE *fp);
- int check_high(FILE *fp);
- int main(int argc,char *argv[])
- {
-
- FILE *a_txt=fp(argv[1],"r");
- FILE *b_txt=fp(argv[2],"w");
-
- int width=0,high=0;
- width=check_width(a_txt);
- high=check_high(a_txt);
-
- void anay(char *,char *,int linesize,int width); //把二维素组做一维数组传递
- void init(char *,int *,int linesize,int width); //把二维素组做一维数组传递
- char mu[LINE_SIZE]="0"; //储存第一行数据
- char temp_1[high][width]={"0"}; //存储分割好的数据
- char temp[LINE_SIZE]="0"; //存储一行数据
- int arr[high][width]={0}; //存储最终数组
- int (*p_arr)[width]=arr;
-
- rewind(a_txt);
- fgets(mu,LINE_SIZE,a_txt);
- fprintf(b_txt,"%s",mu);
- int i;
- for (i=0;fgets(temp,LINE_SIZE,a_txt)!=NULL;i++)
- {
- anay(temp,temp_1[0],width,width); //将数据分割成二维字符串
- init(temp_1[0],arr[i],width,width); //将二维字符串赋值给二维数组
- //for (int i=0;i<5;i++) printf("%d ",arr[n][i]); //这里打印没有问题
- }
- //for (int i=0;i<5;i++) printf("%s ",temp_1[i]); //这里打印没有问题
- //printf("%d",arr); //到这里就访问数据就错了
- //return 0;
- p_arr=arr;
- int j;
- for (i=0;i<high;i++)
- {
- for (j=0;j<width;j++)
- {
- printf("%d ",*(*(p_arr+i)+j));
- }
- putchar('\n');
- }
- fclose(a_txt);
- fclose(b_txt);
- return 0;
-
-
- }
- FILE *fp(char *p,const char *mode)
- {
- FILE *pp;
- if ((pp= fopen(p,mode))==NULL)
- {
- printf("open fail! %s",p);
- getchar();
- exit (0);
- }
- return pp;
- }
- //检查数据
- int check_width(FILE *fp)
- {
- rewind(fp);
- char delims='0';
- int w=1;
- for (;(delims=fgetc(fp))!='\n';)
- {
- if (delims==' ') w++;
- }
- return w;
- }
- int check_high(FILE *fp)
- {
- rewind(fp);
- int h=0;
- char arr[LINE_SIZE];
- for (;fgets(arr,LINE_SIZE,fp)!=NULL;h++);
- return h-1; //去掉第一行不算
- }
- void anay(char *p,char *t,int linesize, int width)
- {
- int i,j=0,k=0; //j第二维,k第一维
- for (i=0;*(p+i)!='\0';i++)
- {
- if (*(p+i)!=' ' && *(p+i)!='\n')
- {
- *(t+j*linesize+k)=*(p+i);
- k++;
- }
- else
- {
- *(t+j*linesize+k)='\0';
- j++,k=0;
- }
- }
- }
- void init(char *p,int *p1,int linesize,int width)
- {
- int i;
- for (i=0;i<width;i++)
- {
- *(p1+i)=atoi(p+i*linesize);
- }
- }
复制代码 |
|