-1

I was wondering how to solve a Core dumped issue on my C code.

When I compile it with: g++ -g MatSim.cpp -o MatSim -lstdc++ -O3, I get three warnings, this is one (The other two are similar and are only differentiated by the string variable name):

MatSim.cpp: In function ‘int main()’:
MatSim.cpp:200037:27: warning: ignoring return value of ‘int fscanf(FILE*, const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
 fscanf(TM,"%255s",string2);

The principal aspects of my code and the related part that the compiler reports:

#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <iostream>
#include <fstream>
#include <string.h>

using namespace std;

int to_int(char string[256])
{
    if( strcmp(string,"0") == 0 )
    {
        return 0;
    }

...

    else if( strcmp(string,"50000") == 0 )
    {
        return 50000;
    }
    return -1;
}   

int main()
{
int a,b,div,value,k,i,j,tm,ler; 
    char string[256];
    char string1[256];
    char string2[256];

FILE *TM;
TM = fopen("TM","r");
if( (TM = fopen("TM","r")) == NULL)
{  
    printf("Can't open %s\n","TM");
    exit(1);
}
fscanf(TM,"%255s",string2);
tm = to_int(string2);
fclose(TM);

...

}

I have tried the reported suggestion in here and I tried to understand what was posted in here. But, I don't see its application on my code.

Finally, when I run the exe file, it returns:

Segmentation fault (core dumped)`.

Community
  • 1
  • 1
Another.Chemist
  • 2,386
  • 3
  • 29
  • 43
  • `TM = fopen("TM","r"); if( (TM = fopen("TM","r"))` Why are you opening the file twice? – PaulMcKenzie Jun 16 '15 at 16:33
  • My tough was: First, open the file and then check if the file is ok – Another.Chemist Jun 16 '15 at 16:34
  • 1
    `MatSim.cpp:200037:27` - wow. *thank you* for not posting the other 200000 lines of code. – WhozCraig Jun 16 '15 at 16:34
  • @beginner But that is not what that code you have does. – PaulMcKenzie Jun 16 '15 at 16:35
  • @beginner Also, if you're using C++, you should be aware of things such as `streams` and usage of `std::string`. So pick a language -- is it C or C++? – PaulMcKenzie Jun 16 '15 at 16:38
  • @PaulMcKenzie I bet you are completely right what your are suggesting me. But, I don't have the proper answer because I'm a beginner. I would be thankful if you recommend me some literature to know what you are asking me. Thank you so much in advance : ) – Another.Chemist Jun 16 '15 at 16:39
  • His point is you are using C language calls and structures not C++, yet the question is tagged with C++. Likely he wants to update the tagging to more reliably represent the question. As for how to use strings and streams in C++, the list of literature is quite deep. I'd start with a web search for C++ IO stream tutorial . If you get through that without also coverng strings, I'll be amazed. – user4581301 Jun 16 '15 at 16:59

2 Answers2

2

In your code, you're fopen()ing the file twice. Just get rid of the

 TM = fopen("TM","r");

before the if statement.

That said, you should check the value of fscanf() to ensure success. Otherwise, you'll end up reading an uninitialized array string2, which is not null-terminated which in turn invokes undefined behaviour.

Please be aware, almost all string related functions expect a null-terminated char array. If your array is not null terminated, there will be UB. Also, it is a good practice to initialize your automatic local variables to avoid possible UB in later part of code.

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
2

You are opening the file twice.

Alll you need is this:

FILE *TM = fopen("TM","r");
if (TM == NULL)  { /* file was not opened */ }
PaulMcKenzie
  • 34,698
  • 4
  • 24
  • 45
  • Thank you for your response, but I tried both suggestions and none worked : ( – Another.Chemist Jun 16 '15 at 17:02
  • No, the code works. If the file `TM` cannot be opened for reading, then `TM` is NULL. A check is made to see what `TM` was set to. So those two lines do indeed work. Your attempt absolutely did not work for the reasons stated. If you have a further issue, then it is the other parts of your code (I bet the string handling stuff) that is causing the problem. – PaulMcKenzie Jun 16 '15 at 17:55
  • Yes, you are right the only addition to omit the warning was to eliminate -O3. The rest you are right (+1). Have a good day : ) – Another.Chemist Jun 16 '15 at 18:55