#include <iostream>
#include <fstream>
#include <vector>
#include <string>
using namespace std;
int main()
{
string text;
int line;
cout << "Text: ";
cin >> text;
cout << "Line: ";
cin >> line;
fstream file;
file.open("main.txt", ios::in | ios::out);
vector<string> lines;
string index;
while (getline(file, index))
{
lines.push_back(index);
}
line--;
for (int i = 0; i < lines.size(); i++)
{
if (i != line)
{
file << lines[i] << endl;
} else
{
file << text << endl;
}
}
file.close();
return 0;
}
I want to change specific text in my file text but my code isnt work.
Could someone help me what is the issue in my code.
*Note: I just see vector in C++ in the first time, so my code can be had many problem.
我想要更改我的文件文本中的特定文本,但我的代码不工作。谁能帮帮我,我的代码中有什么问题。*注:我只是第一次在C++中看到向量,所以我的代码可能会有很多问题。
LINE 1
LINE 2
LINE 3
LINE 4
LINE 5
this is my file text (main.txt)
这是我的文件文本(main.txt)
更多回答
Open the file for reading only, read into the vector, close the file. Modify the vector and its contents as needed (very easy if you have the line-number, as the vector index is the line-number minus one). Then open the file for writing (truncating any existing contents), and write the new contents of the vector.
打开只读文件,读入向量,关闭文件。根据需要修改向量及其内容(如果您有行号,则非常容易,因为向量索引是行号减一)。然后打开文件进行写入(截断任何现有内容),并写入向量的新内容。
We cant read and write in one times ?
我们不能同时读写吗?
No that really don't work very well.
不,那真的不太管用。
Could you help me how to solve it ?
你能帮我解决这个问题吗?
优秀答案推荐
Use separate steps for Read/Update/Write
@Some programmer dude suggests a great approach for solving this problem:
@某位程序员提出了一个解决这个问题的好方法:
Open the file for reading only, read into the vector, close the file. Modify the vector and its contents as needed. Then open the file for writing (truncating any existing contents), and write the new contents of the vector.
You want to use this approach, because the length of a line might change during updates.
您希望使用这种方法,因为行的长度在更新期间可能会更改。
There are techniques for opening a file for both input and output at the same time, but they don't work if line lengths are changed. If you write a new line 3, for instance, that is shorter than the old line 3, then some of the old line 3 will still remain in the file.
有一些技术可以同时打开输入和输出的文件,但如果更改了行长,这些技术就不起作用了。例如,如果您编写了比旧行3更短的新行3,那么一些旧行3仍将保留在文件中。
And if the new line 3 is longer than the old one, then you will overwrite part of line 4.
如果新行3比旧行长,那么您将覆盖行4的一部分。
So it is best to stage the operations separately from each other:
因此,最好是将操作彼此分开进行:
- read
- update
- write
A pseudo-code solution
This appears to be a homework assignment of some sort, so I do not want to provide a complete program. Instead, here is some pseudo-code to get you started.
这似乎是某种形式的家庭作业,所以我不想提供一个完整的程序。相反,这里有一些伪代码可以帮助您入门。
// main.cpp
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
bool read_file(std::string const& file_name,
std::vector<std::string>& lines)
{
bool good{ true };
// Open file for input.
// Read file, one line at a time.
// Call push_back to add each line to vector lines.
// If file open fails, or a read operation fails
// for a reason other than eof, set good to false.
return good;
}
bool update(std::vector<std::string>& lines)
{
bool good{ true };
// Get text and line number from user.
// Update vector lines.
// If the user cancels the update, set good to false.
return good;
}
void write_file(std::string const& file_name,
std::vector<std::string> const& lines)
{
// Open file for output.
// Write file.
}
int main()
{
std::string file_name{ "main.txt" };
std::vector<std::string> lines;
if (read_file(file_name, lines) && update(lines))
write_file(file_name, lines);
return 0;
}
更多回答
我是一名优秀的程序员,十分优秀!