read()
ファイルを単語ごとに読み取るCプログラムを作成する必要があります(メソッドを使用する必要があります。Cライブラリやその他のメソッドの使用は許可されていません)。ファイルの単語と指定された単語を比較したいと思います。基本的には、特定の単語をファイルで検索しています。
私の問題は、ファイルから単語を取得するときです。「bla」と同じ文字列と比較すると、strcmp()
それらが同一であることはわかりません。
以下にコードを貼り付けました。
#include <stdlib.h>
#include <fcntl.h> //open,creat
#include <sys/types.h> //open
#include <sys/stat.h>
#include <errno.h> //perror, errno
#include <string.h>
int tananyag;
int fogalom;
int modositott;
char string_end = '\0';
int main(int argc,char** argv){
tananyag = open("tananyag.txt",O_RDONLY);
fogalom = open("fogalom.txt",O_RDONLY);
modositott =open("modositott.txt",O_WRONLY|O_CREAT|O_TRUNC,S_IRUSR|S_IWUSR);
if (tananyag < 0 || fogalom < 0 || modositott < 0){ perror("Error at opening the file\n");exit(1);}
char c;
int first = 1;
char * str;
str = (char*)malloc(80*sizeof(char));
while (read(tananyag,&c,sizeof(c))){
if(c != ' '){
if(first){
strcpy(str,&c);
first = 0;
}
else{
strcat(str,&c);
}
}
else
{
strcat(str,&string_end);
printf("%s string length: %i \n",str,strlen(str));
printf("%s string compared to bla string: %i \n",str, strcmp(str,"bla"));
str = (char*)malloc(80*sizeof(char));
first = 1;
}
}
close(tananyag);
close(fogalom);
close(modositott);
}