Reputation: 13
Using the language C and Netbeans 8.0.2 I'm trying to read a set of characters, of two files and pass those characters to a method called Patmat which is used to compare them.
The Patmat method has three arguments.The int argument is the number of mismatches that could have and other two character arguments are the file contents.
Below is the Patmat method which I got from an article and there is a long process inside it to compare.
int Patmat(int k, char *pattern, char *text){
}
Here is my main method
int main() {
FILE *fp1;
FILE *fp2;
char c;
char c2;
fp1= fopen ("file1.txt","r");
fp2= fopen("file2.txt","r");
while(1)
{
c = fgetc(fp1);
if(c==EOF)
break;
else
printf("%c", c);
}
while(1)
{
c2 = fgetc(fp2);
if(c2==EOF)
break;
else
printf("%c", c2);
}
fclose(fp1);
fclose(fp2);
**// Patmat(0,&c,&c2);
Patmat(0,c,c2);**
return 0;
}
As i'm not much familiar with C and pointers can anyone help me to pass those character sets of two files to the Patmat method? It does not work as above.
The content of two files are :
file1.txt: AGGTACCGTA
file2.txt: AGGTACCGTA
I directly passed those arguments as Patmat(0,"AGGTACCGTA","AGGTACCGTA")
. It worked fine and returned the answer as 1 which is correct.
But now I want to read those character sets from a file and pass them to the Patmat method. It gives the following warning:
warning: passing argument 3 of 'Patmat' makes pointer from integer without a cast
warning: passing argument 2 of 'Patmat' makes pointer from integer without a cast
Thanks in advance and sorry if I made any inconvenience. This is my first attempt to post a question here.
Upvotes: 1
Views: 1258
Reputation: 929
If you want to read the entire file a char
(one character) is not what you want. In that case you want to read the contents of the files into an array.
char *buffer[1024]; // for example
int result = fread(buffer, 1, 1024, fp1);
Now buffer contains your file contents which can be passed to your Patmat function
Patmat(0, buffer, buffer2); // buffer2 goes the same way
If you want to read the whole file in once (useful for smaller files) you can also allocate buffer
dynamically. Also make sure to set the null terminator.
Update: an example would be:
#include <stdio.h>
#include <stdlib.h>
#define BUFSZ 32
void patmat(int k, char *pattern, char *text) {
/* Compare or do something here */
printf("K %d\nPattern %s\nText %s\n", k, pattern, text);
}
int main(int argc, char *agrv[]) {
FILE *fp1 = NULL;
FILE *fp2 = NULL;
char buffer[BUFSZ];
char buffer2[BUFSZ];
if (!(fp1 = fopen("file1.txt","r"))) {
perror("open file");
return 1;
}
if (!(fp2 = fopen("file2.txt","r"))) {
perror("open file");
return 1;
}
size_t result = fread(buffer, 1, BUFSZ, fp1);
if (!result)
puts("reading error");
size_t result2 = fread(buffer2, 1, BUFSZ, fp2);
if (!result)
puts("reading error");
buffer[result-1] = '\0';
buffer2[result2-1] = '\0';
patmat(0, buffer, buffer2);
fclose(fp1);
fclose(fp2);
return 0;
}
This is just an example, there are other/better ways.
Upvotes: 2
Reputation: 3767
while(1)
{
c = fgetc(fp1);
if(c==EOF)
break;
else
printf("%c", c);
} // By the time this loop breaks c will be EOF
while(1)
{
c2 = fgetc(fp2);
if(c2==EOF)
break;
else
printf("%c", c2);
} // similar applies to this loop c2 EOF
fclose(fp1);
fclose(fp2);
**// Patmat(0,&c,&c2);
Patmat(0,c,c2);// This section is not probably well equipped to provide correct results, becuase of the EOF
I suspect what you are looking for is something like this
while( ((c = fgetc(fp1)) != EOF ) &&
((c2= fgetc(fp2)) != EOF ) ) {
printf("%c %c", c, c2);
Patmat(0,&c,&c2);
}
by the time you break this loop either both files are read completely or one of them still not at EOF, these cases required to be handled in here.
Upvotes: 0