读书人

用VC读入文本文件并取出里面的某些字符

发布时间: 2012-01-29 21:39:32 作者: rapoo

用VC读入文本文件并取出里面的某些字符(有规律的),谢谢!
有这样的文本文件,内容格式是 序号:像素值 (是二值位图的像素信息,序号为像素编号):
0:0 1:0 2:0 3:0 4:0 5:0 6:0 7:0 8:0 9:0 10:1 11:1 12:0 13:0 14:0 15:0 16:0 17:0 18:0 19:0 20:0 21:0 22:0 23:0 24:0 25:0
怎么实现只取出像素值并存为文本文件,而去掉序号和冒号:
0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0
非常感谢啊~

[解决办法]
你可以看看boost中有关字符串处理的函数,象split()和find_all()等。
Two algorithms are provided. find_all() finds all copies of a string in the input. split() splits the input into parts.

string str1( "hello abc-*-ABC-*-aBc goodbye ");

typedef vector < iterator_range <string::iterator> > find_vector_type;

find_vector_type FindVec; // #1: Search for separators
ifind_all( FindVec, str1, "abc " ); // FindVec == { [abc],[ABC],[aBc] }

typedef vector < string > split_vector_type;

split_vector_type SplitVec; // #2: Search for tokens
split( SplitVec, str1, is_any_of( "-* ") ); // SplitVec == { "hello abc ", "ABC ", "aBc goodbye " }

[解决办法]
CString szResult; //结果字符串
CString str= "0:0 1:0 2:0 3:0 4:0 5:0 6:0 7:0 8:0 9:0 10:1 11:1 12:0 13:0 14:0 15:0 16:0 17:0 18:0 19:0 20:0 21:0 22:0 23:0 24:0 25:0 "; //注意分割完成后,str会发生变化
char *p= " "; //以空格作为分割字符
char *Temp;
Temp=new char[str.GetLenth()+1];
strcpy(Temp,str);
str=strtok(Temp,p);
while(str!= " ")
{
if(szResult.IsEmpty())
{
sszResult+=str.Right(1);
}
else
szResult+= " "+str.Rieht(1);
str=strtok(NULL,p);
}
delete Temp;
AfxMessageBox(szResult); //看看结果

-----以上是处理过程,至于文件的读取和写入,自己来
[解决办法]
FILE *fin, *fout;
fin = fopen( "text1.txt ", "r ");
fout = fopen( "text2.txt ", "w ");
int i, j;
while(!feof(fin))
{
fscanf( fin, "%d:%d ", &i, &j );
fprintf(fout, "%d ", j);
};

fclose(fin);
fclose(fout);

读书人网 >VC/MFC

热点推荐