【问题标题】:Reading in a text file from a command line and storing the content of the file into a c-string从命令行读取文本文件并将文件内容存储到 c 字符串中
【发布时间】:2014-10-06 17:00:24
【问题描述】:

我正在我的一门课程中学习 C++,但在将 .txt 文件的内容存储到 c 字符串中时遇到了困难。

我已经弄清楚如何验证 .txt 文件是否存在,但是当我尝试将字符存储到 c 字符串中时它会崩溃。

这是我最近的尝试:

char * fileContent[MAX_SIZE];
ifstream ifile(argv[1]);
while (int i = 0 < MAX_SIZE)
{
    ifile >> fileContent[i];
    cout << fileContent[i];
    if (ifile.eof())
        break;
    i++;
}
ifile.close();

每次控制台进入循环时都会崩溃。有什么建议可以帮助完成这项工作吗?

我需要它是一个 c-string,以便我可以通过其他函数运行 c-string。我对 C++ 还是很陌生。

作业状态:“将文本文件读入内存,一次一个字节” 我希望我想做的是这个。 谢谢

【问题讨论】:

  • 把“char * fileContent[MAX_SIZE]”改成“string fileContent[MAX_SIZE]”试试。
  • 再看看你在while循环中的情况。
  • @NipunTalukdar 这不会一次读取一个字节。
  • char c; while (ifile &gt;&gt; c) { /* whatevs */ }
  • @NeilKirk 或者他可以将“char * fileContent[MAX_SIZE]”更改为“char fileContent[MAX_SIZE]”

标签: c++ command-line-arguments ifstream c-strings


【解决方案1】:

您可以使用以下代码从文本文件中读取并将字符串保存为 C 字符串。输出文件 (output.txt) 包含 c-string 输出。

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


int main()
{

    freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);

    char *out_c_string;
    char ch;
    int index=0;

    while(cin >> ch)
        out_c_string[index++] = ch;


    for(int i=0; i<index; i++)
        cout << out_c_string[i]; // the c string of the file :)


    return 0;
}

【讨论】:

    【解决方案2】:

    您的代码中存在一些错误,请尝试以下操作:

    #include <iostream>
    #include <string>
    #include <fstream>
    
    using namespace std;
    
    int MAX_SIZE = 128; 
    
    int main(int argc, char* argv[])
    {
        char fileContent[MAX_SIZE]; //bad idea never do that!
                                    // use an std::vector<char> instead!
                                    // and reverse a minimum amount of chars 
                                    // using `reserve` if you are after performance
        ifstream ifile(argv[1]);
        int i = 0;
        while (i < MAX_SIZE)
        {
            ifile >> fileContent[i];
            cout << fileContent[i];
            if (ifile.eof())
                break;
            i++;
        }
        ifile.close();
    }
    

    【讨论】:

    • 我这样做了,但它说它期望在 [i]; 之后有一个 '{'
    • 现在试试,你有一个指向 char 的指针数组。你需要一个字符数组
    • 我这样做并添加了 noskipws 但它在末尾给出了一个奇怪的字符和一个数字。
    • 使用调试器,上面的代码已经过测试,运行良好。
    【解决方案3】:

    结合大家的答案,我得到了这个作为我的功能:

    void get_file_info(char * argv, char (&fileContent) [MAX_SIZE], int & filesize ){
        freopen(argv, "r", stdin);
        char ch;
        int index = 0;
    
        while (cin >> noskipws >> ch)
            fileContent[index++] = ch;
        cout << endl << index << endl;
    
    #if SHOW_DEBUG_CODE
        for (int count = 0; count < index; count++)
            cout << fileContent[count];
    #endif
    
        fclose(stdin);
    }
    

    它似乎工作得很好。我将在下一个空闲时间研究向量,但现在,我将继续研究 char 数组。

    感谢您的建议。

    【讨论】:

      【解决方案4】:

      我会这样做。处理任何大小的文件更通用。

      void ReadFile(char*file,char**buff,int*size){
      
          // Open file as binary putting file position at the end
          ifstream is(file,ios::binary|ios::ate);
      
          // Get the current file position, which is the file end
          *size=is.tellg();
      
          // Put file pointer back at the start
          is.seekg(0,ios::beg);
      
          // errors
          if (!*size){
              cout<<"Unable to open input file or file empty\n";
              exit(9);
          }
      
          // allocate a buffer one bigger to allow for zero terminator
          *buff=new char[*size+1];
      
          // read the whole file in one hit
          is.read(*buff,*size);
      
          // Done. So close and zero delimit data.
          is.close();
          *(*buff+*size)=0;
      }
      

      【讨论】:

        猜你喜欢
        • 2020-03-18
        • 1970-01-01
        • 2017-07-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-09-25
        • 2013-04-08
        • 1970-01-01
        相关资源
        最近更新 更多