-2

I have the following code, and I can't seem to find why my IF clause is not working within the while cycle. otherwise, I am able to pull the records from the text file. It appears that when i compare the searched value against the fetched value from the text nothing happens. I am trying to print out only those records by condition contained within the file.

/* 
 * File:   main.c
 * Author: 
 *
 * Created on 06 February 2019, 10:27
 */

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

/*
 * 
 */
struct employee
{
    char Name[100];
    char Emp_ID[10];
    char Sex[10];
    char Department[50];
};

int main(int argc, char** argv) {
    FILE *fp;

    clearScreen();

    struct employee emp;

    fp=fopen("C:\\Users\\vdimitrov\\Documents\\NetBeansProjects\\KevinEngineering\\build\\Debug\\Cygwin-Windows\\employee.txt","r");
    if (!fp) {
        printf("Unable to open file!");
        return -1;
    }   

    printf("Display Employee Data Report");
    while (fread(&emp,sizeof(struct employee),1,fp) != NULL) {

        if(emp.Name == "Something contained in Name"){
            printf("\n");
            printf("\nName: %s\n", emp.Name);
            printf("Emp_ID: %s\n", emp.Emp_ID);
            printf("Sex: %s\n", emp.Sex);
            printf("Department: %s\n", emp.Department);
        }
    }
    printf("\nEnd Of Report\n");
    fclose(fp);
    return 0;

}

void clearScreen()
{
    system("@cls||clear");
}
  • 1
    Note that `NULL` is for null *pointers*. The value returned by [`fread`](https://en.cppreference.com/w/c/io/fread) is an integer saying how many records were read, or `EOF` on error or end of file. – Some programmer dude Feb 07 '19 at 07:42
  • As for your problem, you're not comparing strings but rather ***pointers***. Pointers that will never be equal. To compare strings you need to use [`strcmp`](https://en.cppreference.com/w/cpp/string/byte/strcmp). – Some programmer dude Feb 07 '19 at 07:43
  • Also note that you open the file in text mode, and then read from it like it was a raw binary file. If the file is a text-file you can't use `fread`. Learn about [`fgets`](https://en.cppreference.com/w/c/io/fgets) and [`sscanf`](https://en.cppreference.com/w/c/io/fscanf). – Some programmer dude Feb 07 '19 at 07:44

3 Answers3

1

First, the if condition won't work. You compare pointers, not strings. In C, it's strcmp

Second, if your input file is a text file, you have to read strings, not a buffer of a given size (the size of struct employee). Look at fgets.

rene-d
  • 343
  • 1
  • 6
1

In C, you do not compare strings with ==:

if (emp.Name == "Something contained in Name")

The strcmp function is what you need for equality checking. However, if (as seems to be the case), you want a partial match (like looking for any name containing Smith), you'll need some other functions defined in string.h.

I would suggest looking into strstr, for example, one that searches a string to see if it contains another string.

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
1

In C, you can't compare arrays (including strings, which are char arrays) with the == operator. (Or rather, you can, but it checks if they reside at the same place in memory, not whether their contents are the same.)

If you want to compare one array against another, the standard method is to use memcmp, which compares two raw blocks of memory. An alternative for strings is strcmp, which stops comparing when it finds a null byte. Both of these functions return 0 if the arrays are the same, so you would want:

if(strcmp(emp.Name, "my string here") == 0)

(Also, your file-reading code is probably wrong: you're reading raw bytes instead of text. But I don't know the actual requirements so I don't know how to fix that.)

Draconis
  • 3,209
  • 1
  • 19
  • 31