小编use*_*184的帖子

如何在.c ++中将.txt文件复制到char数组

我试图将整个.txt文件复制到char数组中.我的代码可以工作,但它会遗漏白色空格.因此,例如,如果我的.txt文件读取"I Like Pie"并将其复制到myArray,如果我使用for循环播放我的数组,我会得到"ILikePie"

这是我的代码

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main () 
{
  int arraysize = 100000;
  char myArray[arraysize];
  char current_char;
  int num_characters = 0;
  int i = 0;

  ifstream myfile ("FileReadExample.cpp");

     if (myfile.is_open())
        {
          while ( !myfile.eof())
          {
                myfile >> myArray[i];
                i++;
                num_characters ++;
          }      

 for (int i = 0; i <= num_characters; i++)
      {

         cout << myArray[i];
      } 

      system("pause");
    }
Run Code Online (Sandbox Code Playgroud)

有什么建议?:/

c++ arrays

14
推荐指数
2
解决办法
6万
查看次数

标签 统计

arrays ×1

c++ ×1