如何使用Delphi从二进制文件中移除特定字符?
- 内容介绍
- 文章标签
- 相关推荐
本文共计583个文字,预计阅读时间需要3分钟。
要从文件中删除一个字符或字符串,你可以按照以下步骤操作:
1. 使用`BlockRead`读取文件内容到内存中。
2.找到要删除的字符或字符串的位置。
3.使用`Delete`函数从字符串中删除指定位置的字符或字符串。
4.将修改后的字符串写回文件。
以下是一个简化的示例代码,展示如何实现这个过程:
c
#include #includevoid deleteCharFromString(char *source, int startPos, int endPos) { if (startPos strlen(source)) { printf(Invalid position.\n); return; }
// 计算删除后的字符串长度 int newLength=strlen(source) - (endPos - startPos);
// 移动字符以覆盖要删除的部分 memmove(source + startPos, source + endPos, newLength);
// 字符串末尾添加空字符 source[newLength]='\0';}
int main() { // 假设有一个文件名为example.txt FILE *file=fopen(example.txt, r+); if (file==NULL) { printf(Error opening file.\n); return 1; }
// 读取文件内容到缓冲区 char buffer[1024]; fread(buffer, sizeof(char), sizeof(buffer), file);
// 假设我们要删除第5个字符 int charToDelete=5; deleteCharFromString(buffer, charToDelete - 1, charToDelete); // -1 因为字符串索引从0开始
// 定位到文件开头 fseek(file, 0, SEEK_SET);
// 写回修改后的字符串 fwrite(buffer, sizeof(char), strlen(buffer), file);
// 关闭文件 fclose(file);
return 0;}
这段代码假设你要删除的字符或字符串在文件中的位置是已知的。`deleteCharFromString`函数负责删除指定位置的字符或字符串,然后`main`函数负责打开文件、读取内容、调用删除函数、写回文件并关闭文件。
我想从二进制文件/文本文件中删除一个字符/字符串.如果我从文件中知道char / string的位置,我该如何删除它?我应该读取文件(BlockRead),删除char / string(使用Delete(source,startPos,endPos)然后将(BlockWrite)写入新文件,或者我可以直接从指定文件中删除?谢谢
您可以使用TFileStream.CopyFrom方法复制到不需要的字符串之前,先搜索它,然后再复制CopyFrom文件的其余部分. TFileStreams非常快.像这样的东西(未经测试)
aInFile := TFileStream.Create(sInput, fmOpenRead); try aOutFile := TFileStream.Create(sOutput, fmCreate); try aOutFile.CopyFrom(aInFile, Pos); aInFile.Seek(Skip); aOutFile.CopyFrom(aInFile, aInfile.Size - Pos - Skip); finally aOutFile.Free; end; finally aInFile.Free; end;
本文共计583个文字,预计阅读时间需要3分钟。
要从文件中删除一个字符或字符串,你可以按照以下步骤操作:
1. 使用`BlockRead`读取文件内容到内存中。
2.找到要删除的字符或字符串的位置。
3.使用`Delete`函数从字符串中删除指定位置的字符或字符串。
4.将修改后的字符串写回文件。
以下是一个简化的示例代码,展示如何实现这个过程:
c
#include #includevoid deleteCharFromString(char *source, int startPos, int endPos) { if (startPos strlen(source)) { printf(Invalid position.\n); return; }
// 计算删除后的字符串长度 int newLength=strlen(source) - (endPos - startPos);
// 移动字符以覆盖要删除的部分 memmove(source + startPos, source + endPos, newLength);
// 字符串末尾添加空字符 source[newLength]='\0';}
int main() { // 假设有一个文件名为example.txt FILE *file=fopen(example.txt, r+); if (file==NULL) { printf(Error opening file.\n); return 1; }
// 读取文件内容到缓冲区 char buffer[1024]; fread(buffer, sizeof(char), sizeof(buffer), file);
// 假设我们要删除第5个字符 int charToDelete=5; deleteCharFromString(buffer, charToDelete - 1, charToDelete); // -1 因为字符串索引从0开始
// 定位到文件开头 fseek(file, 0, SEEK_SET);
// 写回修改后的字符串 fwrite(buffer, sizeof(char), strlen(buffer), file);
// 关闭文件 fclose(file);
return 0;}
这段代码假设你要删除的字符或字符串在文件中的位置是已知的。`deleteCharFromString`函数负责删除指定位置的字符或字符串,然后`main`函数负责打开文件、读取内容、调用删除函数、写回文件并关闭文件。
我想从二进制文件/文本文件中删除一个字符/字符串.如果我从文件中知道char / string的位置,我该如何删除它?我应该读取文件(BlockRead),删除char / string(使用Delete(source,startPos,endPos)然后将(BlockWrite)写入新文件,或者我可以直接从指定文件中删除?谢谢
您可以使用TFileStream.CopyFrom方法复制到不需要的字符串之前,先搜索它,然后再复制CopyFrom文件的其余部分. TFileStreams非常快.像这样的东西(未经测试)
aInFile := TFileStream.Create(sInput, fmOpenRead); try aOutFile := TFileStream.Create(sOutput, fmCreate); try aOutFile.CopyFrom(aInFile, Pos); aInFile.Seek(Skip); aOutFile.CopyFrom(aInFile, aInfile.Size - Pos - Skip); finally aOutFile.Free; end; finally aInFile.Free; end;

