I have a txt file like this:
我有一个这样的txt文件:
123456
123456
abcdef
...
With C++, I would like to open it and add data in the following way
使用C ++,我想打开它并以下列方式添加数据
123456 new data new data
123456新数据新数据
123456 new data new data
123456新数据新数据
abcdef new data new data
abcdef新数据新数据
...
I already saw this post on * but it's in Python.
我已经在*上看过这篇文章,但它是在Python中。
I used this function
我用过这个功能
std::ofstream file_to_save;
file_to_save.open(path, ios::out | ios::app);
but it adds the data at the end of the file and not next to each word.
但它会在文件末尾添加数据,而不是在每个单词旁边添加数据。
EDIT: Actually I add the date continuously in a loop and not everything in one time. That's the difficulty. So I have my file with all the date then inside a loop, I create new data. Let's say "new data" and then I would like to assign this new data (always different) to the file which already exist.
编辑:实际上我在循环中连续添加日期,而不是一次性添加所有内容。这就是困难。所以我的文件包含所有日期然后在循环中,我创建新数据。让我们说“新数据”,然后我想将这些新数据(总是不同的)分配给已经存在的文件。
123456
123456
abcdef
...
I create new data in my loop "new data 1" then want to add to the file like that
我在循环“新数据1”中创建新数据,然后想要添加到文件中
123456 new data 1
123456新数据1
123456
abcdef
...
Then Step 2 on my loop, I create "new data 2" then want to add to the file like that
然后在我的循环上的第2步,我创建“新数据2”然后想要添加到该文件
123456 new data 1
123456新数据1
123456 new data 2
123456新数据2
abcdef
...
Then Step 3 on my loop, I create "new data 3" then want to add to the file like that
然后在我的循环上的第3步,我创建“新数据3”然后想要添加到该文件
123456 new data 1
123456新数据1
123456 new data 2
123456新数据2
abcdef new data 3
abcdef新数据3
... and so on, until fill the entire file.
...等等,直到填满整个文件。
Can someone help me ?
有人能帮我吗 ?
Thank
5 个解决方案
#1
You have to store the contents of the existing file in an array or vector and write it back to the same file.
您必须将现有文件的内容存储在数组或向量中,然后将其写回到同一文件中。
void appendToFile(string sFilename, int nInsertAt, string sDataToInsert)
{
std::ifstream infile("output.txt");
string line;
vector<string> vLines;
while (std::getline(infile, line))
{
vLines.push_back(line);
}
infile.close();
std::ofstream outfile("output.txt");
for (int i = 0; i < vLines.size(); i++)
{
char buff[1024];
if (i == nInsertAt)
{
sprintf(buff, "%s %s", vLines[i].c_str(), sDataToInsert.c_str());
outfile << buff << endl;
}
else
{
outfile << vLines[i] << endl;
}
}
outfile.close();
}
void test()
{
for (int i = 0; i < 10; i++)
{
char buff[1024];
sprintf(buff, "new data %d", i);
appendToFile("output.txt", i, buff);
}
}
#2
Read all the file and store your strings in a vector. For each element in a vector, append your new data and then write them in the file, overwriting your file entirely.
读取所有文件并将字符串存储在向量中。对于向量中的每个元素,附加新数据,然后将其写入文件,完全覆盖文件。
Alternatively, you can use the seeking function to read/overwrite your file line by line
或者,您可以使用seek函数逐行读取/覆盖文件
#3
There are quite a few ways of doing this. The safest is usually to copy each line to a new file, append whatever you need to that line, and continue until the end of the input file.
有很多方法可以做到这一点。最安全的通常是将每一行复制到一个新文件,将所需的任何内容附加到该行,并继续直到输入文件的末尾。
Then you can just copy the data back from the new file to the old one, or (if you're sure there aren't other links to it) delete the old, and rename the new to the old name.
然后,您可以将数据从新文件复制回旧文件,或者(如果您确定没有其他链接)删除旧文件,并将新文件重命名为旧名称。
Alternatively, you can copy from the old file to a temporary file, then process the data as you copy it back to the old file (and finally delete the temporary file).
或者,您可以从旧文件复制到临时文件,然后在将数据复制回旧文件时处理数据(最后删除临时文件)。
Reading the data into memory, then overwriting the file with the new data is much more fragile--if you get a crash or power loss in the middle of the operation, your file is likely to be destroyed (i.e., you don't have a copy of either the old or the new data. I'd avoid it unless you're really so much more worried about speed than reliability that you don't mind the possibility of destroying the data entirely.
将数据读入内存,然后使用新数据覆盖文件要脆弱得多 - 如果在操作过程中遇到崩溃或断电,您的文件可能会被破坏(即,您没有旧数据或新数据的副本。我会避免使用它,除非你真的更担心速度而不是可靠性,你不介意完全破坏数据的可能性。
Obvious code:
std::ifstream input("filename");
std::ofstream output("filename2");
std::string line;
while (std::getline(input, line))
output << line << " new data new data new data\n";
#4
From what I've read in you question, you want:
从我在你的问题中读到的,你想要:
- read line
- add new data to it
- store all updated lines
- overwrite the file with the new lines
向其添加新数据
存储所有更新的行
用新行覆盖文件
Here is one possible implemenation:
这是一个可能的实现:
ifstream infile("filetobeupdated.txt");
if(!infile) error("Can't open file: ", filetobeupdated);
// vector holding old data
string line;
vector<string>oldlines;
while(getline(infile,line)) oldlines.push_back(line);
infile.close();
// vector holding newdata
vector<string>newdata;
// vector holding updated data
vector<string>updateddata;
// concatenate old line + new data
for(size_t i=0, i<oldlines.size();i++) updateddata.push_back(oldlines[i]+newdata[i]);
// overwrite old file with new data
ofstream onfile("filetobeupdated.txt");
if(!onfile) error("Can't open file: ", filetobeupdated);
for(size_t i=0, i<newdata.size();i++) onfile << newdata[i] <<'\n';
onfile.close();
#5
That's how "ios::app" works--adding data at the end of a file.
这就是“ios :: app”的工作原理 - 在文件末尾添加数据。
You may want to read each line first and then overwrite all the lines.
您可能希望先读取每一行,然后覆盖所有行。
#1
You have to store the contents of the existing file in an array or vector and write it back to the same file.
您必须将现有文件的内容存储在数组或向量中,然后将其写回到同一文件中。
void appendToFile(string sFilename, int nInsertAt, string sDataToInsert)
{
std::ifstream infile("output.txt");
string line;
vector<string> vLines;
while (std::getline(infile, line))
{
vLines.push_back(line);
}
infile.close();
std::ofstream outfile("output.txt");
for (int i = 0; i < vLines.size(); i++)
{
char buff[1024];
if (i == nInsertAt)
{
sprintf(buff, "%s %s", vLines[i].c_str(), sDataToInsert.c_str());
outfile << buff << endl;
}
else
{
outfile << vLines[i] << endl;
}
}
outfile.close();
}
void test()
{
for (int i = 0; i < 10; i++)
{
char buff[1024];
sprintf(buff, "new data %d", i);
appendToFile("output.txt", i, buff);
}
}
#2
Read all the file and store your strings in a vector. For each element in a vector, append your new data and then write them in the file, overwriting your file entirely.
读取所有文件并将字符串存储在向量中。对于向量中的每个元素,附加新数据,然后将其写入文件,完全覆盖文件。
Alternatively, you can use the seeking function to read/overwrite your file line by line
或者,您可以使用seek函数逐行读取/覆盖文件
#3
There are quite a few ways of doing this. The safest is usually to copy each line to a new file, append whatever you need to that line, and continue until the end of the input file.
有很多方法可以做到这一点。最安全的通常是将每一行复制到一个新文件,将所需的任何内容附加到该行,并继续直到输入文件的末尾。
Then you can just copy the data back from the new file to the old one, or (if you're sure there aren't other links to it) delete the old, and rename the new to the old name.
然后,您可以将数据从新文件复制回旧文件,或者(如果您确定没有其他链接)删除旧文件,并将新文件重命名为旧名称。
Alternatively, you can copy from the old file to a temporary file, then process the data as you copy it back to the old file (and finally delete the temporary file).
或者,您可以从旧文件复制到临时文件,然后在将数据复制回旧文件时处理数据(最后删除临时文件)。
Reading the data into memory, then overwriting the file with the new data is much more fragile--if you get a crash or power loss in the middle of the operation, your file is likely to be destroyed (i.e., you don't have a copy of either the old or the new data. I'd avoid it unless you're really so much more worried about speed than reliability that you don't mind the possibility of destroying the data entirely.
将数据读入内存,然后使用新数据覆盖文件要脆弱得多 - 如果在操作过程中遇到崩溃或断电,您的文件可能会被破坏(即,您没有旧数据或新数据的副本。我会避免使用它,除非你真的更担心速度而不是可靠性,你不介意完全破坏数据的可能性。
Obvious code:
std::ifstream input("filename");
std::ofstream output("filename2");
std::string line;
while (std::getline(input, line))
output << line << " new data new data new data\n";
#4
From what I've read in you question, you want:
从我在你的问题中读到的,你想要:
- read line
- add new data to it
- store all updated lines
- overwrite the file with the new lines
向其添加新数据
存储所有更新的行
用新行覆盖文件
Here is one possible implemenation:
这是一个可能的实现:
ifstream infile("filetobeupdated.txt");
if(!infile) error("Can't open file: ", filetobeupdated);
// vector holding old data
string line;
vector<string>oldlines;
while(getline(infile,line)) oldlines.push_back(line);
infile.close();
// vector holding newdata
vector<string>newdata;
// vector holding updated data
vector<string>updateddata;
// concatenate old line + new data
for(size_t i=0, i<oldlines.size();i++) updateddata.push_back(oldlines[i]+newdata[i]);
// overwrite old file with new data
ofstream onfile("filetobeupdated.txt");
if(!onfile) error("Can't open file: ", filetobeupdated);
for(size_t i=0, i<newdata.size();i++) onfile << newdata[i] <<'\n';
onfile.close();
#5
That's how "ios::app" works--adding data at the end of a file.
这就是“ios :: app”的工作原理 - 在文件末尾添加数据。
You may want to read each line first and then overwrite all the lines.
您可能希望先读取每一行,然后覆盖所有行。