我能够阅读.fna并搜索我想要的模式。但在我读完文件后,它是逐行读文件,而不是整个文件。如何在C编程中提取.fna文件并存储为变量?下面是我的代码和我得到的输出:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXCHAR 70000
void search(char* pattern, char* text)
{
int M = strlen(pattern);
int N = strlen(text);
for (int x = 0; x <= N - M; x++) {
int y;
for (y = 0; y < M; y++)
if (text[x + y] != pattern[y])
break;
if (y == M)
{
printf("Found pattern at position %d \n", x+1);
}
}}
int main()
{
FILE *fp;
char str[MAXCHAR];
char pattern[] = "GTTCTTT";
char* filename = "D:\\Desktop\\NC_007409.fna";
fp = fopen(filename, "r");
if (fp == NULL){
printf("Could not open file %s",filename);
return 1;
}
while (fgets(str, MAXCHAR, fp) != NULL)
search(pattern, str);
return 0;
}发布于 2020-09-15 15:38:28
您可以通过两种方式获取字节数组形式的文件内容:
手动分配数组(简单方法)
使用文件映射(高级方式)
内存映射文件的好处是提高了I/O性能,尤其是在大型文件上使用时。对于小文件,内存映射文件可能会导致闲置空间的浪费,因为内存映射始终与页面大小对齐,页面大小通常为4 KiB。
https://stackoverflow.com/questions/63895163
复制相似问题