10分 |
/************************************** *FILE :D:\c\main.c *PROJECT :NULL *AUTHOR :707wk *CREATED :2015-5-13 下午 09:41:42 *TEXT :NULL *EMAIL :gtsoft_wk@foxmail.com *CODE :https://github.com/707wk ***************************************/ #include <stdio.h> typedef struct node { int num[5]; }DATA; int main() { int i; int j; int len; char ch; FILE* fpin; DATA num[100]; fpin=fopen("input.txt","r"); if(fpin==NULL)return 0; for(len=0;!feof(fpin);len++) { fscanf(fpin,"%d",&num[len].num[0]); for(i=1;!feof(fpin);i++) { if((ch=fgetc(fpin))==""\n"")break; if(feof(fpin))break; fscanf(fpin,"%d",&num[len].num[i]); } if(i!=5)len--; } for(i=0;i<len;i++) { for(j=0;j<5;j++) printf("%d ",num[i].num[j]); printf("\n"); } return 0; } |
看不太懂啊,能否来个注释版?而且代码运行出错了啊 |
|
抱歉刚才工程建错了,运行没问题,但还是想要个注释版看看,代码有些看不懂 |
|
10分 |
作为一个C程序员,对
scanf,sscanf,fscanf printf,sprintf,fprintf 这类函数的用法,还是要做到“拳不离手,曲不离口”的。 #include <stdio.h> #define MAXN 100 int main() { FILE* f; static d[MAXN][5]; char ln[100]; int i,n,j; f=fopen("input.txt","r"); if (f==NULL) return 1; i=0; while (1) { if (NULL==fgets(ln,100,f)) break; if (5==sscanf("%d%d%d%d%d",&d[i][0],&d[i][1],&d[i][2],&d[i][3],&d[i][4])) i++; if (i>=MAXN) break; } fclose(f); n=i; for (i=0;i<n;i++) { for(j=0;j<5;j++) { printf("%d ",d[i][j]); } printf("\n"); } return 0; } |
第5行
static d[MAXN][5]; 应改为 static int d[MAXN][5]; |
|
赵老师,我稍微明白点了,但是如果数组列数比5列多很多呢比如有128列,就不能用这种方式吧 if (5==sscanf("%d%d%d%d%d",&d[i][0],&d[i][1],&d[i][2],&d[i][3],&d[i][4])) i++; 又没有什么好的解决方法呢? |
|
10分 |
如果有的行超过5个数据,可以这样:
#include <ctype.h> #define LINE 100 FILE *f; int data[LINE][5]; int i, j, flag, l, n; char buf[256]; //一行255个字符够吗? int count_int() //获取一行中的数字个数 { n = strlen(buf); if (""\n"" == buf[0]) return 0; //空行 buf[n - 1] = ""\0""; //去回车符 j = 0; flag = 0; for (i = 0; i < n - 1; i++) { if (isdigit(buf[i])) flag = 1; else { if (1 == flag) j++; flag = 0; } } if (isdigit(buf[n - 2])) j++; return j; } int main(void) { if (NULL == (f = fopen("1.txt", "r"))) { fprintf(stderr, "Can not open file : 1.txt\n"); return 2; } l = 0; while (1) { if (l == LINE) break; if (NULL == fgets(buf, 256, f)) break; if (5 != (count_int())) continue; sscanf(buf, "%d%d%d%d%d", data[l] + 0, data[l] + 1, data[l] + 2, data[l] + 3, data[l] + 4); l++; } for (i = 0; i < l; i++) printf("%d %d %d %d %d\n", data[i][0], data[i][1], data[i][2], data[i][3], data[i][4]); return 0; } //1 3 34 4 98 //13 21 213 234 45 //1 2 3 4 5 //12 34 56 78 9 //2 3 4 5 6 //9 8 7 6 5 |
一个个拆 别想那么复杂
|
|
活人永远不会被尿憋死! #include <stdio.h> char s[]="123 ab 4"; char *p; int v,n,k; void main() { p=s; while (1) { k=sscanf(p,"%d%n",&v,&n); printf("k,v,n=%d,%d,%d\n",k,v,n); if (1==k) { p+=n; } else if (0==k) { printf("skip char[%c]\n",p[0]); p++; } else {//EOF==k break; } } printf("End.\n"); } //k,v,n=1,123,3 //k,v,n=0,123,3 //skip char[ ] //k,v,n=0,123,3 //skip char[a] //k,v,n=0,123,3 //skip char[b] //k,v,n=1,4,2 //k,v,n=-1,4,2 //End. |
|
10分 |
只能当例子。
# include <stdio.h> # include <string.h> # include <ctype.h> int number_in_line(char s[1000], int a[100]) { int n = 0; int i = 0; while ((sscanf(s + i, "%d", a + n)) == 1 && n < 100) { while (isspace(s[i])) i++; while (!isspace(s[i])) i++; n++; } return n; } void show(int a[100], int n) { int i; for (i = 0; i < 100 && i < n; i++) printf("%d\n", a[i]); } int main() { char line[1000]; int a[100]; int n; while (1) { strcpy(line, ""); if (!fgets(line, 1000, stdin)) break; n = number_in_line(line, a); if (n == 5) show(a, 5); } return 0; } |