在C++从文本文件逐个读取字符

yakult 2007-08-29 05:18:44
有文本文件in.txt
内容:
abcder
abcdddddd
现想将文件in.txt的内容读出来,放在主函数的数组中,应如何用C++ 语言描述?
...全文
828 5 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
yakult 2007-08-30
  • 打赏
  • 举报
回复
谢谢各位了
believefym 2007-08-29
  • 打赏
  • 举报
回复
#include <stdio.h>
#include <stdlib.h>


int main()
{
FILE* pf = fopen("in.txt","r");
char buffer[80]={0};//数组大小可以设大一点,避免溢出
int i=0;
while (fscanf(pf,"%c",&buffer[i++])!=EOF);

printf("%s\n",buffer);

system("pause");
return 0;
}
karl_max 2007-08-29
  • 打赏
  • 举报
回复
//c++ CODE
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main(int argc, char** argv)
{
string s, str;
ifstream infile("in.txt");
if (!inf)
return -1;

while (getline(infile, s))
{
str += s;
cout << s << endl;
}
//如果想要一个C的数组,可以这样
char * carray = str.c_str();
printf("%s", carray);
return 0;
}
yakult 2007-08-29
  • 打赏
  • 举报
回复
一行放在一个字符数组中,
能否给些语句
taodm 2007-08-29
  • 打赏
  • 举报
回复
看《Effective STL》item 29

65,187

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧