Reputation: 137
It's like my code doesn't compare the two. I don't understand why. It's the first if condition that gives me trouble. How can I do to solve the problem?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char *argv[]){
FILE *in;
in = fopen(argv[1], "rw");
char s1[30], s2[30], s3[30];
if(strcmp(argv[2], "new") == 0){
while(fscanf(in, "%s %s %s", s1,s2,s3) == 3){
if(strcmp(s1, argv[3]) == 0 && strcmp(s2,argv[4])==0 && strcmp(s3, argv[5])==0){
printf("Errore! Cartolina già esistente.\n");
exit(0);
}
}
fprintf(in, "%s %s %s\n", argv[3], argv[4], argv[5]);
}
if(strcmp(argv[2], "find") == 0){
while(fgets(s1, 30, in) != NULL){
if(strstr(s1, argv[3]) != NULL){
printf("%s", s1);
}
}
}
return 0;
}
EDIT: I just realized that I used "r" instead of "rw" but it still doesn't print argv[3], argv[4] and argv[5] in the file.
Upvotes: 1
Views: 113
Reputation: 223972
You're not opening the file correctly:
in = fopen(argv[1], "rw");
rw
is not a valid mode. If you want to open for reading and writing, use r+
instead. Also, don't forget to check if fopen
failed.
in = fopen(argv[1], "r+");
if (!in) {
perror("fopen failed");
exit(1);
}
Upvotes: 1