C中的文件:如何检查文件中是否存在数据? [英] Files in C: How to check if data is present in a file r not

查看:114
本文介绍了C中的文件:如何检查文件中是否存在数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请帮助,

我创建了一个文件,可以写入该文件,甚至可以追加它.

所以我的问题是:

在将一些数据附加到该文件之前,我想检查该文件在文件上是否具有相同的数据,
如果它具有相同的数据,那么我不想在该文件上写,否则要写入该文件.

Help please,

I have created a file and I can write to that file and can even append it.

so my question is:

Before appending some data to that file I wanted to check if that file has the same data on the file,
If it has the same data then I don''t want to write on that file else write to that file.

Help appreciated and program codes most honored.

推荐答案

hi
将指针移动到文件的结尾和开头,并使用tellp()或tellg()方法获取位置(这些是必须寻找c方法的c ++方法)
确定位置后,计算差值.
如果将其设置为零,则其中没有内容写入.

我发现一个链接可能对您有帮助,但我不确定


http://stackoverflow.com/questions/238603/how- can-i-get-a-files-size-in-c [
hi
Move the pointer to end and start of the file and get the position using tellp() or tellg() methods(these are c++ methods you have to look for c methods)
Once you have the position calculate the difference.
If you''ll get it to be zero then there is no content written in it.

i Have found one link may be it helps you but i''m not sure


http://stackoverflow.com/questions/238603/how-can-i-get-a-files-size-in-c[^]

Thanks


比较文件数据是否与您的内存数据相同:

1)获取文件大小(例如,您可以在C ++中使用GetFileSize)
2)如果文件大小与您的内存数据大小不同,请转到第4点
3)如果文件和mem数据大小相同,则打开文件进行读取(请参阅注1),然后将其与内存中的数据进行比较(请参阅注3).如果在到达文件结尾之前发现不匹配,请转到步骤4.否则,请关闭文件并退出而不覆盖(数据相同).
4)用mem数据覆盖文件.最简单的方法是使用Create/Write属性关闭并重新打开文件,然后将整个内存数据块写入文件.

注意1:我的偏好是在第3步中打开文件以进行只读访问,并在第4步中关闭并重新打开它.但是您也可以在读/写模式下打开它,避免关闭/重新打开.关于个人品味.

注意2:我假设如果它们的大小不同,即使所有可用字节数据都匹配,那么您也要覆盖.

注意3:您可以逐字节读取和比较,也可以将整个文件读取到另一个缓冲区并进行内存比较.首先不需要任何额外的内存分配,所以如果速度不是问题,我会去做.
To compare if the file data is identical to your data in memory:

1) Get the file size (for example you could use GetFileSize in C++)
2) If the file size is not the same as the size of your mem data, go to point 4
3) If the file and mem data are the same size, then open the file for read (see Note 1) and compare with your data in memory (See note 3). If you find a mismatch before the end of the file is reached, go to step 4. Otherwise, close the file and exit without overwriting (data identical).
4) Overwrite the file with mem data. Easiest way, close and reopen file with Create/Write attribute and then write the whole chunk of your memory data to file.

Note1 : my preference would be to open file in step 3 for read access only and close and re-open it in step 4. But you can also open it in read/write mode and avoid closing/reopening. A matter of personal taste.

Note2: I assumed that if they are different size, even if all available bytes of data match, then you want to overwrite as well.

Note3: You can either read and compare byte-by-byte, or, alternatively read the whole file into another buffer and do mem compare. First would not require any additional memory allocation so I would go for it if speed is not an issue.


这篇关于C中的文件:如何检查文件中是否存在数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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