如何用另一个/ C++代码替换一行

问题描述:

我正在使用ubuntu。我有一个名为test.txt的文件。我想用另一行替换第二行。我怎样才能做到这一点?我不想创建一个新文件并删除第一个文件。如何用另一个/ C++代码替换一行

我想说明的是,新生产线的lenght与OND一个

+5

你想长的一模一样的另一条线来代替行? – 2012-01-13 13:23:18

+0

是的!新行的长度与第一行的长度相同 – 2012-01-14 10:20:09

+0

@AngelDream:是纯ASCII还是可能在这行中有UTF-8序列? – 2012-01-14 11:18:58

试着这么做:

#include <fstream> 
#include <string> 

int main() { 
    const int lineToReplace = 14; 
    std::fstream file("myfile.txt", std::ios::in | std::ios::out); 
    char line[255]; 
    for (int i=0; i<lineToReplace-1; ++i) 
     file.getline(line, 255); // This already skips the newline 
    file.tellg(); 
    file << "Your contents here" << std::endl; 
    file.close(); 
    return 0; 
} 

注意line最多可以容纳254个字节(加空终止),所以如果您的订单需要做的更多,相应的调整。

+0

如何改变这个替换线14? – 2012-01-14 11:15:46

+0

将getline放入要执行13次的循环中:'for(int i = 0; i jweyrich 2012-01-14 11:18:55

+0

它不会更改新行 – 2012-01-14 11:23:19

的长度相同的,如果该文件是足够小,你可以将它读入内存中,你想在任何修改内存中的副本,如果退出则写入。

编辑的请求代码:

// A vector to store all lines 
std::vector<std::string> lines; 

// The input file 
std::ifstream is("test.txt") 

// Get all lines into the vector 
std::string line; 
while (std::getline(is, line)) 
    lines.push_back(line); 

// Close the input file 
is.close(); 

// All of the file is now in memory, each line a single entry in the vector 
// "lines". The items in the vector can now be modified as you please. 

// Replace the second line with something else 
lines[1] = "Something else"; 

// Open output file 
std::ofstream os("test.txt"); 

// Write all lines to the file 
for(const auto& l : lines) 
    os << l << '\n'; 

// All done, close output file 
os.close(); 
+0

请显示一些代码。赞赏 – 2012-01-14 10:21:55

+0

为了在内存中获取文件,你可以使用'stat(2)'syscall来查询文件的长度,然后'打开(2)'它和'mmap(2)'它。详细信息请参阅手册页。 – 2012-01-14 11:03:13

+0

@AngelDream为我的答案添加了一个简单的例子。 – 2012-01-14 13:14:08

这是Python的,但它是显著更具可读性和简洁为了这个目的:

f = open('text.txt', 'w+') # open for read/write 
g = tempfile.TemporaryFile('w+') # temp file to build replacement data 
g.write(next(f)) # add the first line 
next(f) # discard the second line 
g.write(second_line) # add this one instead 
g.writelines(f) # copy the rest of the file 
f.seek(0) # go back to the start 
g.seek(0) # start of the tempfile 
f.writelines(g) # copy the file data over 
f.truncate() # drop the rest of the file 

你也可以使用shutil.copyfileobj而不是writelines做阻止文件之间的复制。

+0

这是哪一种语言?看起来不像C++。 – MSalters 2012-01-13 13:45:59

+0

哦,我甚至没有注意到他说过C++。我想看看这个简洁的C++解决方案。 OP我建议你在python中这样做。 – 2012-01-13 13:48:41

+1

在一个类似的非他所要求的静脉中,sed -e'2s /.*/这绝对是第二行/'〜/ test_file' – 2012-01-13 14:09:53

这是我会怎么做,没有一个硬性限制上线长度:

#include <fstream> 
#include <string> 

using namespace std; 

int main() 
{ 
    fstream file("test.txt",std::ios::in|std::ios::out); 
    string line; 
    string line_new="LINE2"; 

    // Skip the first line, file pointer points to beginning of second line now. 
    getline(file,line); 

    fstream::pos_type pos=file.tellg(); 

    // Read the second line. 
    getline(file,line); 

    if (line.length()==line_new.length()) { 
     // Go back to start of second line and replace it. 
     file.seekp(pos); 
     file << line_new; 
    } 

    return 0; 
}