分段错误,我不知道为什么? [英] Segmentation fault, I dont know why?
问题描述
大家好,有人可以帮我,我的程序报废,当我调试时需要SIGSEGV Segmentation Fault并显示我的CPU窗口。它只需要在Dev-C ++中,在visual studio中它很顺利,但我希望它能在Dev-C ++中完成。调试中的光标停留在第30行(str2 [k] = str1 [j];)。我有32位操作系统。
感谢您的帮助。
< pre lang =c#> ;
#include< stdio.h>
#include< string.h>
//príklad2
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] ='\'';
fprintf(fw,%s,str2);
fclose(fw);
返回0;
}
我的尝试:
因为我有点新编程我不知道这个错误(我第一次得到它)我知道这是memmory acces等。
引用:while(str1 [i]!= EOF)
循环不太可能将结束(你没有在
str1
中存储EOF)。
你要么或
- 要跟踪读取的字符数,比如说
count
并在循环停止条件下使用它。
- 将停止条件更改为
(str1 [i]!='\ 0')
。
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.
Quote:while (str1[i] != EOF)
It is very unlikely that the loop will end (you didn't store EOF in
str1
).
You eitheror
- 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')
.
这篇关于分段错误,我不知道为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!