Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/133.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何将.txt文件复制到c++;_C++_Arrays - Fatal编程技术网

C++ 如何将.txt文件复制到c++;

C++ 如何将.txt文件复制到c++;,c++,arrays,C++,Arrays,我正试图将整个.txt文件复制到一个字符数组中。我的代码可以工作,但它省略了空格。例如,如果我的.txt文件读取“I Like Pie”并将其复制到myArray,如果我使用for循环复制数组,则会得到“ILikePie” 这是我的密码 #include <iostream> #include <fstream> #include <string> using namespace std; int main () { int arraysize = 1

我正试图将整个.txt文件复制到一个字符数组中。我的代码可以工作,但它省略了空格。例如,如果我的.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");
    }
#包括
#包括
#包括
使用名称空间std;
int main()
{
int arraysize=100000;
char myArray[arraysize];
字符当前字符;
int num_字符=0;
int i=0;
ifstream myfile(“FileReadExample.cpp”);
如果(myfile.is_open())
{
而(!myfile.eof())
{
myfile>>myArray[i];
i++;
num_字符++;
}      
对于(int i=0;i

您正在逐字读取文件,这会导致跳过空格

您可以使用以下命令将整个文件读入字符串:

std::ifstream in("FileReadExample.cpp");
std::string contents((std::istreambuf_iterator<char>(in)), 
    std::istreambuf_iterator<char>());
std::istreambuf_迭代器
迭代器是从流缓冲区读取连续元素的输入迭代器

std::istreambuf_iterator<char>(in)
std::istreambuf_迭代器(in)
将为
中的
ifstream(文件开头)创建迭代器,如果您不向构造函数传递任何参数,它将创建流结束迭代器(最后位置):

默认构造的std::istreambuf_迭代器称为流结束迭代器。当有效的std::istreambuf_迭代器到达基础流的末尾时,它将等于流结束迭代器。对其进行解引用或递增将进一步调用未定义的行为


因此,这将复制所有字符,从文件中的第一个字符开始,直到下一个字符是流的结尾。

使用以下代码段:

FILE *f = fopen("textfile.txt", "rb");
fseek(f, 0, SEEK_END);
long fsize = ftell(f);
fseek(f, 0, SEEK_SET);

char *string = (char *)malloc(fsize + 1);
fread(string, fsize, 1, f);
fclose(f);

string[fsize] = 0;

这是一个简单的解决方案,如果您必须使用字符数组,并且对代码进行最小的修改。下面的代码段将包含文件结尾之前的所有空格和换行符

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

更简单的方法是使用get()成员函数:

while(!myfile.eof() && i < arraysize)
{
    myfile.get(array[i]); //reading single character from file to array
    i++;
}
while(!myfile.eof()&&i
以下是您需要的代码片段:

#include <string>
#include <fstream>
#include <streambuf>
#include <iostream>


int main()
{
  std::ifstream file("name.txt");
  std::string str((std::istreambuf_iterator<char>(file)),
                        std::istreambuf_iterator<char>());

  str.c_str();

  for( unsigned int a = 0; a < sizeof(str)/sizeof(str[0]); a = a + 1 )
  {
    std::cout << str[a] << std::endl;
  }

  return 0;
}
#包括
#包括
#包括
#包括
int main()
{
std::ifstream文件(“name.txt”);
std::string str((std::istreambuf_迭代器(文件)),
std::istreambuf_迭代器();
str.c_str();
对于(无符号整数a=0;astd::cout arraysize应该是常量。
!myfile.eof()
的使用是不正确的。即使您想读取单词,而不是所有字符,您也在使用
myfile>>myArray[i]
的结果,而没有验证它是否成功,这是不正确的。如果您想读取所有字符,那么在(myfile.get(myArray[i])++i;
可以工作(但仍然需要边界检查).但Nemanja的答案要高得多。这当然是正确的答案,但有两个细节值得指出:首先,如果你想准确地看到文件中的字节,你应该以二进制模式打开它,并确保文件中充满了
“C”
locale;否则,可能会发生各种翻译。其次,如果文件包含空字符,
contents.c_str()
不是解决办法。更一般地说,在这种情况下,我会使用
contents.data()
(但我看不出有任何理由不直接处理字符串)@JamesKanze,谢谢你的建议。我没有使用二进制模式,我建议使用
c_str
,因为我假设OP使用的是ASCII txt文件。不过,谢谢你的评论,因为这是我(和其他人)学习新东西的最好方式(比如“c”语言环境-我不知道它是什么,所以我会查找)。我不清楚OP是想要字符串中文件的精确字节,还是想要字符串的文本表示形式。我的评论是指他想要精确字节的情况;您的代码完全按照编写的那样,可以作为文件中文本的内部表示形式(假设它是文本-,但除此之外,
std::vector
比字符串更有意义。简单一提-在我想起以前在哪里看到这一点之前,我键入了一个很长的问题:“‘额外’括号解决了“最麻烦的解析”。只是修复了它,在fopen()中添加了标志‘b’)调用。
void*
不能直接转换为
char*
(在malloc行上)。这是性能差的定义。很好的演示,对于实用性来说很糟糕,因为逐字节读取确实会对磁盘造成负担。感谢您的反馈,您能为未来的读者提供更好的解决方案吗?您是说这可能很差,还是很差?而且,速度或复杂性都很差?我不太清楚关于硬件层,但我假设编译器会进行一些展开/矢量化,以使性能具有可比性(特别是因为数据只是成对依赖的)一般来说,我在循环中使用“一行”方法,但如果我关心速度,我也会关心幕后虚拟方法的惩罚等等。
while(!myfile.eof() && i < arraysize)
{
    myfile.get(array[i]); //reading single character from file to array
    i++;
}
#include <string>
#include <fstream>
#include <streambuf>
#include <iostream>


int main()
{
  std::ifstream file("name.txt");
  std::string str((std::istreambuf_iterator<char>(file)),
                        std::istreambuf_iterator<char>());

  str.c_str();

  for( unsigned int a = 0; a < sizeof(str)/sizeof(str[0]); a = a + 1 )
  {
    std::cout << str[a] << std::endl;
  }

  return 0;
}