在C ++中,在文本文件开头插入行的正确方法是什么? [英] In C++, what is the proper way to insert a line at the beginning of a text file?

查看:807
本文介绍了在C ++中,在文本文件开头插入行的正确方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个大文本文件,第一行是一个摘要,取决于内容。我目前插入一个摘要行,其中包含我期望的摘要是,并在创建文件后,我将其与实际写入的内容进行比较。如果信息不同,我将覆盖初始摘要。

I am creating a large text file, the first line is a summary which depends on the contents. I am currently inserting a summary line that contains what I expect the summary to be, and after creating the file I compare this with what was actually written. If the information is different I overwrite the initial summary.

代码如下。

  std::ofstream ofile(filename); 
  numOutSegInSynth-=computeSegmentsFromLevel(numLevelsInSynth-1,myInitCkt);

  //extra padding inserted, to prevent summary overwriting segments
  ofile<<numNodesInSynth<<" "<<numOutSegInSynth<<"  0"<<std::endl;
  ofile.close();
  for (long i=0; i<(numLevelsInSynth-1); i++) {  
    //at all
    long numSegOnLevel = computeSegmentsFromLevel(i,myInitCkt); 
    assert(numSegOnLevel); 
    numSegsCreated+=numSegOnLevel;
    buildSegmentsList(i,numSegOnLevel,filename,myInitCkt);   
  }
  if (numSegsCreated != numOutSegInSynth) {
  //update segment count
    std::ofstream ofile(filename, ios::in | ios::out );
    ofile.seekp(0, ios::beg);
    short pred = log10(numOutSegInSynth)+1;
    short act = log10(numSegsCreated)+1;
    if (act>pred) {
      ofile<<numNodesInSynth<<" "<<numSegsCreated<<" 0"<<std::endl;
    } else {
      ofile<<numNodesInSynth<<" "<<numSegsCreated<<"  0"<<std::endl;
    }
      ofile.close();
    }



我想知道是否可能只是追加到开始文件,要插入一个新行或单个字符
感谢

I would like to know if it is possible to just append to the start of the file, either to insert an new line or a single character Thanks

推荐答案

正如Ignacio说,大多数文件系统不支持在文件中插入,更不用说文本文件。而C ++标准库的一个文件的概念是一个简单的字节流。因此,在开始时,一个固定大小的摘要,因为它似乎是你的意图与你的代码,是一个实用的解决方案(注意:你的代码似乎没有达到这个固定的大小)。

As Ignacio says, most filesystems don't support insertion in files, not to mention text files. And the C++ standard library's notion of a file is a simple stream of bytes. And so a fixed size summary at the start, as it seems you were intending with your code, is one practical solution (note: your code doesn't appear to achieve this fixed size).

但是,一些常见的文件系统确实支持将额外的信息与文件关联。

However, some common filesystems do support associating extra information with files.

例如,Windows NTFS支持多个数据流文件:

For example, Windows NTFS supports multiple data streams per file:


C:\test> echo blah blah >data.txt

C:\test> type data.txt
blah blah

C:\test> echo some info >data.txt:summary

C:\test> type data.txt
blah blah

C:\test> more <data.txt:summary
some info

C:\test> _

例如,这是Windows资源管理器用于向文件添加摘要信息的机制。

E.g., this is the mechanism used by Windows Explorer for adding summary info to files.

所以,如果你为一个支持多个流的系统编程,并且不计划将程序或数据移植到其他系统,你可以考虑一个多流文件。

So, if you're programming for a system that supports multiple streams, and don't plan on porting the program or the data to other systems, you might consider a multiple-stream file.

第三种方法是通过拥有两个或多个关联文件来有效实现自己。例如。一个数据文件和一个摘要文件。使用例如一个连接它们的命名约定。

A third alternative is to effectively implement that yourself, by having two or more associated files. E.g. one data file, and one summary file. With e.g. a naming convention connecting them.

hth。,

这篇关于在C ++中,在文本文件开头插入行的正确方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆