-3

I am trying to make a code that reads from a txt file the names, genders, current relationships and current school. I try to identify if a letter in the txt file is for example "z" (as in a Woman) but every time it gets to strcmp it crashes and outputs with -1073741819

I've tried different methods of inputting stuff into strcmp, like using "" instead of char. That has not worked out though.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main()
{
    FILE *f;
    if ((f=fopen("data.txt","r"))==NULL)
        {
            printf("File cannot be opened or does not exist");
        }
    else
        {
            char data[20],girl_c;
            int girl,high,college,divorced;
            girl_c = 'z';
            girl=0;
            high = 0;
            college = 0;
            while (!feof(f))
                {
                    fgets(data,20,f);
                    printf("I've just read: %s\n",data);
                    if (strcmp(girl_c,data) == 0)
                        {
                            printf("Its a woman.");
                        }
                }
            if (high > college)     //this is the end part of the code that i 
                                      premade, of course its not functioning 
                                      yet
                {
                    printf("There are more divorced highschool girls (%d) than college girls (%d).",high,college);
                }
            else if (college > high)
                {
                    printf("There are more divorced college girls (%d) than highschool girls (%d).",college,high);
                }
            else if (college == high)
                {
                    printf("There is equal amount of divorced college (%d) and highschool girls (%d).",college,high);
                }
            else
                {
                    printf("Data provided is insufficient or wrong.");
                }
        }
}

//code not yet finished as it cannot go past the strcmp and unless i fix its not worth going further

The problematic part is:

char data[20],girl_c;
int girl,high,college,divorced;
girl_c = 'z';
girl = 0;
high = 0;
college = 0;
while (!feof(f))
    {
        fgets(data,20,f);
        printf("I've just read: %s\n",data);
        if (strcmp(girl_c,data) == 0)
            {
                printf("Its a woman.");
            }
    }

It crashes on the strcmp.

the txt file is here: https://pastebin.com/T0FeKTwJ (Name; m = man, z = woman; s = single, v = married, r = divorced; z = elementary, s = high school, v = college)

As I said it goes to line 23 and then crashes and outputs with

Process returned -1073741819 (0xC0000005)   execution time : 1.202 s
Press any key to continue.
John Kugelman
  • 349,597
  • 67
  • 533
  • 578
TheBlue22
  • 39
  • 2
  • 3
    I don't know how this even compiles. You should get an error like: 'int strcmp(const char *,const char *)': cannot convert argument 1 from 'char' to 'const char *'. – John Kugelman Oct 28 '19 at 20:16
  • girl_c is a char, not a char*, so strcmp() won't know what to do with it. – Lee Daniel Crocker Oct 28 '19 at 20:17
  • 5
    Aside: please see [Why is `while ( !feof (file) )` always wrong?](http://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong) Replace with `while(fgets(data, 20, f) != NULL)`. Note too that there will be a newline in the input buffer (if room) which will prevent it being the same as another name. Please see [Removing trailing newline character from fgets() input](https://stackoverflow.com/questions/2693776/removing-trailing-newline-character-from-fgets-input/28462221#28462221) – Weather Vane Oct 28 '19 at 20:18

1 Answers1

0

For each line in your input file,fgets stops when it encounters a newline character \n. It includes this newline character in the array, which is then terminated by NULL '\0.

Keeping this in mind,you'll have to rephrase the declaration of girl_c in your code to:

char girl_c[] = "z\n";to include the character you are searching for and also the newline character.

 else
    {
        char data[20];
        char girl_c[] = "z\n";//declare girl_c as a character array
        int girl,high,college,divorced;
        girl=0;
        high = 0;
        int i = 0;
        college = 0;
        while (!feof(f))//
          {
                fgets(data,20,f);
                printf("I've just read: %s\n",data);
                if (strcmp(girl_c,data) == 0)//strcmp would work fine
                    {
                        printf("Its a woman.\n");
                    }
          }
    }

Or if you would still want to use a character constant girl_c = 'z';, you'll need to drop strcmp since it requires it's arguments to be character strings.

Your code should then measure like this....

else
    {
    char data[20],girl_c;
    int girl,high,college,divorced;
    girl_c = 'z';
    girl=0;
    high = 0;
    int i = 0;
    college = 0;
    while (!feof(f))//
      {
            fgets(data,20,f);
                printf("I've just read: %s\n",data);
                if (girl_c == data[0])//compare 1st character of each line
                    {
                        printf("Its a woman.\n");
                    }
      }
    }
Lily AB
  • 392
  • 2
  • 6