簡體   English   中英

C++ ofstream 將變量寫入文本文件

[英]C++ ofstream writing variables into a text file

我無法找到任何明顯的答案,我很困惑。

當我想將一個變量字符串寫入文本文檔時,與我只想將一個設置字符串寫入所述文檔時需要做的事情相比,我是否需要做一些不同的事情?

此代碼將正確地在 newfile.txt 上的 8 個單獨行上寫入“設置字符串”(並創建它)。

string output_file = "newfile.txt"
ofstream file_object_2;
        file_object_2.open(output_file, std::ofstream::out | std::ofstream::app);
        string nextline;
        for (int i = 0; i <= row_number; ++i)
        {
            file_object_2 << "set string" << "\n";
        }
        file_object_2.close();
        return 0;

但這會使文件完全為空,即使 line_vector[i] 本身包含字符串(並且 cout 可以打印它們)

string output_file = "newfile.txt"
ofstream file_object_2;
        file_object_2.open(output_file, std::ofstream::out | std::ofstream::app);
        string nextline;
        for (int i = 0; i <= row_number; ++i)
        {
            nextline = line_vector[i];
            file_object_2 << nextline << "\n";
        }
        file_object_2.close();
        return 0;

我試圖查看文檔並以與他們所做的相同的方式進行操作,但是我在這里沒有成功。 顯然是因為我自己的失敗,但我就是無法弄清楚我在這里做錯了什么。

這兩個代碼的唯一區別在於我試圖寫入文檔的行

file_object_2 << nextline << "\n";

對比

file_object_2 << "set string" << "\n";

main() 我試圖減少它的動態功能(沒有手動輸入),但仍然不起作用:

文本文件“a.txt”只有幾行隨機字符串

[a.txt]
("Yogi has a best friend too
Boo Boo, Boo Boo
Yogi has a best friend too
Boo Boo, Boo Boo Bear
Boo Boo, Boo Boo Bear
Boo Boo, Boo Boo Bear
Yogi has a best friend too
Boo Boo, Boo Boo Bear")

和函數本身

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


    using namespace std;

    int main() {
        string input_file = "a.txt";

        vector<string> line_vector;

        string output_file = "output.txt";

 // reads from the original text file. Included in the example because I might
 // be constructing the vector in a stupid way and this is where it happens
            int row_number = 0;
            string line;
            while (getline(file_object, line) )
            {
                cout << line << endl;
                line_vector.push_back(line);
                ++row_number;
            }
            file_object.close();


  // writing onto a new file starts, this is where I'd assume the problem is
            ofstream file_object_2;
            file_object_2.open(output_file, std::ofstream::out | std::ofstream::app);
            string nextline;
            for (int i = 0; i <= row_number; ++i)
            {
                nextline = i + " " + line_vector[i];
                file_object_2 << nextline << "\n";
            }
            file_object_2.close();
            return 0;    
    }

這已破了:

i + " " + line_vector[i]

您的意思是to_string(i) + " " + line_vector[i] ,但是您直接在i""上使用了+ ,編譯器確定它是operator+(int, const char*) ,它是指針算術,而不是i到的轉換一個字符串。 由於所討論的字符串只有一個字符(NUL),因此添加1數字會導致無法取消引用的尾后指針,添加更大的數字已經是未定義的行為。

最簡單的解決方法是將inextline分開,並將其直接寫入文件。 你的循環體變成:

{
   nextline = line_vector[i];
   file_object_2 << i << " " << nextline << "\n";
}

但它也應該使用std::to_string()i轉換為字符串。


作為旁注,您聲稱的第一段代碼已損壞(其中nextline = line_vector[i]; )實際上很好。 如果您不厭其煩地進行測試,您可能會自己發現問題。

暫無
暫無

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

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