簡體   English   中英

如何將數據從在同一程序中創建的文本文件加載到向量中。 在 C++ 中

[英]How to load data into a vector from a text file that has been created inside the same program. in C++

我想將在同一程序中創建的文本文件中的數據加載到字符串向量中。 但是這里沒有一行文本被推入向量中。

在這里,首先我從一些輸入文件中讀取數據,然后對其進行一些操作(刪除多余的空格),然后將此文件保存為“intermediate.txt”。 這個intermediate.txt 正在創建中,我想做的所有操作都成功發生了。 最后,我想將此文件讀入vector<string>代碼,但它不起作用。 我無法在vector<string>代碼中存儲任何內容。 它的大小為零。

#include <bits/stdc++.h>
using namespace std;

int main()
{
    string inputFileName;
    cout << "Enter the Input File Name: ";
    cin >> inputFileName;
    ifstream f1(inputFileName);
    ofstream f0("intermediate.txt");
    string text_line;

    while (getline(f1, text_line))
    {
        string word;
        istringstream text_stream(text_line);
        while (text_stream >> word)
        {
            f0 << word << " ";
        }
        f0 << "\n";
    }
    f0.close()
    ifstream file("intermediate.txt");
    vector<string> code;
    string line;
    while (getline(file, line, '\n'))
    {
        code.push_back(line);
    }

    for (auto it : code)
    {
        cout << it << "\n";
    }
}

我剛剛在打開 intermediat.txt 之前添加了 f0.close() 並且它有效

這是一個迷你代碼審查:

#include <bits/stdc++.h>  // Don't do this; doesn't even compile for me
using namespace std;      // Don't do this either

int main()
{
    string inputFileName;
    cout << "Enter the Input File Name: ";
    cin >> inputFileName;
    ifstream f1(inputFileName);  // Bad name
    ofstream f0("intermediate.txt");  // Bad name

    // You never check that you successfully opened *any* files.

    string text_line;

    /*
     * You don't describe why this is necessary, can a line not be read and
     * written as-is? Is it already a line of space-separated variables?
     *
     * In any case, this is where you already have the words; so store them in
     * the vector here as well.
     */
    while (getline(f1, text_line))
    {
        string word;
        istringstream text_stream(text_line);
        while (text_stream >> word)
        {
            f0 << word << " ";
        }
        f0 << "\n";
    }
    f0.close()  // Forgot your semi-colon
                // Left f1 open, that's bad practice
    ifstream file("intermediate.txt");
    vector<string> code;
    string line;

    /*
     * I would hope you felt that reading from a file, writing to a new file,
     * closing both files, opening the new file, and reading from the new file
     * into the vector was wasteful.
     */
    while (getline(file, line, '\n'))
    {
        code.push_back(line);
    }

    for (auto it : code)
    {
        cout << it << "\n";
    }
}

您最初的問題最直接的問題是您試圖在兩個不同的流中打開同一個文件。 第二次,文件打開失敗,但是因為你從來沒有檢查過你是否真的打開了文件,你以為一切正常,但事實並非如此,這把你帶到了這里。

但是,有更好的方法。

#include <fstream>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>

int main() {
  std::string inputFileName;
  std::cout << "Enter the Input File Name: ";
  std::cin >> inputFileName;

  // Always check that you successuflly opened the file.
  std::ifstream fin(inputFileName);
  if (!fin) {
    std::cerr << "Error opening: " << inputFileName << ". Exiting...\n";
    return 1;
  }

  std::ofstream fout("intermediate.txt");
  if (!fout) {
    std::cerr << "Error opening: intermediate.txt. Exiting...\n";
    return 2;
  }

  std::vector<std::string> code;

  std::string text_line;
  while (std::getline(fin, text_line))  // You've read the line
  {
    std::string word;
    std::istringstream text_stream(text_line);
    while (text_stream >> word) {
      fout << word << " ";
    }
    fout << "\n";
    code.push_back(text_line);  // Just store it while you have it
  }
  fin.close();   // Best practice is to close a file as soon as you're done
  fout.close();  // with it. Don't hog resources.

  for (const auto& it : code)  // Avoid making copies
  {
    std::cout << it << "\n";
  }
}

while循環,您在其中讀取要存儲在向量中的行,現在寫入您的文件並將這些行存儲到向量中。 我們現在還檢查我們是否成功打開了文件,並在完成文件后立即關閉文件流,以免無緣無故地保持鎖定狀態。

進一步改進這個程序的一個好的下一步是避免向用戶詢問文件名。 相反,將其作為main()的參數。 這樣,某人只需在命令行上鍵入./a.out input.txt ,程序就會自動完成這項工作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM