Click here to Skip to main content
13,740,710 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;
		i++;
	}
	str1[i]= '\0';
	fclose (fr);
	printf("%s",str1); 
	i=0;
	while (str1[i] != EOF)
	{		
		if(str1[i] == '\n')	
		{	
			for (int j=i-1;j>=f;j--)
			{
				str2[k] = str1[j];
				k++;
			}
			f = i+1;
			str2[k] = '\n';
			k++;
		}
		i++;
	}
	str2[k] = '\0';
	
	fprintf(fw,"%s",str2);
	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.
Posted 2-Nov-17 22:41pm
Updated 2-Nov-17 22:56pm

1 solution

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

Solution 1

Quote:
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.
or
  • Change the stop condition to (str1[i] != '\0').
  Permalink  

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
Web05-2016 | 2.8.180920.1 | Last Updated 3 Nov 2017
Copyright © CodeProject, 1999-2018
All Rights Reserved.
Layout: fixed | fluid

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