1

Im trying to compare 2 strings with strcmp(). One of them is implemented by keyboard, but the other is scanned from a file, so i use fgets(vec[i].nombre,80,fentrada); where "vec" is a vector from a typedef struct, with a char variable named "nombre". The thing is although I enter the exact same string which is taken from the file, it doesnt return a 0. I limited the maximum characters to the length of the string which is in the file, and it makes it work, so must have something to do with the blank spaces. But the point is making it work for whatever the length of the string is.

This is the structure:

typedef struct f{
    int dia;
    int mes;
    int anno;
    };
typedef struct pelicula{
    char nombre[80];
    float nota;
    f fecha;
}peliculas;
peliculas vec[dim];

Here's the sentence to scan what is in the file.

FILE *fentrada=fopen("peliculas.dat","rt");
while(!feof(fentrada)){
    fgets(vec[i].nombre,80,fentrada);
    fscanf(fentrada,"%i",&vec[i].fecha.dia);
    fscanf(fentrada,"%i",&vec[i].fecha.mes);
    fscanf(fentrada,"%i",&vec[i].fecha.anno);
    fscanf(fentrada,"%f\n",&vec[i].nota);
    i++;
    printf("\n");
}

And this is where i compare the strings:

int i=0,j=0;
char *nombre,cos[46],c[66];
strcpy(c,vec[i].nombre);
gets(cos);
printf("%s\n",c);
printf("%i",strcmp(cos,c));

And this is what the file looks like:

Casa Blanca
4 11 2013
6
Mi villano favorito
5 11 2013
7
Crepusculo
6 11 2013
8

P.D, ive tried using scanf(%s) instead of fgets but it scans just "Casa", then "Blanca", so i get 6 elements instead of 3...

Jose
  • 13
  • 3
  • 3
    Grab your debugger and see what the strings are. Do note that `fgets()` stores the newline also in the result. – Sami Kuhmonen Nov 21 '17 at 08:43
  • 1
    Lines read with `fgets` contain a `'\n'` at the end of the line, whereas lines read with `gets` don't. BTW `gets` has been deprecated long time ago, don't use it for real world programs; or better don't use it at all. – Jabberwocky Nov 21 '17 at 08:44
  • 1
    Note that [`while (!feof(file))` is always wrong](https://stackoverflow.com/questions/5431941/), and [`gets()` is too dangerous to be used — ever!](https://stackoverflow.com/questions/1694036/why-is-the-gets-function-dangerous-why-should-it-not-be-used). – Jonathan Leffler Nov 21 '17 at 08:53
  • 1
    Also, it is odd (in the extreme) to read a line with `fgets()` and then read 3 integers and a `float` using `fscanf()`. Remember, [`scanf()` leaves the newline in the input buffer](https://stackoverflow.com/questions/5240789/scanf-leaves-the-new-line-char-in-buffer), and [Trailing white space in a `scanf()` format string is a UI disaster](https://stackoverflow.com/questions/15740024/), though as long as the data really is coming from a file, it is OK. – Jonathan Leffler Nov 21 '17 at 08:56
  • Did my answer help you.? @Jose – Jay Joshi Nov 23 '17 at 06:45
  • @JayJoshi Sorry man i forgot to close the thread, minutes after posting i found the fix by myself! THX anyway! – Jose Nov 27 '17 at 23:08
  • @Jose, oh its ok. You can still select the answer if you feel it is. :) – Jay Joshi Nov 27 '17 at 23:10

1 Answers1

0

Possible Problems:

lines you read from file using fgets contains a new line character but gets doesn't. also using feof(file) is not advisable

Solution:

use fscanf instead of fgets. It returns a integer value which represents how many values are matched with the file input.

fscanf returns the number of input items successfully matched and assigned, which can be fewer than provided for, or even zero in the event of an early matching failure.

Code:

just change in the code for scanning the file lines

int ret;
while(ret = fscanf(fentrada,"%[0-9a-zA-Z ] %d %d %d %d ", vec[i].nombre, &vec[i].fecha.dia, &vec[i].fecha.mes, &vec[i].fecha.anno, &vec[i].nota))
{
    if(ret == EOF)
    {
        break;
    }else
    {
        // printf("\n %d",ret);
        // ^ prints how many values are successfully matched
    }
}

note: I have checked this code for your input. It works fine and returns 0 as the output of strcmp

reference: fscanf - TutorialsPoint

Community
  • 1
  • 1
Jay Joshi
  • 868
  • 8
  • 24