6 Replies - 293 Views - Last Post: 19 November 2012 - 03:20 PM Rate Topic: -----

#1 sumsar1812  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 19-November 12

Segmentation fault (core dumped) why ?

Posted 19 November 2012 - 07:04 AM

Hi so I'm working on an exam projekt and i need to read this file, but when i try to call the function I made below, I just get Segmentation fault. I tried switch EOF out with '\n' and that works fine but i want too read the whole file and not only 1 line.
void readFile(char str[])
{
  FILE *result_input;
  int i = 0;
  int ch;
  result_input = fopen("results.txt","r");
  while ((ch = fgetc(result_input)) != EOF){
    str[i] = ch;
    i++;
  }
  str[i] = '\0';
}



Is This A Good Question/Topic? 0
  • +

Replies To: Segmentation fault (core dumped) why ?

#2 JackOfAllTrades  Icon User is online

  • Saucy!
  • member icon

Reputation: 5954
  • View blog
  • Posts: 23,222
  • Joined: 23-August 08

Re: Segmentation fault (core dumped) why ?

Posted 19 November 2012 - 07:17 AM

You don't bother to check that the pointer returned by fopen() is valid before trying to use it. You should never do that. If fopen() fails, then NULL is returned.
Was This Post Helpful? 0
  • +
  • -

#3 sepp2k  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2013
  • View blog
  • Posts: 3,038
  • Joined: 21-June 11

Re: Segmentation fault (core dumped) why ?

Posted 19 November 2012 - 07:23 AM

Are you sure that the str array is large enough to hold the file's contents? That seems like the most likely culprit.

View PostJackOfAllTrades, on 19 November 2012 - 03:17 PM, said:

You don't bother to check that the pointer returned by fopen() is valid before trying to use it. You should never do that. If fopen() fails, then NULL is returned.


True, but given that he says it works if he only reads one line, that's probably not what's causing the segfault in this case.
Was This Post Helpful? 2
  • +
  • -

#4 sumsar1812  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 19-November 12

Re: Segmentation fault (core dumped) why ?

Posted 19 November 2012 - 07:23 AM

oh yea totaly forgot about that thanks, but it did not fix my error ?

updated code
void readFile(char str[])
{
  FILE *result_input;
  int i = 0;
  int ch;
  result_input = fopen("results.txt","r");
  if (result_input != NULL)
  {
    while ((ch = fgetc(result_input)) != EOF){
      str[i] = ch;
      i++;
    }
    str[i] = '\0';
  }
}


Was This Post Helpful? 0
  • +
  • -

#5 JackOfAllTrades  Icon User is online

  • Saucy!
  • member icon

Reputation: 5954
  • View blog
  • Posts: 23,222
  • Joined: 23-August 08

Re: Segmentation fault (core dumped) why ?

Posted 19 November 2012 - 07:26 AM

Oh, I missed that sepp2k.

How are you calling this function?
Was This Post Helpful? 0
  • +
  • -

#6 sumsar1812  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 19-November 12

Re: Segmentation fault (core dumped) why ?

Posted 19 November 2012 - 07:28 AM

View Postsepp2k, on 19 November 2012 - 07:23 AM, said:

Are you sure that the str array is large enough to hold the file's contents? That seems like the most likely culprit.

View PostJackOfAllTrades, on 19 November 2012 - 03:17 PM, said:

You don't bother to check that the pointer returned by fopen() is valid before trying to use it. You should never do that. If fopen() fails, then NULL is returned.


True, but given that he says it works if he only reads one line, that's probably not what's causing the segfault in this case.

i was thinking 12000 was enough space but seems not, just called strlen and apperently it is over 13000. So yea the array was too small. Thanks!
Was This Post Helpful? 0
  • +
  • -

#7 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1046
  • View blog
  • Posts: 4,451
  • Joined: 09-June 09

Re: Segmentation fault (core dumped) why ?

Posted 19 November 2012 - 03:20 PM

void readFile(char str[])
{
  FILE *result_input;
  int i = 0;
  int ch;
  result_input = fopen("results.txt","r");
  while ((ch = fgetc(result_input)) != EOF){
    str[i] = ch;
    i++;
  }
  str[i] = '\0';
}



This code is extremely dangerous. This is one of the biggest security flaws in C programming. If a sophisticated user wanted to, they could over run your str memory and actually overwrite informtion stored in readFile's stack frame to run their own instructions. You should always have a limit to the amount of data that a user can enter.

void readFile(char str[], int size)
{
  FILE *result_input;
  int i = 0;
  int ch;
  result_input = fopen("results.txt","r");
  //limit str to only size characters
  while (size-- && (ch = fgetc(result_input)) != EOF){
    str[i++] = ch;
  }
  str[i] = '\0';
}


Was This Post Helpful? 1
  • +
  • -

Page 1 of 1