Jump to content

Segmentation fault please help

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
2 replies to this topic

#1
summoner90

summoner90

    Newbie

  • Members
  • Pip
  • 1 posts
I dont know whats wrong with my code so ig anyone could please help me i keep getting segmetation fault

#include<stdio.h>

int main(){
char s[100];
FILE*f=fopen("rec1.txt","r");
if(f==NULL)printf("greska");
FILE*g=fopen("reci2.txt","w");
if(g==NULL)printf("greska");

while(fscanf(f,"%s",s)!=EOF){
fprintf(g,"%s",s);}

fclose(f);
fclose(g);
}

#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
If f or g is NULL, it still attempts to read from f and write to g.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
ZekeDragon

ZekeDragon

    Writes binary right handed and hex left handed

  • Moderators
  • 2,103 posts
Further, if rec1 has more than 100 characters you'll get a buffer overflow.
Wow I changed my sig!