您的位置:首页 > 编程语言

将一个txt文档中所有字符读入一个数组中的代码

2008-01-31 23:42 337 查看
FILE* f = fopen("filename", "rb");
// get file length
fseek(f, SEEK_END, 0);
const int len = ftell(f);
fseek(f, SEEK_SET, 0);

// read file
char* buf = new char[len + 1];
fread(buf, 1, len, f);
fclose(f);

buf[len] = 0;

// remove /r and /n
char* buf_1 = buf;
char* buf_2 = buf;
while (*buf_1 != '/0') {
if (*buf_1 != '/r' && *buf_1 != '/n')
*(buf_2++) = *buf_1;
buf_1 ++;
} // while
*buf_2 = '/0';
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐