OpenVZ Forum


Home » General » Support » URGENT ... Segmentation fault in program... Pls help
URGENT ... Segmentation fault in program... Pls help [message #9698] Wed, 17 January 2007 19:50 Go to previous message
turkish_van is currently offline  turkish_van
Messages: 1
Registered: January 2007
Junior Member
I have currently this C program that always gives me segmentation fault when executed.
============================================================ ====

#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<strings.h>

typedef struct amino{
char seq;
struct amino *before;
struct amino *after;
}AMINO;

int main (void)
{FILE *data, *output, *output1;
data = fopen ("Sample.txt", "r");
output = fopen ("output_sample1.txt","w");
output1 = fopen ("output_sample2.txt","w");
char input[100] = {};
AMINO *ptr;
AMINO *start;
AMINO *start1;
AMINO *next;
AMINO *pop;
AMINO *pop1;

while (fgets(input,100,data)!= NULL)
{ if (strstr(input, "SQ SEQUENCE " ) != NULL)
{ fprintf (output, "=================================\n");
start = malloc (sizeof (AMINO));
start1 = start;
start->before = NULL;
start->after = NULL;
while ( (start->seq = fgetc (data)) != '/')
{ fprintf (output, "%c", start->seq);
if (start->seq == ' ' || start->seq == '\n')
{ fprintf(output,"_");
(AMINO*)pop = (AMINO*)start;
(AMINO*)pop1 = (AMINO*)start->before;
start = (AMINO*)pop1; start->after = NULL;
free(pop);
}
next = malloc (sizeof(AMINO));
start->after = next;
next->before = start;
start = next;
start->after = NULL;
}
ptr = start1;
while(ptr->after != NULL)
{fprintf (output1, "%c", ptr->seq);
ptr = ptr->after;
}
fprintf(output1, "\n==============================\n");
free (start1);
}
}
fclose (data);
fclose (output);
fclose (output1);
return EXIT_SUCCESS;
}

========================================================
This file actually reads a file and removes subsequent new line characters and spaces. However, i had segmentation fault in the line "start = (AMINO*)pop1;" ... May i know what is wrong here and how can i change it ?
Thank you
  • Attachment: read_sprot1.c
    (Size: 2.00KB, Downloaded 221 times)
  • Attachment: Sample1.txt
    (Size: 8.13KB, Downloaded 252 times)
 
Read Message
Read Message
Read Message
Previous Topic: Linux Stable
Next Topic: *SOLVED* Installation on Debian - VFS: Cannot open root device
Goto Forum:
  


Current Time: Fri Nov 15 21:07:46 GMT 2024

Total time taken to generate the page: 0.03442 seconds