![](/img/trans.png)
[英]How can I store a string(from a file with n number of lines) in a dynamic array? C++
[英]How can I replace n lines from a file to another file in C++?
我想用 second.txt 的前 n 行替換 first.txt 的最后 n 行,其中 n 由用戶輸入。 這是我的代碼:
ofstream myFirst("first.txt", ios:: binary);
ifstream mySecond("second.txt", ios::binary);
int firstFileLine = lineCountFirst;
int count = 0;
while(getline(myFirst, line)){
count++;
if(count >= firstFileLine){
getline(mySecond, line2); // I don't know if it can be written like this
line = line2; // here to replace line to line2
}
}
myFirst.close();
mySecond.close();
帶注釋的兩行代碼是我感到困惑的地方。 關於如何替換文件的任何想法?
我想用 second.txt 的前 n 行替換 first.txt 的最后 n 行,其中 n 由用戶輸入。
first.txt
讀取std::vector<std::string>
first.txt
。 從向量中刪除最后n
元素。 將向量的元素寫入臨時文件。 從second.txt
讀取n
行並將它們附加到臨時文件中。 取消first.txt
並重命名您創建的臨時文件。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.