English 中文(简体)
C 文档流,首插入
原标题:C file streams, inserting at the beginning

Is there a simple way to insert something at the beginning of a text file using file streams? Because the only way I can think of, is to load a file into a buffer, write text-to-append and then write the buffer. I want to know if it is possible to do it without my buffer.

问题回答

无,是不可能的。 您必须重新撰写文件,在开始时插入案文。

EDIT:如果你使用临时档案,你可以避免阅读整个档案:

  1. Write the value you want inserted at the beginning of the file
  2. Read X bytes from the old file
  3. Write those X bytes to the new file
  4. Repeat 2,3 until you are done reading the old file
  5. Copy the new file to the old file.

没有什么简单的方法,因为实际操作并不简单。 当档案存放在磁盘上时,在档案开始之前没有空洞的 by,因此,你只能把数据放在桌面上。 缺乏理想的通用解决办法——通常是指复制全部数据,以便使之有空间。

因此,C决定了你如何解决这一问题。

只是想对以下一些更为绝对的要求作出反驳:

无法将数据输入档案。

由于某些限制,存在缺陷。

当档案存放在磁盘上时,在档案开始之前没有空洞的 by,因此,你只能把数据放在桌面上。

在按批次处理卷宗的抽象程度处理时,情况可能如此。 然而,档案系统通常将档案作为序列号储存,而某些档案系统则允许在这一级更容易自由查阅。

鉴于某些抵消/宽松的限制,Ltlin+(XFS,分机4)和4.2+(XFS,分机4)使你能够用<代码>falloate在档案中添加漏洞:

典型的情况是,被抵消和透镜必须是档案系统符合逻辑的组群规模的多种,根据档案系统类型和配置而有所不同。

StackExchange网站的例子可以通过网上查询,以收集原始资料。

无法将数据输入档案。

审问者还说,他们想解决问题的唯一途径是把整个档案读到记忆中,并再次予以书写。 这里还有其他方法。

  1. 1. 核销了已知长度为零的股东。 只要你不超出持单人的规模,你就能够回避档案,并撰写有关数据的文件。

    一个简单的例子是,在开始时写出一个未签字的字体的大小,这代表了后面的线数,但在你达到目的之前,无法填满,并且能够更换手稿并重写正确的价值。

    <>说明: 不同平台上的一些C版本要求你最终在文件结尾处放手,然后关闭文件手,以便正确开展工作。

  2. 将新数据输入新档案,然后将旧数据输入新档案。 删除旧档案,然后将新档案改名为旧档案名称。 不使用复印件,是浪费时间。

所有方法均在磁盘大小与记忆和CPU的使用之间交易。 所有这些都取决于您的申请要求。

严格地说,如果你在很长的档案中重新添加短篇,你就可以给你希望增加的同样长的缓冲,并且可以把额外性分为几类:

//also serves as the buffer; the null char gives the extra char for the begining
char insRay[] = "[inserted text]";

printf("inserting:%s size of buffer:%ld
", insRay,sizeof(insRay));

//indecies to read in and out to the file
int iRead = sizeof(insRay)-1;
int iWrite = 0;

//save the input, so we only read once
int in =   ;

do{
    in = fgetc(fp);
    
    //don t go to next char in the file
    ungetc(in,fp);
    
    if(in != EOF){
        //preserve og file char
        insRay[iRead] = in;
        
        //loop
        iRead++;
        if(iRead == sizeof(insRay))
            iRead = 0;
        
        //insert or replace chars
        fputc(insRay[iWrite],fp);
        
        //loop
        iWrite++;
        if(iWrite == sizeof(insRay))
            iWrite = 0;
    
    }
    
}while(in != EOF);


//add buffer to the end of file, - the char that was null before
for(int i = 0; i < sizeof(insRay)-1;i++){
    fputc(insRay[iWrite],fp);
    
    iWrite++;
    if(iWrite == sizeof(insRay))
        iWrite = 0;
}




相关问题
Fastest method for running a binary search on a file in C?

For example, let s say I want to find a particular word or number in a file. The contents are in sorted order (obviously). Since I want to run a binary search on the file, it seems like a real waste ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Tips for debugging a made-for-linux application on windows?

I m trying to find the source of a bug I have found in an open-source application. I have managed to get a build up and running on my Windows machine, but I m having trouble finding the spot in the ...

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

Encoding, decoding an integer to a char array

Please note that this is not homework and i did search before starting this new thread. I got Store an int in a char array? I was looking for an answer but didn t get any satisfactory answer in the ...

热门标签