user-0 提问: C语言如何实现从txt中读取特定数据？

回复:
user: user-1
content: 仅供参考：//NAME: essaie bla bla //DIMENSION: 8 //DATA //1  14  15 //2  11  10 //3  6   4 //4  7   13 //5  9   21 //6  19  3 //7  1   5 //8  8   8 //EOF // // 文本文件中可能还含有其他内容，但是需要用到的内容即以上  //比如data.txt: //NAME: essaie bla bla //其它内容 //DIMENSION: 8 //其它内容 //DATA //其它内容 //1  14  15 //其它内容 //2  11  10 //其它内容 //3  6   4 //其它内容 //4  7   13 //其它内容 //5  9   21 //其它内容 //6  19  3 //其它内容 //7  1   5 //其它内容 //8  8   8 //其它内容 //EOF  // 目标是要获取NAME后字串，DIMENSION后数值，以及DATA以下的数值 // 其中NAME就是随便个字句，DIMENSION是城市数量，DATA以下是城市编号，X坐标，Y坐标 // 所有的这些将赋值给一个事先定义好的结构 #include <stdio.h> #include <string.h> #define MAXCPL   80   //每行最大字符数 #define MAXCITY  100  //每组数据中DATA最多项数，DIMENSION的最大值 #define MAXNAMEL 32   //NAME最大长度 struct S {     char NAME[MAXNAMEL+1];     int  DIMENSION;     struct D {         int NO;         int X;         int Y;     } DATA[MAXCITY]; } s; FILE *f; int st,n,i; char ln[MAXCPL]; int main() {     f=fopen("data.txt","r");     if (NULL==f) {         printf("Can not open file data.txt!\n");         return 1;     }     st=0;     n=0;     while (1) {         if (NULL==fgets(ln,MAXCPL,f)) break;         if (st==0) {             if (1==sscanf(ln,"NAME: %32[^\n]",s.NAME)) st=1;         } else if (st==1) {             if (1==sscanf(ln,"DIMENSION: %d",&s.DIMENSION)) st=2;         } else if (st==2) {             if (0==strcmp(ln,"DATA\n")) st=3;         } else if (st==3) {             if (3==sscanf(ln,"%d%d%d",&s.DATA[n].NO,&s.DATA[n].X,&s.DATA[n].Y)) {                 n++;                 if (n>=MAXCITY || n>=s.DIMENSION) break;             }         }     }     fclose(f);     printf("s.NAME=[%s]\n",s.NAME);     printf("s.DIMENSION=%d\n",s.DIMENSION);     for (i=0;i<n;i++) {         printf("s.DATA[%d].NO,X,Y=%d,%d,%d\n",i,s.DATA[i].NO,s.DATA[i].X,s.DATA[i].Y);     }     return 0; } //s.NAME=[essaie bla bla] //s.DIMENSION=8 //s.DATA[0].NO,X,Y=1,14,15 //s.DATA[1].NO,X,Y=2,11,10 //s.DATA[2].NO,X,Y=3,6,4 //s.DATA[3].NO,X,Y=4,7,13 //s.DATA[4].NO,X,Y=5,9,21 //s.DATA[5].NO,X,Y=6,19,3 //s.DATA[6].NO,X,Y=7,1,5 //s.DATA[7].NO,X,Y=8,8,8

user: user-2
content: 引用 15 楼 zhao4zhong1 的回复:To read strings not delimited by space characters, a set of characters in brackets ([ ]) can be substituted for the s (string) type character. The corresponding input field is read up to the first character that does not appear in the bracketed character set. If the first character in the set is a caret (^), the effect is reversed: The input field is read up to the first character that does appear in the rest of the character set.  Note that %[a-z] and %[z-a] are interpreted as equivalent to %[abcde...z]. This is a common scanf function extension, but note that the ANSI standard does not require it.

user: user-1
content: To read strings not delimited by space characters, a set of characters in brackets ([ ]) can be substituted for the s (string) type character. The corresponding input field is read up to the first character that does not appear in the bracketed character set. If the first character in the set is a caret (^), the effect is reversed: The input field is read up to the first character that does appear in the rest of the character set.  Note that %[a-z] and %[z-a] are interpreted as equivalent to %[abcde...z]. This is a common scanf function extension, but note that the ANSI standard does not require it.

user: user-1
content: To read strings not delimited by space characters, a set of characters in brackets ([ ]) can be substituted for the s (string) type character. The corresponding input field is read up to the first character that does not appear in the bracketed character set. If the first character in the set is a caret (^), the effect is reversed: The input field is read up to the first character that does appear in the rest of the character set.  Note that %[a-z] and %[z-a] are interpreted as equivalent to %[abcde...z]. This is a common scanf function extension, but note that the ANSI standard does not require it.

user: user-2
content: 引用 11 楼 zhao4zhong1 的回复:仅供参考：//NAME: essaie bla bla //DIMENSION: 8 //DATA //1  14  15 //2  11  10 //3  6   4 //4  7   13 //5  9   21 //6  19  3 //7  1   5 //8  8   8 //EOF // // 文本文件中可能还含有其他内容，但是需要用到的内容即以上  //比如data.txt: //NAME: essaie bla bla //其它内容 //DIMENSION: 8 //其它内容 //DATA //其它内容 //1  14  15 //其它内容 //2  11  10 //其它内容 //3  6   4 //其它内容 //4  7   13 //其它内容 //5  9   21 //其它内容 //6  19  3 //其它内容 //7  1   5 //其它内容 //8  8   8 //其它内容 //EOF  // 目标是要获取NAME后字串，DIMENSION后数值，以及DATA以下的数值 // 其中NAME就是随便个字句，DIMENSION是城市数量，DATA以下是城市编号，X坐标，Y坐标 // 所有的这些将赋值给一个事先定义好的结构 #include <stdio.h> #include <string.h> #define MAXCPL   80   //每行最大字符数 #define MAXCITY  100  //每组数据中DATA最多项数，DIMENSION的最大值 #define MAXNAMEL 32   //NAME最大长度 struct S {     char NAME[MAXNAMEL+1];     int  DIMENSION;     struct D {         int NO;         int X;         int Y;     } DATA[MAXCITY]; } s; FILE *f; int st,n,i; char ln[MAXCPL]; int main() {     f=fopen("data.txt","r");     if (NULL==f) {         printf("Can not open file data.txt!\n");         return 1;     }     st=0;     n=0;     while (1) {         if (NULL==fgets(ln,MAXCPL,f)) break;         if (st==0) {             if (1==sscanf(ln,"NAME: %32[^\n]",s.NAME)) st=1;         } else if (st==1) {             if (1==sscanf(ln,"DIMENSION: %d",&s.DIMENSION)) st=2;         } else if (st==2) {             if (0==strcmp(ln,"DATA\n")) st=3;         } else if (st==3) {             if (3==sscanf(ln,"%d%d%d",&s.DATA[n].NO,&s.DATA[n].X,&s.DATA[n].Y)) {                 n++;                 if (n>=MAXCITY || n>=s.DIMENSION) break;             }         }     }     fclose(f);     printf("s.NAME=[%s]\n",s.NAME);     printf("s.DIMENSION=%d\n",s.DIMENSION);     for (i=0;i<n;i++) {         printf("s.DATA[%d].NO,X,Y=%d,%d,%d\n",i,s.DATA[i].NO,s.DATA[i].X,s.DATA[i].Y);     }     return 0; } //s.NAME=[essaie bla bla] //s.DIMENSION=8 //s.DATA[0].NO,X,Y=1,14,15 //s.DATA[1].NO,X,Y=2,11,10 //s.DATA[2].NO,X,Y=3,6,4 //s.DATA[3].NO,X,Y=4,7,13 //s.DATA[4].NO,X,Y=5,9,21 //s.DATA[5].NO,X,Y=6,19,3 //s.DATA[6].NO,X,Y=7,1,5 //s.DATA[7].NO,X,Y=8,8,8  第72行  if (1==sscanf(ln,"NAME: %32[^\n]",s.NAME)) st=1; 不是 sscanf(ln,"NAME: %32[^\n]s",s.NAME)

user: user-3
content: 引用 10 楼 zxh707wk 的回复:fscanf(fp,"%s %d",ch,&num);  +++++++++++++++++++++++++++++++++++

user: user-4
content: 1. fgets 2. strchr 3. atoi

user: user-0
content: #include <stdio.h> #include <windows.h>  int main(void) { 	char name[20]; 	char id[20]; 	char name_1[20]; 	char id_1[20]; 	FILE *fp; 	fp = fopen("ceshi.txt", "w"); 	gets(name); 	gets(id); 	fputs(name, fp); 	fprintf(fp, " %s", id); 	fclose(fp); 	fp = fopen("ceshi.txt", "r"); 	fscanf(fp, "%s %s", name_1, id_1); 	fclose(fp); 	printf("name = %s\nid = %s\n", name_1, id_1); 	system("pause"); 	return 0; } 最后用这个实现了，请忽略文件打开检查等 还是这个比较方便和简单，谢谢大家

user: user-5
content: fscanf(fp,"%s %d",ch,&num);

user: user-6
content: http://blog.csdn.net/hong__fang/article/details/43488265 可以直接获取空格间的字符串，数字可以以字符串的方式读取，然后转化为数字。

user: user-2
content: 引用 4 楼 zhangxiangDavaid 的回复:这样可好：  /* abcdef 123456//字母和数字之间用空格相分割 那么如何把abcdef和123456从文件中分别读入到 */ #include <stdio.h> #include <string.h> typedef struct data { 	char ch[20]; 	int num; }Data; FILE *f; #define MAXLINE 1000 static Data ds[MAXLINE]; char buf[100]; char ch[20]; int i, r, num; int main(void) { 	if (NULL == (f = fopen("1.txt", "r"))) 	{ 		fprintf(stderr, "Can not open file!\n"); 		return 1; 	} 	i = 0; 	while (1) 	{ 		if (NULL == fgets(buf, 100, f))break; 		if ('\n' == buf[0]) continue;  		r = sscanf(buf, "%20s%d", ch, &num); 		if (2 == r) 		{ 			strcpy(ds[i].ch, ch); 			ds[i].num = num; 			i++; 		} 	} 	fclose(f); 	for (r = 0; r < i; r++) 		printf("%s %d\n", ds[r].ch, ds[r].num); 	return 0; } //1.txt //David 1111 //zhangxiang 2222 //lili 3333 //lalala 4444  这种做法比较标准

user: user-7
content: int main(int argc, char *argv[]) { 	FILE *fp; 	fp=fopen("E:\\file\\1.txt","r"); 	if(fp==NULL){ 		printf("打不开文件！！\n"); 		return; 	} 	char buf[50],str[50]; 	int num=0;//str保存abcdef num保存12345 	fgets(buf,50,fp); 	fclose(fp);  	spilit_str(buf,str,&num);//把数据存入到str和num中  	printf("str:%s\nnum:%d\n",str,num); 	return 0; } void spilit_str(char *buf,char *str,int *num){ 	char temp[20]; 	int i=0,j=0,z=0,flag=0; 	char ch; 	while((ch=buf[i++])!='\0'){ 		if(ch==' ')flag=1; 		if(!flag){ 			str[j++]=ch; 		}else if(ch!=' '){ 			temp[z++]=ch; 		} 	} 	str[j]='\0'; 	temp[z]='\0'; 	chang_number(temp,num); } //将数字字符转成数字 "1234" ->1234 void chang_number(char *str,int *num){ 	int base,i=0,j=0; 	for(i=0;i<strlen(str);i++){ 		base=1; 		for(j=i+1;j<strlen(str);j++){ 			base=base*10; 		} 		*num+=(str[i]-'0')*base; 	} }

user: user-2
content: 这样可好：  /* abcdef 123456//字母和数字之间用空格相分割 那么如何把abcdef和123456从文件中分别读入到 */ #include <stdio.h> #include <string.h> typedef struct data { 	char ch[20]; 	int num; }Data; FILE *f; #define MAXLINE 1000 static Data ds[MAXLINE]; char buf[100]; char ch[20]; int i, r, num; int main(void) { 	if (NULL == (f = fopen("1.txt", "r"))) 	{ 		fprintf(stderr, "Can not open file!\n"); 		return 1; 	} 	i = 0; 	while (1) 	{ 		if (NULL == fgets(buf, 100, f))break; 		if ('\n' == buf[0]) continue;  		r = sscanf(buf, "%20s%d", ch, &num); 		if (2 == r) 		{ 			strcpy(ds[i].ch, ch); 			ds[i].num = num; 			i++; 		} 	} 	fclose(f); 	for (r = 0; r < i; r++) 		printf("%s %d\n", ds[r].ch, ds[r].num); 	return 0; } //1.txt //David 1111 //zhangxiang 2222 //lili 3333 //lalala 4444

user: user-8
content: 可以自己读文件处理

user: user-9
content: 参考代码如下：  #include <stdio.h> #include <stdlib.h>  void main (void) { 	char ch1[20]={'\0'}; 	char ch2[20]={'\0'}; 	FILE *fp; 	if((fp=fopen("test.txt","r"))==NULL) //test.txt 为你存数据的文件 	{ 		printf("initalization failed !\n"); 		exit(1); 	} 	int i=0; 	for(ch1[i]=fgetc(fp);ch1[i]!=' ';i++,ch1[i]=fgetc(fp)); 	puts(ch1); 	for(i=0,ch2[i]=fgetc(fp);!feof(fp);i++,ch2[i]=fgetc(fp)); 	puts(ch2); 	fclose(fp); }

user: user-10
content: fgets()然后fscanf()