Click here to Skip to main content
13,836,537 members
Rate this:
Please Sign up or sign in to vote.
See more:
Hi guys, can someone help me, my program scrashing and when i debugging it takes "SIGSEGV Segmentation Fault" and show me my CPU window. It takes only in Dev-C++, in visual studio it goes well, but i want it to do in Dev-C++. Cursor in debug stay at line 30 (str2[k] = str1[j];). I have 32-bit OS.
Thanks for help.

<pre lang="c#">
#include <stdio.h>
#include <string.h>

//príklad 2

int main()

	FILE *fr,*fw;
	int c,i=0,k=0,f = 0;
	char str1[50];
	char str2[50];
	fr = fopen ("vstup.txt","r");
	fw = fopen ("vystup.txt","w");
	while ((c=getc(fr)) != EOF){
		str1[i] = c;
	str1[i]= '\0';
	fclose (fr);
	while (str1[i] != EOF)
		if(str1[i] == '\n')	
			for (int j=i-1;j>=f;j--)
				str2[k] = str1[j];
			f = i+1;
			str2[k] = '\n';
	str2[k] = '\0';
	fclose (fw);
	return 0;

What I have tried:

Cause i am a little bit new in programming i didn't know about this fault(first time i get it) i know that is something with memmory acces etc.
Updated 2-Nov-17 23:56pm

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

while (str1[i] != EOF)
It is very unlikely that the loop will end (you didn't store EOF in str1).

You either
  • Keep track of the number of read characters, say count
    and use it in the loop stop condition.
  • Change the stop condition to (str1[i] != '\0').

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Cookies | Terms of Service
Web06 | 2.8.190114.1 | Last Updated 3 Nov 2017
Copyright © CodeProject, 1999-2019
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100