【问题标题】:Copy file content to a string将文件内容复制到字符串
【发布时间】:2014-06-11 02:26:22
【问题描述】:

我想将文本文件中的内容复制到string*char。如果我可以将文件内容复制到字符串的array 会更好(每一行都是那个array 的元素)。这是我的代码:

int main() {
   ifstream inFile;
   inFile.open("index.in.txt"); //index.in has in each line a name and at the end there is a "."
   char ab[11];
   int q=0;
   char *a[111];
   if (inFile.is_open()) {
      while (!inFile.eof()) {
         inFile >> ab; //i think i don't understand this line correctly
         a[q]=ab;
         cout<<a[q]<<endl;
         q++;
      }
   }
   else{ 
     cout<<"couldnt read file";
   }
   inFile.close();
   cout<<"\n"<<ab<<endl; //it shoud be "." and it is
   cout<<"\n"<<a[0]<<endl; //it should be "ion" but it gives me "."
   return 0;
}

array 中的所有值都等于最后一行,即点

【问题讨论】:

  • 检查this
  • 您应该使用std::vector&lt;std::string&gt; 而不是字符指针数组。另外,您希望每一行都在自己的字符串中,还是每个单词中?
  • 每一行只有一个单词,所以对我来说没什么区别
  • @MohitJain 谢谢我会检查的
  • 不要这样做while (!inFile.eof()),它不会像你期望的那样工作,并且会导致你迭代一次到多次。而是做while (inFile &gt;&gt; ab)。原因是eofbit 标志直到您尝试从文件末尾读取之后才设置。

标签: c++ arrays fstream ifstream


【解决方案1】:
int main() {
   ifstream inFile;
   inFile.open("index.in.txt"); //index.in has in each line a name and at the end there is a "."
   std::vector< std::string > lines;
   std::string line;
   if (inFile.is_open()) {
      while ( getline( inFile, line ) ) {
         lines.push_back( line );
      }
   }
...

现在lines 是一个字符串向量,每个都是文件中的一行

【讨论】:

  • 谢谢,这对我来说比较复杂,但是,我应该学习:)
【解决方案2】:

您每次在inFile &gt;&gt; ab; 中都会覆盖您唯一的缓冲区 您在缓冲区中读取一行并将缓冲区的地址保存在某处。下次您在同一缓冲区中读取下一行并保存与第二行完全相同的地址。如果您回读第一行,您最终将读取更新的缓冲区,即最后一行。

您可以将代码更改为

#include <vector>
#include <string>
#include <fstream>

using namespace std;
int main() {
   ifstream inFile;
   inFile.open("index.in.txt"); //index.in has in each line a name and at the end there is a "."
   string ab; //char ab[11];
   int q=0;
   vector< string > a(111); //char *a[111];
   if (inFile.is_open()) {
      while (!inFile.eof()) {
         inFile >> ab;
         a[q]=ab;
         cout<<a[q]<<endl;
         q++;
      }
    }
    else cout<<"couldnt read file";
    inFile.close();
    cout<<"\n"<<ab<<endl; //it shoud be "." and it is
    cout<<"\n"<<a[0]<<endl; //it should be "ion" but it gives me "."
    return 0;
}

最好使用 std::string 和 std::vector 代替数组。

【讨论】:

  • std::string 和 std::vectors 是负责内存管理的智能容器。因此,每次将字符串分配给vector[idx] 时,都会为vector[idx] 创建一个新缓冲区。有关更多详细信息,您可以阅读c-dynamic memory management malloc 等,C++ 动态内存,即 new/delete,然后是基本 STL(std::string、std::vector、std::list、std::set、std::map 等.
  • @WhozCraig 我从一个站点复制了代码,然后将其用于字符数组而不是 *char
  • @MohitJain 所以我想如果想用数组和 *chars 来做,那么我必须手动处理内存分配,嗯...
  • 您需要在代码中使用 malloc/new 和 free/delete 来处理内存分配。或 char *a[111];需要替换为a[111][11],需要直接读入a。
【解决方案3】:
#include <iostream>
#include <fstream>
#include <vector>
#include <string>

bool ReadFile(const std::string &sFileName, 
              std::vector<std::string> &vLines)
{
    std::ifstream ifs(sFileName);
    if (!ifs)
        return false;
    std::string sLine;
    while (std::getline(ifs, sLine))
        vLines.push_back(sLine);
    return !ifs.bad();
}

int main() 
{
    const std::string sFileName("Test.dat");
    std::vector<std::string> vData;
    if (ReadFile(sFileName, vData))
        for (std::string &s : vData)
            std::cout << s << std::endl;
    return 0;
}

【讨论】:

    【解决方案4】:

    为了阅读一行你必须使用std::getline&gt;&gt; 运算符只会读取 word,即以空格结尾的字符序列。

    见:http://en.cppreference.com/w/cpp/string/basic_string/getline

    【讨论】:

      猜你喜欢
      • 2010-10-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-07
      • 1970-01-01
      • 2020-08-21
      • 2011-01-25
      相关资源
      最近更新 更多