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.
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");
}
}
}
User contributions licensed under CC BY-SA 3.0