Click here to Skip to main content
15,885,182 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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
Updated 2-Nov-17 22:56pm

1 solution

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').
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900