我試圖寫一個程序: -reads一個文本文件,然後把它轉換成字符串 -change每個字母在字符串中減去4 -outputs在修改行讀取文件到程序
我知道如何輸入/輸出文件。我沒有比這更多的代碼,因此對我來說這是一個非常新的概念。我已經研究過,找不到直接的答案。如何將原始文件的每一行輸入到一個字符串中然後進行修改?
謝謝!
// Lab 10
// programmed by Elijah
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main()
{
fstream dataFile;
//Set the file "coded" as the line input
dataFile.open("coded.txt", ios::in);
//Create the file "plain2" as program output
dataFile.open("plain2.txt", ios::out);
}
謝謝,這個作品很棒!一個問題:是否有辦法維護原始輸入文件中包含的換行符? – Elijah 2014-12-06 20:14:17
如果你的意思是換行符,我認爲這些將會像任何其他字符一樣存儲到ch中。如果不是,請填寫 – 2014-12-06 21:08:51
@Elijah您可能會無意中通過減去4來修改ch,如果它是換行符或空格,則不會執行此操作。看修改後的答案。 – 2014-12-06 21:19:02