#sscanf

67 messages · Page 1 of 1 (latest)

topaz edge
#

Hey, I'm currently trying to do a project, I have in a file.txt the info of several people, each line has their age, name and gender (Ex: 18,Paul Walker,Male), I'm trying to just print the age and name of the person if they are male and just the name if they are female.

I did:

fgets(line, 200, file); //to get each string from the file.txt
sscanf(line, "%d,%[^,],%[^,]", &age, name, gender); //store each value in a variable 

however when I tried to do:

if(gender == "male"){
     printf("%d, %s\n", age, name);
} else if(gender == "female") {
     printf("%s\n", name);
}

to try to just show their name if they are female and the name + age if they are male, it doesen't work, how do I fix this? And then with the info that I just printed, how do I create a seperate file and put it in there?

remote sealBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question run !howto ask.

topaz edge
#

Updated ^^^^^^^^^^^^

mossy knot
#

You cannot compare strings using ==.
That would be a pointer comparison.
You really want to use strcmp().

topaz edge
mossy knot
#
if (strcmp(gender, "male") == 0) {}
#

Should have mentioned that strcmp() returns 0 if the strings match.

topaz edge
#

I did:

char male[] = "male";
int result;

result = strcmp(male, gender);

printf("%d ---> %s", result, gender);

and the output was:

1 ---> male
1 ---> female
.
.
.

#

just to test it out, seeing as it wasn't working

mossy knot
#

Maybe gender is not null terminated. Try:

strncmp(gender, "male", 4)
topaz edge
#

the 4 is for the size of the char gender?

mossy knot
#

Yeah, so it compares the first 4 characters of each.

topaz edge
#

ok 4 without the null so, that would 6 for female and 4 for male

mossy knot
#

Yep, the issue of course if gender were to read "malevolent".

topaz edge
#

yh

#

that's the problem

#

cause gender will read

#

female

#

and male

#

but I can't even

#

add the 4

#

it says too many arguments to call

mossy knot
#

strncmp()
Note the little 'n' there

topaz edge
#

ah, mb

#

it has worked

#

that was it

mossy knot
#

Hang on.
Counter suggestion, if I may.

topaz edge
#

mhm

mossy knot
#

Try

char gender[10] = { 0 }; // or whatever size gender is in your code

Then go back to plain strcmp().
I think you may find this works.

topaz edge
#

nope

mossy knot
#

🤔

#

How does your code declare gender?

topaz edge
#

I have a file.txt

#

with lines

#

each line has "19,Paul Walker,Male" and so on

#

I just did fgets

#

and sscanf

mossy knot
#

;compile

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

int main()
{
  char* line = "10,fred,male,something";

  int age = 0;
  char gender[200] = { 0 };
  char name[200] = { 0 };

  if (sscanf(line, "%d,%199[^,],%199[^,]", &age, name, gender) == 3) {
    if (strcmp(gender, "male") == 0) {
      puts("male");
    }
    else {
      puts("not male");
    }
  }
}

Note the %9[^,]
Those are 1 less than the size of the placeholder strings. That prevents you from reading beyond the buffer AND ensures that these strings are always NULL terminated.

scarlet plazaBOT
#
Program Output
male
topaz edge
#

I did all of that

#

I didn't use the 9 tho

#

I just did %[^,]

mossy knot
#
char name[10] = { 0 };
// scan as %9[^,]

If the buffer was 20, then scan as %19[^,].
I.e. always one less than the buffer size.

topaz edge
#

well the thing is the name varies

#

and the size of the name varies

#

so I can't really do that

#

or I'll eat other people's names

mossy knot
#

How are name and gender declared in your code?

topaz edge
#

char name[200];
char gender[200];

#

to make sure it fits either way

#

cause no one will have a name that takes over 200 characters

#

I'm still in the basics of C, I've started uni like 2 months ago

#

So I'm not worried about memory yet

#

And optimizing

mossy knot
#

There you go.
So scan as %199[^,]

The 199 is a maximum.
See revised program I wrote above.

topaz edge
#

why did you do the if sscanf(......) == 3

#

?

#

to make sure it always gets at least 3 variables

mossy knot
#

Because you are extracting 3 params

topaz edge
#

ah ok

mossy knot
#

If you didn't get three, then that line is wrong, or empty.

topaz edge
#

mhm

#

It's working now

#

I just have to move on to the next problem lol

mossy knot
#

NP

topaz edge
#

👍

#

!solved