Originally Posted by
Mungo
The thing is that I cannot use functions as strcmp or memcmp or strncmp, etc.
You should use strcmp or similiar when comparing the strings, but of course not when reading them. If you aren't permitted to do so, then you should write your own version of strcmp first (i.e., the reason why you aren't supposed to use strcmp is to give you practice in writing your own version). If you aren't permitted to do that either, then your teacher is either a poor teacher or a poor programmer, or both, but hopefully it doesn't come to that.
Originally Posted by
Mungo
And the names on the former list are separated by two or three spaces (my teacher told me that if I know how to code that wouldnt makes difference).
That's right, because you're just dealing with single "word" names, so you can use fscanf. But, you need to know the maximum length of a name so that you can properly specify the field width when using %s with fscanf.
Originally Posted by
Mungo
Here is my try. It doesnt working at all, so help please
You should control the input loop by checking the return value of fscanf, not by calling feof. Also, you don't need the "\n". For example:
Code:
while (fscanf(arch, "%s", n[i].names) == 1) {
i++;
}
You need to use descriptive variable names: n will not do. I would call the list of names in the second file something like reference_names, and store them in a struct with both an array member and an integer member for the number of names stored. Then I would read the first file and search reference_names for each name in the first file (i.e., your logic is reversed since you "need to check if these names [in the first file] are in a list of names that is into another file [reference_names]").
For this exercise, I doubt you need to use do while loops at all.
process should not return a char. I would declare it like this:
Code:
typedef struct {
char names[NAME_LIST_MAX_SIZE][NAME_MAX_SIZE];
size_t size;
} name_list;
int find_name(const struct name_list *reference_names, const char *name);
The idea is that find_name will return 1 (true) if name can be found in reference_names->names, otherwise it will return 0 (false). You need the size member variable because names might have fewer elements in use than NAME_LIST_MAX_SIZE.
In your main function, you will then call it like this:
Code:
while (fscanf(first_fp, "%s", name) == 1) {
if (find_name(&reference_names, name)) {
printf("%s\n", name);
}
}
Notice that I pass reference_names as an argument rather than have it as a global variable. I pass a pointer to it because it is potentially expensive to copy.
To populate reference_names, your use of fgets in a while loop could work, but note that fgets leaves the newline in the buffer. I think that it may be simpler to just use fscanf as with the first file, unless your teacher wants you to validate that each line does only contain one name.