在磁盘上写入文件时出现问题 [英] Problem with writing files on disk

查看:68
本文介绍了在磁盘上写入文件时出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我应该在磁盘上写1024个文件.文件的内容是一个简单的计数器.例如,当我写入64个文件时,一切正常,但是当文件数增加时,文件的内容就会损坏.步骤如下:
线程从FIFO读取长度为32KB的缓冲区. FIFO是使用链接列表实现的.在此线程中,读取的数据被写入磁盘.写入过程如下:

I should write 1024 files on disk. The content of files are a simple counter. When I write for example 64 files, every thing is OK but when the number of file increases, the content of file are corrupted. The procedure is as follow:
A thread reads buffers with 32KB length from a FIFO. FIFO is implemented using Link List. In this thread, the read data is written on disk. The write procedure is as follow:

char path[MAX_PATH+1];
char ch_itoa[8];
strcpy(path, "d:\\counter\\");
itoa(class_pointer->m_startWriteLL->index, ch_itoa, 10);
strcat(path, ch_itoa);
strcat(path, ".bin");

HANDLE fHandle;
fHandle = CreateFile(CA2W(path), GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
DWORD dwPos = SetFilePointer(fHandle, 0, NULL, FILE_END);
DWORD dwBytes = class_pointer->m_startWriteLL->size;
bool bRet = LockFile(fHandle, dwPos, 0, dwBytes, 0);
WriteFile(fHandle, class_pointer->m_startWriteLL->data, dwBytes, &dwBytesWritten, NULL);
bRet = UnlockFile(fHandle, dwPos, 0, dwBytes, 0);
CloseHandle(fHandle);


首先,我打开适当的文件,然后查找到文件的末尾,然后将数据(32 KB)写入文件,最后关闭文件.在下一次,为了将新数据写入文件,重复此过程.全部1024个文件均如上所述编写.正如我提到的,当文件数很少时,没有问题,但是当我应该将数据写入1024个文件时,文件已损坏.我应该提到的是,每次都只打开一个文件进行写入.谁能说我为什么要腐败?解决方案是什么?

[edit]已添加代码块-OriginalGriff [/edit]


At first I open appropriate file and then seek to the end of file, then I write data (32 KB) to the file and finally I close file. At next time, for writing new data to the file, this procedure is repeated. All of 1024 files are written as above. As I mentioned, when the number of files is little, there is no problem but when I should write data to 1024 files, files are corrupted. I should mention that at each time, just one file is opened for writing. Can any body say me why there is corruption? What is the solution?

[edit]Code block added - OriginalGriff[/edit]

推荐答案

一些想法/问题可能有助于解决此问题.
1.它在什么平台/操作系统上运行?您正在使用什么开发环境?
2. D驱动器是什么类型的设备(固定硬盘,记忆棒……)
3.它使用什么格式的文件系统(NTFS,FAT,HFS,...)
4.当您说腐败"时,您能否描述您的期望/获得的东西/它们之间的不同?
5.在破坏发生之前,您是否精确地浏览了可以写入多少个文件?它总是完全相同的数字吗?如果超过该数目,是仅此点之后的文件已损坏,还是所有文件都被写入?
6.您要写入的所有1,024个文件是否已经存在,还是仅在创建新文件时(或仅在将现有文件附加到新文件时)出现的问题?
7.您确定问题出在文件写入中,而不是您要写入的数据中吗?
A few thoughts / questions that might help resolve this.
1. What platform / operating system is this running on? What development environment are you using?
2. What sort of device is your D-drive (fixed hard disc, memory stick, . . .)
3. What file system is it formatted with (NTFS, FAT, HFS, . . .)
4. When you say ''corrupted'', can you describe what you expect / what you get / how they differ?
5. Have you explored exactly how many files you can write before corruption occurs? Is it always exactly the same number? If you exceed that number is it only files after that point that are corrupted or is it then all files written?
6. Do all 1,024 files you want to write already exist or is it a problem that only occurs when new files are created (or only when existing files are appended to)?
7. Are you sure that the problem is the file writing and not in the data you''re trying to write?


好吧,如果并行运行的代码存在多种可能性,这种错误.

您可以尝试这些东西.

1. WriteFile()之后的WaitForSigleObject(fHandle)
2.检查API的返回状态
3.登录dwBytes并检查它们是否等于32 o.


希望这些对您有所帮助.
Well, if the code running in parallel there are so many possibilities of this kind of error.

You can try for these things.

1.WaitForSigleObject(fHandle) after WriteFile()
2.Check the return status of the API''s
3.Log dwBytes and check they are equal to 32 o not.


Hope these things might help you.


在发布的代码中,最可能的原因是您的链表已损坏.如果您是我,我将使用调试器检查列表中的数据是否正确(无论是写入还是读取),这是最简单的.
From the posted code, the most probable cause is your linked list being corrupted. If I were you, I would use the debugger to check that the data in the list is correct, on write or read, which ever is the easiest.


这篇关于在磁盘上写入文件时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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