64,644
社区成员
发帖
与我相关
我的任务
分享
ZXZ1 ZXZ1 ICE NULL IC cmd 纽元/美元11 69
ZXZ2 ZXZ2 ICE NULL IC cmd 纽元/美元12 69
ZXZ3 ZXZ3 ICE NULL IC cmd 纽元/美元13 69
ZXZ4 ZXZ4 ICE NULL IC cmd 纽元/美元14 69
ZXZ5 ZXZ5 ICE NULL IC cmd 纽元/美元15 69
ZXZ6 ZXZ6 ICE NULL IC cmd 纽元/美元16 69
struct node
{
CString Acode; //ZXZ6
CString Bcode; //ZXZ6
CString Ccode; //ICE
CString Dcode; //NULL
CString Ecode; //IC
CString Fcode; //cmd
CString Gcode; //纽元/美元16
int Hcode; //69
};
if( file.Open( pszFile, CFile::modeRead | CFile::shareDenyWrite ) )
{
while( file.ReadString( strData ) )
{
strField = strData.Tokenize("\t", iStart );
while( strField.GetLength() )
{
switch( index )
{
case 0:
symbolData.Acode = strField;
break;
case 1:
symbolData.Bcode = strField;
break;
case 2:
symbolData.Ccode = strField;
break;
case 3:
symbolData.Dcode = strField;
break;
case 4:
symbolData.Ecode = strField;
break;
case 5:
symbolData.Fcode = strField;
break;
case 6:
symbolData.Gcode = strField;
break;
case 7:
symbolData.Hcode = atoi( strField );
break;
}
++index;
strField = strData.Tokenize("\t", iStart );
}
m_vctSymbolDataArr.push_back( symbolData );
......
}
file.Close();
}
//输出PROG中有但LIST中没有的文本行,即集合PROG-LIST
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <search.h>
#define MAXCHARS 512
int MAXLINES=10000,MAXLINES2;
char *buf,*buf2;
char PROG[256]="PROG";//程序Program需要的文件列表
char LIST[256]="LIST";//dir /b /s生成的实际文件列表List
FILE *fp,*fl;
int i,c,n,L,hh;
int ignore_case=0;
char ln[MAXCHARS];
int icompare(const void *arg1,const void *arg2) {
return stricmp((char *)arg1,(char *)arg2);
}
int compare(const void *arg1,const void *arg2) {
return strcmp((char *)arg1,(char *)arg2);
}
int main(int argc,char **argv) {
if (argc>1) strcpy(PROG,argv[1]);//命令行参数1覆盖PROG
if (argc>2) strcpy(LIST,argv[2]);//命令行参数2覆盖LIST
if (argc>3) ignore_case=1;//若存在命令行参数3,忽略大小写
if ((fl=fopen(LIST,"rt"))==NULL) {
fprintf(stderr,"Can not open %s\n",LIST);
fprintf(stderr,"Usage: %s [PROG] [LIST] [-i]\n",argv[0]);
return 1;
}
if ((fp=fopen(PROG,"rt"))==NULL) {
fclose(fl);
fprintf(stderr,"Can not open %s\n",PROG);
fprintf(stderr,"Usage: %s [PROG] [LIST] [-i]\n",argv[0]);
return 2;
}
buf=(char *)malloc(MAXLINES*MAXCHARS);
if (NULL==buf) {
fclose(fl);
fclose(fp);
fprintf(stderr,"Can not malloc(%d LINES*%d CHARS)!\n",MAXLINES,MAXCHARS);
return 4;
}
n=0;
hh=0;
i=0;
while (1) {
if (fgets(ln,MAXCHARS,fl)==NULL) break;//
hh++;
L=strlen(ln)-1;
if ('\n'!=ln[L]) {//超长行忽略后面内容
fprintf(stderr,"%s Line %d too long(>%d),spilth ignored.\n",LIST,hh,MAXCHARS);
while (1) {
c=fgetc(fl);
if ('\n'==c || EOF==c) break;//
}
}
while (1) {//去掉行尾的'\n'和空格
if ('\n'==ln[L] || ' '==ln[L]) {
ln[L]=0;
L--;
if (L<0) break;//
} else break;//
}
if (L>=0) {
strcpy(buf+i,ln);i+=MAXCHARS;
n++;
if (n>=MAXLINES) {
MAXLINES2=MAXLINES*2;
if (MAXLINES2==1280000) MAXLINES2=2500000;
buf2=(char *)realloc(buf,MAXLINES2*MAXCHARS);
if (NULL==buf2) {
free(buf);
fclose(fl);
fclose(fp);
fprintf(stderr,"Can not malloc(%d LINES*%d CHARS)!\n",MAXLINES2,MAXCHARS);
return 5;
}
buf=buf2;
MAXLINES=MAXLINES2;
}
}
}
fclose(fl);
if (ignore_case) qsort(buf,n,MAXCHARS,icompare);
else qsort(buf,n,MAXCHARS,compare);
hh=0;
while (1) {
if (fgets(ln,MAXCHARS,fp)==NULL) break;//
hh++;
L=strlen(ln)-1;
if ('\n'!=ln[L]) {//超长行忽略后面内容
fprintf(stderr,"%s Line %d too long(>%d),spilth ignored.\n",PROG,hh,MAXCHARS);
while (1) {
c=fgetc(fp);
if ('\n'==c || EOF==c) break;//
}
}
while (1) {//去掉行尾的'\n'和空格
if ('\n'==ln[L] || ' '==ln[L]) {
ln[L]=0;
L--;
if (L<0) break;//
} else break;//
}
if (L>=0) {
if (ignore_case) {
if (NULL==bsearch(ln,buf,n,MAXCHARS,icompare)) printf("%s\n",ln);
} else {
if (NULL==bsearch(ln,buf,n,MAXCHARS,compare)) printf("%s\n",ln);
}
}
}
fclose(fp);
free(buf);
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
#include <iostream>
#include <string.h>
#include <stdio.h>
using namespace std;
typedef char* c_string ;
int loadfile_strid(FILE* in , c_string* ps_strid , int max);
int main()
{
char InstrumentID[7][32];
c_string ppInstrumentID[7];
for (int i = 0; i != 7 ; ++i) {
ppInstrumentID[i] = InstrumentID[i];
}
// 下面来实现从文件中读取配置
FILE* in = fopen("strid.txt" , "r");
int readflag = loadfile_strid(in , ppInstrumentID , 7);
if (readflag == 7) {
cout << "这行有数据" << endl;
}
readflag = loadfile_strid(in , ppInstrumentID , 7);
if (readflag == -1)
cout << "这行注解" << endl;
readflag = loadfile_strid(in , ppInstrumentID , 5);
if (readflag > 0) {
c_string* it = ppInstrumentID; // auto 自动判别类型 char**
c_string* end = ppInstrumentID + 5;
while (it != end) // 迭代器遍历输出
cout << *it++ << endl;
}
fclose(in);
return 0;
}
// 从配置文件 in 里读取数据,存放到c_string* ps_strid 指向空间,max是一行中数据数量
int loadfile_strid(FILE* in , c_string* ps_strid , int max)
{
// 读取一行
char line[512] = {0};
fgets(line, 512, in);
if (strstr(line, "#"))
return -1; // 注解行 返回 -1
int index = 0;
char* pch;
// 切割后,然后复制到指针指向分配的空间的地址
pch = strtok(line, " ,:;-");
while (pch != NULL && max--) {
strcpy(ps_strid[index] , pch);
pch = strtok(NULL, " ,:;-");
index++;
}
return index; // 没有切割返回0 , 可以判断切换后数量是否正确
}
/* filename: strid.txt 分割符号 " ,:;-"
Panzer , 蘭公子二世 : 秦.狮王 ; 似水無痕 - kerou H0rol4 GM
# 注解行 ; 似水無痕 - kerou H0rol4 GM Panzer , 蘭公子二世 :
秦.狮王 ; 似水無痕 - kerou H0rol4 GM Panzer , 蘭公子二世 :
*/