gpt4 book ai didi

c - 在 C 中将文本文件的特定部分作为字符串读取?

转载 作者:太空狗 更新时间:2023-10-29 17:04:18 25 4
gpt4 key购买 nike

我正在编写代码来读取包含 DNA 碱基的大量文本文件,我需要能够提取特定部分。该文件如下所示:

TGTTCCAGGCTGTCAGATGCTAACCTGGGG
TCACTGGGGGTGTGCGTGCTGCTCCAGCCT
GTTCCAGGATATCAGATGCTCACCTGGGGG

...

每行30个字符。

我有一个单独的文件指示这些部分,这意味着我有一个start 值和一个end 值.因此,对于每个 startend 值,我需要在文件中提取相应的字符串。例如,如果我有 start=10,end=45,我需要存储从第一行 (C) 的第 10 个字符开始并结束于单独的临时文件中第二行 (C) 的第 15 个字符。

我尝试使用如下所示的 fread 函数对包含上述几行字母的测试文件进行测试。参数为 start=1,end=90,生成的文件如下所示:

TGTTCCAGGCTGTCAGATGCTAACCTGGGG
TCACTGGGGGTGTGCGTGCTGCTCCAGCCT
GTTCCAGGATATCAGATGCTCACCTGGG™eRV

每次运行都会在最后给出随机字符。

代码:


FILE* fp;
fp=fopen(filename, "r");
if (fp==NULL) puts("Failed to open file");

int start=1, end=90;
char string[end-start+2]; //characters from start to end = end-start+1

fseek(fp, start-1, SEEK_SET);

fread(exon,1, end-start+1, fp);

FILE* tp;
tp=fopen("exon", "w");
if (tp==NULL) puts("Failed to make tmp file");

fprintf(tp, "%s\n", string);
fclose(tp);

我无法理解 fread 如何处理\n 字符,所以我尝试用以下内容替换它:

int i=0;
char ch;
while (!feof(fp))
{
ch=fgetc(fp);

if (ch != '\n')
{
string[i]=ch;
i++;
if (i==end-start) break;
}

}
string[end-start+1]='\0';

它创建了以下文件:TGTTCAGGCTGTCAGATGCTAACCTGGGGTCACTGGGGTGTGCGTGCTGCTCCAGCCTGTTCCAGGATATCAGATGCTCACCTGGGGô

(没有任何换行符,我不介意)。每次运行,我都会得到一个不同的随机字符,而不是“G”。

我做错了什么?有没有办法用 fread 或其他功能来完成它?

提前谢谢你。

最佳答案

我已经修改了您的代码并添加了注释以进行解释。

请仔细阅读。您忽略了错误检查,代码中几乎没有 undefined variable 。

我已经从 if block 返回失败,goto` 会更合适。

请引用this comment是否将 1 个字符或 2 个字符添加到 startend

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main()
{
FILE* fp;
// fp = fopen(filename, "r");
// since the filename is undeclared i have used hard coded file name
fp = fopen("dna.txt", "r");
// Nothing wrong in performing error checking
if (fp == NULL) {
puts("Failed to open file");
return -1;
}

// Make sure start is not 0 if you want to use indices starting from 1
int start = 1, end = 90;

// I would adjust the start and end index by adding count of '\n' or '\r\n' to the start and end
// Here I am adjusting for '\n' i.e 1 char
// since you have 30 chars so hardcoding it.
int m = 1; // m depends on whether it is \n or \r\n
// 1 for \n and 2 for \r\n
--start; --end; // adjusting indexes to be 0 based
if (start != 0)
start = start + (start / 30) * m; // start will be 0
if (end != 0)
end = end + (end / 30) * m; // start will be 93

// lets declare the chars to read
int char_to_read = end - start + 1;

// need only 1 extra char to append null char
// If start and end is going to change, then i would suggest using malloc instead of static buffer
// because compiler cannot predict the memory to allocate to the buffer if it is dependent on external factor
// char string[char_to_read + 1]; //characters from start to end = end-start+1

char *string = malloc(char_to_read + 1);
if (string == NULL) {
printf("malloc failed\n");
fclose(fp);
return -2;
}

// zero the buffer
memset(string, 0, char_to_read + 1);

int rc = fseek(fp, start, SEEK_SET);
if (rc == -1) {
printf("fseek failed");
fclose(fp);
return -1;
}

// exon is not defined, and btw we wanted to read in string.
int bytes_read = fread(string, 1, char_to_read, fp);

// Lets check if there is any error after reading
if (bytes_read == -1) {
fclose(fp);
return -1;
}

// Now append the null char to the end
string[bytes_read] = 0;
printf("%s\n", string);
fclose(fp);

// free the memory once you are done with it
if (string)
free(string);


// Now u can write it back to file.
// FILE* tp;
// tp=fopen("exon", "w");
// if (tp==NULL) puts("Failed to make tmp file");

// fprintf(tp, "%s\n", string);
// fclose(tp);
}

关于c - 在 C 中将文本文件的特定部分作为字符串读取?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56668075/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com