69,371
社区成员
发帖
与我相关
我的任务
分享
// 在main 函数的开头,定义一个字符数组,用来接收所读取文件的每行的末尾残余字符
char empty[100];
//fgetc(fp);
fgets(empty,10,fp);
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char const *argv[])
{
FILE *fp;
fp = fopen("test.xml", "r");
if (NULL == fp) {
printf("Error! File open #1 failed!\n");
exit(EXIT_FAILURE);
}
/*遍历test.xml,得到所需数据的行数,即字符数组数*/
char info_start[10] = "<info>";
char info_end[10] = "</info>";
char line_check[1000];
// 按行来读取,需要额外添加一行来读取行尾换行符
fscanf(fp, "%[^\n]", line_check);
fgetc(fp);
while (0 != strcmp(info_start, line_check) && !feof(fp)) {
fscanf(fp, "%[^\n]", line_check);
fgetc(fp);
}
int parenthesis_count = 0;
fscanf(fp, "%[^\n]", line_check);
fgetc(fp);
while (0 != strcmp(info_end, line_check)) {
char *p = line_check;
while ('\0' != *p) {
if ('(' == *p) {
while (')' != *p && '\0' != *p) {
p++;
}
if (')' == *p) {
parenthesis_count++;
} else if ('\0' == *p) {
printf("Warning : missing ')' !");
}
} else {
p++;
}
}
fscanf(fp, "%[^\n]", line_check);
fgetc(fp);
}
fclose(fp);
// 开始获取需要的字符串
fp = fopen("test.xml", "r");
if (NULL == fp) {
printf("Error! File open #2 failed!\n");
exit(EXIT_FAILURE);
}
// 指针数组,存储各个字符数组的首地址
char *whole[parenthesis_count];
int count = 0;
char search[1000];
fscanf(fp, "%[^\n]", search);
fgetc(fp);
while (0 != strcmp(info_start, search)) {
fscanf(fp, "%[^\n]", search);
fgetc(fp);
}
fscanf(fp, "%[^\n]", search);
fgetc(fp);
while (0 != strcmp(info_end, search)) {
char *p = search;
char *q = search;
while ('\0' != *p) {
p = strchr(p, '(') + 1;
q = p;
p = strchr(p, ')') + 1;
int len = p - q - 1;
char *dst = (char *)malloc(sizeof(char) * (len + 1));
for (int i = 0; i < len; i++) {
dst[i] = *q;
q++;
}
dst[len] = '\0';
whole[count++] = dst;
}
fscanf(fp, "%[^\n]", search);
fgetc(fp);
}
fclose(fp);
for (int i = 0; i < count; i++) {
printf("%s\n", whole[i]);
}
for (int i = 0; i < count; i++) {
free(whole[i]);
whole[i] = NULL;
}
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char const *argv[])
{
FILE *fp;
fp = fopen("test.xml", "r");
if (NULL == fp) {
printf("Error! File open #1 failed!\n");
exit(EXIT_FAILURE);
}
/*遍历test.xml,得到所需数据的行数,即字符数组数*/
char info_start[10] = "<info>";
char info_end[10] = "</info>";
char line_check[1000];
// 按行来读取,需要额外添加一行来读取行尾换行符
fscanf(fp, "%[^\n]", line_check);
fgetc(fp);
while (0 != strcmp(info_start, line_check) && !feof(fp)) {
fscanf(fp, "%[^\n]", line_check);
fgetc(fp);
}
int parenthesis_count = 0;
fscanf(fp, "%[^\n]", line_check);
fgetc(fp);
while (0 != strcmp(info_end, line_check)) {
char *p = line_check;
while ('\0' != *p) {
if ('(' == *p) {
while (')' != *p && '\0' != *p) {
p++;
}
if (')' == *p) {
parenthesis_count++;
} else if ('\0' == *p) {
printf("Warning : missing ')' !");
}
} else {
p++;
}
}
fscanf(fp, "%[^\n]", line_check);
fgetc(fp);
}
fclose(fp);
// 开始获取需要的字符串
fp = fopen("test.xml", "r");
if (NULL == fp) {
printf("Error! File open #2 failed!\n");
exit(EXIT_FAILURE);
}
// 指针数组,存储各个字符数组的首地址
char *whole[parenthesis_count];
int count = 0;
char search[1000];
fscanf(fp, "%[^\n]", search);
fgetc(fp);
while (0 != strcmp(info_start, search)) {
fscanf(fp, "%[^\n]", search);
fgetc(fp);
}
fscanf(fp, "%[^\n]", search);
fgetc(fp);
while (0 != strcmp(info_end, search)) {
char *p = search;
char *q = search;
while ('\0' != *p) {
p = strchr(p, '(') + 1;
q = p;
p = strchr(p, ')') + 1;
int len = p - q - 1;
char *dst = (char *)malloc(sizeof(char) * (len + 1));
for (int i = 0; i < len; i++) {
dst[i] = *q;
q++;
}
dst[len] = '\0';
whole[count++] = dst;
}
fscanf(fp, "%[^\n]", search);
fgetc(fp);
}
fclose(fp);
for (int i = 0; i < count; i++) {
printf("%s\n", whole[i]);
}
for (int i = 0; i < count; i++) {
free(whole[i]);
whole[i] = NULL;
}
return 0;
}
设置可以查找的指针。
//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