c ++ /以特定方式在txt文件上追加数据
c++ / Append data on a txt file in a specific way
我有一个这样的 txt 文件:
123456
123456
abcdef
...
用C++,我想用下面的方式打开并添加数据
123456 new data new data
123456 new data new data
abcdef new data new data
...
我已经在 Whosebug 上看到这个 但它在 Python.
我用过这个功能
std::ofstream file_to_save;
file_to_save.open(path, ios::out | ios::app);
但它会在文件末尾添加数据,而不是在每个单词旁边。
编辑: 实际上,我在一个循环中连续添加日期,而不是一次添加所有内容。这就是困难所在。所以我的文件包含所有日期,然后在循环中创建新数据。让我们说“新数据”,然后我想将这个新数据(总是不同的)分配给已经存在的文件。
123456
123456
abcdef
...
我在循环“新数据 1”中创建了新数据,然后想像这样添加到文件中
123456 new data 1
123456
abcdef
...
然后循环中的第 2 步,我创建了“新数据 2”,然后想像这样添加到文件中
123456 new data 1
123456 new data 2
abcdef
...
然后在循环的第 3 步,我创建了“新数据 3”,然后想像那样添加到文件中
123456 new data 1
123456 new data 2
abcdef new data 3
...
and so on, until fill the entire file.
有人可以帮助我吗?
感谢
这就是 "ios::app" 的工作方式——在文件末尾添加数据。
您可能希望先阅读每一行,然后覆盖所有行。
读取所有文件并将字符串存储在向量中。对于向量中的每个元素,追加您的新数据,然后将它们写入文件,完全覆盖您的文件。
或者,您可以使用搜索功能逐行read/overwrite您的文件
您必须将现有文件的内容存储在一个数组中
或矢量并将其写回同一个文件。
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);
}
}
有很多方法可以做到这一点。最安全的通常是将每一行复制到一个新文件,将您需要的任何内容附加到该行,然后继续直到输入文件的末尾。
然后您可以将数据从新文件复制回旧文件,或者(如果您确定没有其他链接)删除旧文件,然后将新文件重命名为旧文件.
或者,您可以从旧文件复制到临时文件,然后在将数据复制回旧文件时处理数据(最后删除临时文件)。
将数据读入内存,然后用新数据覆盖文件是非常脆弱的——如果你在操作过程中遇到崩溃或断电,你的文件很可能会被破坏(即,你没有旧 或 新数据的副本。我会避免使用它,除非你真的更担心速度而不是你不介意的可靠性完全破坏数据的可能性。
明显代码:
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";
根据我在你的问题中读到的内容,你想要:
- 读取行
- 向其中添加新数据
- 存储所有更新的行
- 用新行覆盖文件
这是一种可能的实现方式:
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();
我有一个这样的 txt 文件:
123456
123456
abcdef
...
用C++,我想用下面的方式打开并添加数据
123456 new data new data
123456 new data new data
abcdef new data new data
...
我已经在 Whosebug 上看到这个
我用过这个功能
std::ofstream file_to_save;
file_to_save.open(path, ios::out | ios::app);
但它会在文件末尾添加数据,而不是在每个单词旁边。
编辑: 实际上,我在一个循环中连续添加日期,而不是一次添加所有内容。这就是困难所在。所以我的文件包含所有日期,然后在循环中创建新数据。让我们说“新数据”,然后我想将这个新数据(总是不同的)分配给已经存在的文件。
123456
123456
abcdef
...
我在循环“新数据 1”中创建了新数据,然后想像这样添加到文件中
123456 new data 1
123456
abcdef
...
然后循环中的第 2 步,我创建了“新数据 2”,然后想像这样添加到文件中
123456 new data 1
123456 new data 2
abcdef
...
然后在循环的第 3 步,我创建了“新数据 3”,然后想像那样添加到文件中
123456 new data 1
123456 new data 2
abcdef new data 3
... and so on, until fill the entire file.
有人可以帮助我吗?
感谢
这就是 "ios::app" 的工作方式——在文件末尾添加数据。
您可能希望先阅读每一行,然后覆盖所有行。
读取所有文件并将字符串存储在向量中。对于向量中的每个元素,追加您的新数据,然后将它们写入文件,完全覆盖您的文件。
或者,您可以使用搜索功能逐行read/overwrite您的文件
您必须将现有文件的内容存储在一个数组中 或矢量并将其写回同一个文件。
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);
}
}
有很多方法可以做到这一点。最安全的通常是将每一行复制到一个新文件,将您需要的任何内容附加到该行,然后继续直到输入文件的末尾。
然后您可以将数据从新文件复制回旧文件,或者(如果您确定没有其他链接)删除旧文件,然后将新文件重命名为旧文件.
或者,您可以从旧文件复制到临时文件,然后在将数据复制回旧文件时处理数据(最后删除临时文件)。
将数据读入内存,然后用新数据覆盖文件是非常脆弱的——如果你在操作过程中遇到崩溃或断电,你的文件很可能会被破坏(即,你没有旧 或 新数据的副本。我会避免使用它,除非你真的更担心速度而不是你不介意的可靠性完全破坏数据的可能性。
明显代码:
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";
根据我在你的问题中读到的内容,你想要:
- 读取行
- 向其中添加新数据
- 存储所有更新的行
- 用新行覆盖文件
这是一种可能的实现方式:
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();