2016-05-29 2 views
1

Iファイルに空白、改行文字をカウントする次のソースコードを持っている:C:数えていないスペースや改行

#include <stdio.h> 
#include <sys/stat.h> 
#include <fcntl.h> 
#include <unistd.h> 
int main(){ 
    int fd; 
    int b=0, nl=0, c=0; 
    char ch[1]; 
    fd=open("text.txt", O_RDONLY); 

    while((read(fd, ch, 1))>0) 
    { 
     if(ch==" ") 
      b++; 
     if(ch=="\n") 
      nl++; 
     c++; 
    } 
    printf("no of blanks %d\n", b); 
    printf("no of new line %d\n", nl); 
    printf("no of characters %d\n", c); 
} 

結果はこのようなものです:

no of blanks 0 
no of new line 0 
no of characters 24 

私のtext.txtファイルの内容は次のとおりです。

hello world 
hello 
world 

文字の数が正しく(スペースと改行を含みます)しかし、なぜ変数bnlの結果が間違っていますか?
PS:私はCの新人ですが、C++で少し練習しています。

+0

'(、CH、FD(1を読んで))> 0 'あなたがしているように見えます情報を捨てる –

+1

'char ch [1];'は奇妙に見えます。おそらくちょうど 'char ch;' – pzaenger

+0

最小の修正は 'ch [0] == '''と 'ch [0] == '\ n''または' 'ch =='' 'と' * ch – alk

答えて

1
#include<stdio.h> 
#include<sys/stat.h> 
#include<fcntl.h> 
#include<unistd.h> 
#include<string.h> // or just include <string> it may vary depending on the compiler you use 
int main(){ 
int fd; 
int b=0, nl=0, c=0; 
char ch[1]; 
fd=open("text.txt", O_RDONLY); 

while((read(fd, ch, 1))>0) 
{ 
if(strcasecmp(ch, " ") == 0) //you need to use strcasecmp() instead of == for strings 
b++; 
if(ch[0] == '\n') //you can also check like this. 
nl++; 
c++; 
} 
printf("no of blanks %d\n", b); 
printf("no of new line %d\n", nl); 
printf("no of characters %d\n", c); 
} 
+0

をアカウントに取ったgccのコンパイル時に警告があります:警告:暗黙の関数 'strcmp' [-Wimplicit-function-declaration]の if(strcmp(ch、 ")== 0)。しかし、それは動作します – Stranger

+0

strcasecmp()を使用します。 –

2

あれば(CH ==」「)

(CH ==」「)場合

でなければなりませんが

と同じ他の比較のためには、"\n"は、'\n'

二重引用符文字列用です。文字には一重引用符を使用します。

はい、低レベルopenの代わりにfopenを使用してください。

int ch; 
FILE *fp=fopen("text.txt", "r"); 

while((ch = getc(fp)) != EOF) 
{ 
    if(ch==' ') 
     b++; 
    if(ch=='\n') 
     nl++; 
    c++; 
} 

これで問題が解決するはずです。

+0

@正しいものが使用されています。 'fopen'を使い、' ch'を配列から単一文字変数に変更する方が良いでしょう。私はそれを編集しました。 – artm

+0

私はこのタスクでfopenの原因を使用することはできません私はシステムコールを使用する必要があり、前のタスクではスペースと改行を数えるためにfopenをすでに使用しています。しかし、とにかくありがとう – Stranger

0

" "と改行文字"\n"を一重引用符で囲み、chcharと宣言してください。

0

私はあなたのコードをテストしていないが、一見私がミスを参照してください。

char ch[1]; 

あなたは1文字の配列を使用しています。文字のみを使用する必要があります。

char ch; 

なぜですか? あなたがテストしているときなので:

if(ch==' ') 
    b++; 
if(ch=='\n') 
    nl++; 

あなたは、配列の開始ADDRESを渡しています。配列を使用する場合はch [0]、charを使用する場合はchをテストする必要があります。 また、charと文字列を比較します。charsは単純引用符で囲まれています。二重引用符は文字列です。 文字列内に1つの文字がある場合でも、文字列と見なされます。つかいます ' '。

+0

私はstrcmpを使用している場合、私はコメント – Stranger

0

C言語では、2つの文字列を直接比較することはできません。 strcmp(char * str1、char * str2)またはstrncmp(char * str1、char * str2、ssize_t size)を使用する必要があります。

文字列を直接比較すると、0が返されます。そのため、空白と改行は増えません。

この修正をお試しください。

#include<stdio.h> 
#include<sys/stat.h> 
#include<fcntl.h> 
#include<unistd.h> 
int main() 
{ 
int fdesc; 
int blanks=0, newlines=0, characters=0; 
char buf[1]; 
fdesc=open("text.txt", O_RDONLY); 
while((read(fdesc,buf, 1))>0) 
{ 
    if(strcmp(buf," ")) 
    blanks++; 
    if(strcmp(buf,"\n")) 
    newlines++; 
    characters++; 
} 
printf("no of blanks %d\n", blanks); 
printf("no of new line %d\n", newlines); 
printf("no of characters %d\n", characters); 
} 
+0

は動作しませんでした – Stranger

0

すべてのウルのフィードバックのおかげで、私は、コードを修正するために管理し、最終的には以下のようになります。

#include<stdio.h> 
#include<sys/stat.h> 
#include<fcntl.h> 
#include<unistd.h> 
int main(){ 
int fd; 
int b=0, nl=0, c=0; 

char ch[1]; 
fd=open("text.txt", O_RDONLY); 

while((read(fd, ch, 1))>0) 
{ 
if(ch[0]==' ') 
b++; 
if(ch[0]=='\n') 
nl++; 
c++; 
} 
printf("no of blanks %d\n", b); 
printf("no of new line %d\n", nl); 
printf("no of characters %d\n", c); 
} 
関連する問題