第1个回答 2014-07-24
#include <iostream>
#include <fstream>
using namespace std;
int main() {
int t = 0, i, temp;
ifstream file("q.txt");
streampos fsize = file.tellg(); // 得到文件位置
file.seekg(0, ios::end); // 定位至文件末端
size_t filesize = file.tellg() - fsize; // 相差值为文件大小
file.seekg(fsize); // 返回文件开头
char* str = new char[filesize + 1]; // 开空间,注意 + 1
file.getline(str, filesize + 1);
cout << str << endl;
for (i = 0; i < strlen(str) + 1; i++) {
if (str[i] == ' ' || str[i] == '\0') {
temp = i;
char *string = new char[temp - t + 1]; // 这里注意 + 1
strncpy(string, str + t, temp - t);
string[temp - t] = '\0'; // 补回null终结符
cout << string << endl;
t = i + 1;
}
}
}本回答被提问者和网友采纳