69,371
社区成员
发帖
与我相关
我的任务
分享
//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: %31[^\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
#include <stdio.h>
#include <string.h>
int main()
{
FILE* pFile;
char* pch;
char str [255] = {0};
pFile = fopen("tel.csv" , "r");
if (pFile == NULL) {
perror("Error opening file");
return -1;
}
fgets(str , 255 , pFile);
while (! feof(pFile)) {
pch = strtok(str, ",\n");
while (pch != NULL) {
printf("%s\t", pch);
pch = strtok(NULL, ",\n");
}
printf("\n");
fgets(str , 255 , pFile); // 这样写法 解决BUG,不会输出最后一行的名字
}
fclose(pFile);
return 0;
}
/* 输出结果
姓名 办电 宅电 手机 虚拟网
卢小珍 34521111 88903453 13537902123 332123
童云龙 34521113 88927528 13503793985 373985
童万贞 34521112 34523377 13803778530 338530
*/
数据文件 tel.csv
姓名,办电,宅电,手机,虚拟网
卢小珍,34521111,88903453,13537902123,332123
童云龙,34521113,88927528,13503793985,373985
童万贞,34521112,34523377,13803778530,338530
卢小珍,34521111,88903453,13537902123,332123
struct person_info {
char name[32];
int age;
char sex[6];
char tel[16];
char address[32];
};
读得时候建议使用fscanf,因为文件数据很有规则;
struct person_info info;
fscanf("%s %d %s %s %s\n", info.name, &info.age, info.sex, info.tel, info.address);
方法可以参考一下;有问题可以继续提出来。
fscanf("%s %d %s %s %s", name, &age, sex, tel, address);
你必须把所有列都一次性读出来,只是某些列不用来显示罢了,此时文件指针会自动往后移。
无意义并且无规则的行,可以用读行的函数读出来后不处理,使文件指针后移。//#include <stdio.h>
#include <stdlib.h>
#include <memory.h>
int main(void)
{
FILE *fp = NULL;
char buf[1024] = {0};
int count = 0;
int row = 0;
int ch = 0;
fp = fopen("host.txt", "r");
if (fp == NULL)
{
printf("Cann't open the file!\n");
exit(1);
}
else
{
while (1)
{
printf("Input a row you want to read: \n");
scanf("%d", &row);
fflush(stdin);
while (!feof(fp))
{
fgets(buf, 1024, fp);
count++;
if (row == count)
{
printf("%s", buf);
}
memset(buf, 0, sizeof(buf));
}
count = 0;
rewind(fp);
printf("go on? [y/n]: ");
scanf("%c", &ch);
fflush(stdin);
if (ch == 'y' || ch == 'Y')
{
continue;
}
else
{
break;
}
}
fclose(fp);
}
return 0;
}
刚在网上看到或者读任意一行的代码~但还是不知道如何把name字段下的东西放进一个组里~啊啊啊啊啊~哪位大神能写个代码啊啊啊啊~抓狂ing ~img=https://forum.csdn.net/PointForum/ui/scripts/csdn/Plugin/003/monkey/50.gif][/img]