मैं कुछ कोड लिख रहा हूं जिन्हें fasta files पढ़ने की आवश्यकता है, इसलिए मेरे कोड का हिस्सा (नीचे शामिल) एक फास्ट पार्सर है। चूंकि एक अनुक्रम फास्ट प्रारूप में कई लाइनों का विस्तार कर सकता है, इसलिए मुझे फ़ाइल से एक ही स्ट्रिंग में पढ़ने वाली कई लगातार पंक्तियों को जोड़ना होगा। मैं यह करता हूं, अनुक्रम की वर्तमान लंबाई और लाइन पढ़ने की लंबाई होने के लिए, प्रत्येक पंक्ति को पढ़ने के बाद स्ट्रिंग बफर को फिर से चलाकर। मैं कुछ अन्य सामान करता हूं, जैसे सफेद स्थान को अलग करना आदि। सभी के लिए अच्छा है पहला अनुक्रम, लेकिन फास्ट फाइलों में एकाधिक अनुक्रम हो सकते हैं। इसी तरह, मेरे पास दो तारों (शीर्षक, और वास्तविक अनुक्रम) के साथ "char *" होने के साथ structs की एक गतिशील सरणी है। दोबारा, जैसा कि मुझे एक नया शीर्षक मिलता है ('>' से शुरू होने वाली रेखा से पेश किया गया है) मैं अनुक्रमों की संख्या में वृद्धि करता हूं, और अनुक्रम सूची बफर को फिर से चलाता हूं। मुझे के जीवन मैं क्यों नहीं देख सकते हैं के लिएफ़ाइल क्रैश से पढ़ने के दौरान बफर का विस्तार करने के लिए रीयलोक का उपयोग करना
*** glibc detected *** ./stackoverflow: malloc(): memory corruption: 0x09fd9210 ***
Aborted
के साथ दूसरे दृश्य के लिए अंतरिक्ष आवंटन पर realloc segfaults।
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include <math.h>
#include <errno.h>
//a struture to keep a record of sequences read in from file, and their titles
typedef struct {
char *title;
char *sequence;
} sequence_rec;
//string convenience functions
//checks whether a string consists entirely of white space
int empty(const char *s) {
int i;
i = 0;
while (s[i] != 0) {
if (!isspace(s[i])) return 0;
i++;
}
return 1;
}
//substr allocates and returns a new string which is a substring of s from i to
//j exclusive, where i < j; If i or j are negative they refer to distance from
//the end of the s
char *substr(const char *s, int i, int j) {
char *ret;
if (i < 0) i = strlen(s)-i;
if (j < 0) j = strlen(s)-j;
ret = malloc(j-i+1);
strncpy(ret,s,j-i);
return ret;
}
//strips white space from either end of the string
void strip(char **s) {
int i, j, len;
char *tmp = *s;
len = strlen(*s);
i = 0;
while ((isspace(*(*s+i)))&&(i < len)) {
i++;
}
j = strlen(*s)-1;
while ((isspace(*(*s+j)))&&(j > 0)) {
j--;
}
*s = strndup(*s+i, j-i);
free(tmp);
}
int main(int argc, char**argv) {
sequence_rec *sequences = NULL;
FILE *f = NULL;
char *line = NULL;
size_t linelen;
int rcount;
int numsequences = 0;
f = fopen(argv[1], "r");
if (f == NULL) {
fprintf(stderr, "Error opening %s: %s\n", argv[1], strerror(errno));
return EXIT_FAILURE;
}
rcount = getline(&line, &linelen, f);
while (rcount != -1) {
while (empty(line)) rcount = getline(&line, &linelen, f);
if (line[0] != '>') {
fprintf(stderr,"Sequence input not in valid fasta format\n");
return EXIT_FAILURE;
}
numsequences++;
sequences = realloc(sequences,sizeof(sequence_rec)*numsequences);
sequences[numsequences-1].title = strdup(line+1); strip(&sequences[numsequences-1].title);
rcount = getline(&line, &linelen, f);
sequences[numsequences-1].sequence = malloc(1); sequences[numsequences-1].sequence[0] = 0;
while ((!empty(line))&&(line[0] != '>')) {
strip(&line);
sequences[numsequences-1].sequence = realloc(sequences[numsequences-1].sequence, strlen(sequences[numsequences-1].sequence)+strlen(line)+1);
strcat(sequences[numsequences-1].sequence,line);
rcount = getline(&line, &linelen, f);
}
}
return EXIT_SUCCESS;
}
सबस्ट्रिंग दिनचर्या के बारे में सभी टिप्पणियों के लिए धन्यवाद। मैंने इसे अपने कोड में ठीक कर दिया है। मैंने यह भी ध्यान दिया कि जिस तरह से मैंने नकारात्मक इंडेक्स के साथ निपटाया वह गलत था। मुझे नकारात्मक सूचकांक जोड़ना चाहिए, इसे घटाएं नहीं। ऐसा कहा जा रहा है, मुझे यह भी एहसास हुआ कि मैंने सबस्ट्रेट फ़ंक्शन को गलती से कॉपी किया है, क्योंकि मैं इसे चिपकाए गए कोड के बाकी हिस्सों में नहीं कहता हूं। – sirlark
'स्ट्रिप() 'भी छोटी है। यह शून्य-लंबाई तारों के साथ बुरी चीजें करेगा। ऐसा लगता है कि आप इसे इस तरह के तारों से नहीं बुलाते हैं, लेकिन मुझे लगता है कि इसे कहीं और इस्तेमाल करने के लिए ठीक करना एक अच्छी बात होगी। –