[英]C++: How to correctly write in a .csv file
我在文本文件中有以下數據
04EC3AC705EA3AC8042C3BC7051A3AC8043C3AD7054A3AE8
我想將此數據放入單個列中的 .csv 文件中,如下所示。
04EC,3AC7
05EA,3AC8
042C,3BC7
051A,3AC8
043C,3AD7
054A,3AE8
到目前為止我嘗試過的是:
fp=fopen("D:/pattern.csv", "a+")
Then I read the original values from the text file in a buffer
fwrite(buffer, 48,1,fp);
但是我再次在 csv 表的單個單元格中獲取所有值。
請告訴我一個更好的方法來實現這一目標。 我在 MSVS 2010 win32 中開發了這段代碼,我將在另一個用 C++/CLI Windows 窗體應用程序編寫的應用程序中使用這段代碼。
謝謝
如果我很好理解,您的輸入文件只包含一行和一個字符串。 並且您想將其轉換為成對的 4 個字符?
像這樣更“C++”的方式怎么樣?
std::string input_file_name = "input.txt";
std::string output_file_name = "output.csv";
std::string line;
std::ifstream input_file(input_file_name.c_str());
input_file >> line;
std::ofstream output_file(output_file_name.c_str());
for(unsigned long int i = 0; i + 7 < line.length(); i += 8)
output_file << line.substr(i, 4) << "," << line.substr(i + 4, 4) << std::endl;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.