Reputation: 117
I'm writing a C program in order to read the file and check if there is a given sentence inside of it. The function has to return "found" or "not found" respectively if the given sentence exists in the file or not. The sentences are divided by a / symbol.
Example of file:
1,2,3,4/
car, house, hotel/
2,age,12/
1,2/
1,2,3,5/
house, car/
Example of word to look for:
1,2/
My idea is to take each time a sentence from the file and put it in an array (called ary), check if the array (ary) is equal to the array (called sentence) that contains the given sentence that I'm looking for, and reuse that array (ary) for the next sentence in the file.
I've written this code:
#include <stdio.h>
void main()
{
char *sentence;
FILE *my_file;
char *ary;
int size = 500;
int got;
int ind=0;
int rest;
int found=0;
sentence="1,2";
my_file=fopen("File.txt", "r");
if(my_file==NULL)
{
printf("I couldn't open the file\n");
}
else
{
ary = (char*)malloc(500*sizeof(char));
while((got=fgetc(my_file))!=EOF)
{
if(got!='/')
{
ary[ind++]=(char)got;
}
else
{
ary[ind++]='\0';
rest = compare(sentence,ary);
if(rest==0)
{
found =1;
printf("found\n");
return;
}
ind=0;
free(ary);
ary = (char*)calloc(500, sizeof(char));
}
}
if(found==0)
{
printf("not found\n");
}
fclose(my_file);
}
}
int compare(char str1[], char str2[])
{
int i = 0;
int risp;
if(str1>str2 || str1<str2)
{
risp=-1;
}
if(str1==str2)
{
while(str1[i++]!='\0')
{
if(str1[i]!=str2[i]) risp=1;
}
}
return risp;
}
It compiles, but doesn't work properly and I don't find out why.
I've tried another solution, more simple, but doesn't work too.
void main()
{
char sentence[]="1,2";
FILE *my_file;
char string[2000];
int ind=0;
int rest;
int trovato = 0;
int got;
my_file=fopen("File.txt", "r");
if(my_file==NULL)
printf("I couldn't open the file\n");
else
{
string[0]='\0';
while((got=fgetc(my_file))!=EOF)
{
if(got!='/')
{
string[ind++]=(char)got;
}
else
{
string[ind++]='\0';
rest = compare(sentence, string);
if(rest==0)
{
found =1;
printf("found\n");
return;
}
ind=0;
//delete the array
int x=0;
while(string[x]!='\0')
{
string[x]='\0';
x++;
}
}
}
if(found==0) printf("not found\n");
fclose(my_file);
}
}
Can someone please point out my mistakes or let me know of a better solution? Thank you.
Upvotes: 1
Views: 1828
Reputation: 664
I'm not exactly sure why you think the compare function should work. If you want to compare two arrays, you don't compare their start addresses. Also, before you integrate a function into your program, test it with some other data first.
If you want to compare the array A[10] with B[10], write a function that takes char* A
, char* B
and its int size
as input, then compare each element from A with B through a loop that runs size
times.
Upvotes: 1
Reputation: 15642
Of course, compare
doesn't work because it's comparing pointers rather than strings (except in the case where the pointers are equal, which is the one case where you can guarantee that the strings are equal without testing each char). What meaning does an address have in determining the equality of the contents that are located at that address? None.
Do you really need to use a string
or an ary
array? Consider a compare
function that operates on a FILE *
and a char *
, rather than two char *
's.
Take in two arguments: FILE *file
and char *sentence
. Start off with a size_t offset = 0;
, as you would in any comparison function. Each time you get a character (from fgetc(file)
), compare it to sentence[offset]
. If it matches, increment offset
and repeat. When sentence[offset] == '\0'
, you've reached the end of sentence without any mismatches. Doesn't this mean you found a sentence? If it doesn't match, put the mismatched character back (using ungetc
) and return a value that corresponds to "mismatch".
Beginners tend to overcomplicate algorithms. -shrugs-
Upvotes: 0
Reputation: 98108
Regarding the first code, the compare function is wrong.
These checks do not make sense, you can use strcmp instead (or don't compare pointers):
if(str1>str2 || str1<str2)
{
risp=-1;
}
if(str1==str2)
Second, You are appending the newline after the /
to the new sentence so it never compares equal. Add this to the beginning of the while loop:
if (got == '\n') continue;
Upvotes: 2
Reputation: 21
Assuming this is for a homework problem, are the sentences guaranteed to be both on separate lines and terminated with '/'?
If this is the case, you should use the getline function, and then use strcmp.
Upvotes: 2