c++读取文件中的数据如何跳过字符说明文字

例如:txt文档内容如下:
20070112
time vt vp vi t0 t1
2089 12.34 9.9 4.5 4 90
2098 23.34 44 45 5 98
要跳过前两行说明性的文字,只需读两行文字后的数据,用C++如何读取??

1、先将文件全部读入 char* 变量。再用 string 类 构建函数建一个string 对象,在把 char* 内容放入。然后在缓存内容中删去不要的字符集说明文字。
2、例程:

/* fread example: read an entire file */
#include <stdio.h>
#include <stdlib.h>
int main () {
FILE * pFile;
long lSize;
char * buffer;
size_t result;
pFile = fopen ( "myfile.bin" , "rb" );
if (pFile==NULL) {fputs ("File error",stderr); exit (1);}
// obtain file size:
fseek (pFile , 0 , SEEK_END);
lSize = ftell (pFile);
rewind (pFile);
// allocate memory to contain the whole file:
buffer = (char*) malloc (sizeof(char)*lSize);
if (buffer == NULL) {fputs ("Memory error",stderr); exit (2);}
// copy the file into the buffer:
result = fread (buffer,1,lSize,pFile);
if (result != lSize) {fputs ("Reading error",stderr); exit (3);}
/* the whole file is now loaded in the memory buffer. */
// terminate
fclose (pFile);
free (buffer);
return 0;
}

温馨提示:答案为网友推荐,仅供参考
第1个回答  推荐于2016-06-05
ifstream txtfile("test.txt");
char * c = new char[128];
for(int i = 0 ; i < 2; i++)
{
txtfile.getline(c,999,'\n'); // 读一行
}本回答被提问者采纳
第2个回答  2011-03-30
找到第二个回车换行符之后,后面的内容就是你要的数据了
相似回答