使用C与第二和截断替换的文件的前半? [英] Using C to replace the first half of a file with the second and truncating?

查看:151
本文介绍了使用C与第二和截断替换的文件的前半?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个很大的文件,一旦达到一定规模,我想完全删除上半年和下半年按住Shift键,这实际上使得一半大小。继承人是我在想什么:

So I have a big file, and once it reaches a certain size, I want to totally remove the first half and shift the second half down, effectively making it half the size. Heres's what I'm thinking:

FILE *fp, *start;
int ch, block_length, filesize;
char c;

//open the file and initialize pointers
fp = fopen(FILEPATH, "rb+");
start = fp;
rewind(start);

//Check the size of the file
fseek(fp, 0, SEEK_END);
filesize = ftell(fp);    

if(filesize >= LOG_MAX_FILE_SIZE)
{
  //Go to middle of file
  fseek(fp, (-1) * LOG_MAX_FILE_SIZE/2, SEEK_END);

  //Go forwards until you get a new line character to avoid cutting a line in half
  for(;;)
  {
     //Read char
     fread(&ch, 1, 1, fp);

     //Advance pointer
     fseek(fp, 1, SEEK_CUR);

     if( (char)ch == '\n' || ch == EOF)
        break;
  }

  //fp is now after newline char roughly in middle of file

  //Loop over bytes and put them at start of file until EOF
  for(;;)
  {
     //Read char
     fread(&ch, 1, 1, fp);

     //Advance pointer
     fseek(fp, 1, SEEK_CUR);

     if(ch != EOF)
     {
        c = (char)ch;
        fwrite(&c,1,1,start);
        fflush(start);

        //Advance start
        fseek(start, 1, SEEK_CUR);
     }

     else
        break;
  }

  //Calculate length of this new file
  block_length = ftell(start);

  //Go back to start
  rewind(start);

  //Truncate file to block length
  ftruncate(fileno(start), block_length);

}

不过,这似乎是在做一些非常非常奇怪的事情(填充有F的文件,混合了在他们里面的线条和一些字符等)。没有人有任何想法,我可能会在这个code错在做什么?由于事先!

But, this seems to be doing some very very strange things (padding the file with 'f''s, mixing up lines and some characters inside them, etc.). Does anyone have any ideas as to what I may be doing wrong in this code? Thanks beforehand!

推荐答案

我认为部分问题是,你正在使用 fseek的而读。该调用fread和fwrite提前文件指针。如果你上调用fseek,它将然后跳到下一个字符。

Part of the problem I think is that you are using fseek while reading. The call to fread and fwrite advance the file pointer. If you call fseek, it will then skip the next character.

在code以下序列中, FREAD 通话将读取字符并推进当前偏移到下一个字符。随后 fseek的然后跳过该字符,并移动到下一个。因此,它会读取第二个字符。

In the following sequence of code, the fread call will read a character and advance the current offset to the next character. The subsequent fseek then skips that character and moves to the next. So it will read every second character.

fread(&ch, 1, 1, fp);
fseek(fp, 1, SEEK_CUR);

同样的问题与写入调用存在(它不需要后续搜索)。此外,由于编辑的任择议定书表明,启动和FP值是相同的,逻辑将是不正确的(你需要单独的文件指针使用逻辑)。

The same issue exists with the write call (it does not need a subsequent seek). Also, since the edit to the OP shows that start and fp are the same value, the logic will not be correct (you would need separate file pointers to use that logic).

这篇关于使用C与第二和截断替换的文件的前半?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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