一、使用背景:
工作中有一个多进程程序,每一个进程都要处理一个文件,这些文件名存放在一个文件filelist.txt中。我这段程序就是要获取一个标志为0的文件名,把这个文件名记录下来,让一个进程去处理这个文件,然后把这个文件名后面的标志改为1。
file1 处理前,filelist.txt内容:
file1 0
file2 0
file3 0
file1 处理后,filelist.txt内容:
file1 1
file2 0
file3 0
二、如何用c++代码实现这样修改文件内容的操作呢?
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
void get_linenum_linedata(string& file, int& linenum, string& linedata, string& filename) {
ifstream in(file);
string line;
if (in) {
while (getline(in, line)) {
linenum++;
if (line[line.size() - 1] == '0') {
filename = line.substr(0, l