2016-12-30 6 views
-4

私の仕事は、ユーザが何らかのテキストを書き込んだ後、そのテキストが(){} /の数とともに印刷される.cファイルを開くCプログラムを作成することです%比率コメント:Cプログラムの全文。 は、これまでのところ、私はしましたこの:ファイルの書き込みと統計の印刷

#include <stdio.h> 
#include <stdlib.h> 

int main() 
{ 
    int k, j, m, n, l, z, count1 = 0, count2 = 0, count3 = 0, count4 = 0, count5 = 0, count6 = 0; 
    char str[10000], chh, chhh; 
    char ch, file_name[75]; 
    FILE *fp; 

    printf("Enter the name of file you wish to see with extension .c or .txt\n"); 
    gets_s(file_name); 

    fp = fopen_s(file_name, "r"); // reads the file 

    if (fp == NULL) 
    { 
     perror("Error while opening the file.\n"); 
     _getche(); 
     exit(EXIT_FAILURE); 
    } 

    printf("The contents of %s file are :\n", file_name); //prints out the text 
    int i = 0; 
    while ((ch = fgetc(fp)) != EOF) { 
     printf("%c", ch); 
     str[i] = ch; 
     i++; 
    } 
    int fsize = i; 
    // code above opens up the symbols of the file, code below searches for specific symbols 
    int count = 0; 
    printf("\nEnter the character to be searched : "); //which symbol to search 
    scanf_s("%c", &chh); 
    for (i = 0; i < fsize; i++) { 
     if (str[i] == chh) 
      count++; 
    } 

    if (count == 0) 
     printf("\nCharacter '%c' is not present", chh); //if there isn't one 
    else 
     printf("\nOccurence of character '%c' : %d", chh, count); //prints their number if there is 

    for (k = 0; k<fsize; k++) { 
     if (str[k] == '>') 
      count1++; 
    } 
    for (j = 0; j<fsize; j++) { 
     if (str[j] == '<') 
      count2++; 
    } 
    for (m = 0; m<fsize - 1; m++) { 
     if (str[m] == '=' && str[m + 1] == '=') 
      count3++; 
    } 
    for (n = 0; n<fsize - 4; n++) { 
     if (str[n] == 'e' && str[n + 1] == 'l' && str[n + 2] == 's' && str[n + 3] == 'e') 
      count4++; 
    } 
    for (l = 0; l<fsize - 2; l++) { 
     if (str[l] == 'i' && str[l + 1] == 'f') 
      count5++; 
    } 

    int br; 
    br = count4 + count5; 
    printf("\nOccurence of character '%c' : %d", '>', count1); 
    printf("\nOccurence of character '%c' : %d", '<', count2); 
    printf("\nOccurence of character == : %d ", count3); 
    printf("\nOccurence of character else : %d ", count4); 
    printf("\nOccurence of character if: %d \n", count5); 
    printf("\nobsht broi if+else: %d ", br); 

    fclose(fp); 
    return 0; 
} 

それは、ファイル内のテキストをプリントアウトしたい特定の文字を検索し、その発生を出力します。

PS:私のPCで実行しようとすると、Visual Studioはエラーと警告の束を吐き出します。私はそれらを取り除く方法について困惑しています。 Errors image ありがとうございます!

+3

あなたの問題は何ですか? Visual Studioが不平を言っていると言えば、別のシステムでそれをコンパイルすることができますか? – RichouHunter

+3

まず、「あまりにも少ない引数」のエラーはすべて、正しい引数でそれらの関数を呼び出すことで修正する必要があります。 –

+0

あなたが '#include 'が見つからないようです。 –

答えて

1

GCCを使用して、私はいくつかの方法を変更することでこれをコンパイルできました。

  • gets_s gets gets(file_name)これは危険な機能であることを警告します。
  • は、このコードにコンパイルおよびLinux

    上でGCCを使用して実行しました)(fopenの)
  • 変更fopen_s()(scanfのために)(

をscanf_sを変更)(のgetcharする)(_getcheを変更しました

#include <stdio.h> 
#include <stdlib.h> 

int main() 
{ 
    int k, j, m, n, l, z, count1 = 0, count2 = 0, count3 = 0, count4 = 0, count5 = 0, count6 = 0; 
    char str[10000], chh, chhh; 
    char ch, file_name[75]; 
    FILE *fp; 

    printf("Enter the name of file you wish to see with extension .c or .txt\n"); 
    gets(file_name); 

    fp = fopen(file_name, "r"); // reads the file 

    if (fp == NULL) 
    { 
     perror("Error while opening the file.\n"); 
     getchar(); 
     exit(EXIT_FAILURE); 
    } 

    printf("The contents of %s file are :\n", file_name); //prints out the text 
    int i = 0; 
    while ((ch = fgetc(fp)) != EOF) { 
     printf("%c", ch); 
     str[i] = ch; 
     i++; 
    } 
    int fsize = i; 
    // code above opens up the symbols of the file, code below searches for specific symbols 
    int count = 0; 
    printf("\nEnter the character to be searched : "); //which symbol to search 
    scanf("%c", &chh); 
    for (i = 0; i < fsize; i++) { 
     if (str[i] == chh) 
      count++; 
    } 

    if (count == 0) 
     printf("\nCharacter '%c' is not present", chh); //if there isn't one 
    else 
     printf("\nOccurence of character '%c' : %d", chh, count); //prints their number if there is 

    for (k = 0; k<fsize; k++) { 
     if (str[k] == '>') 
      count1++; 
    } 
    for (j = 0; j<fsize; j++) { 
     if (str[j] == '<') 
      count2++; 
    } 
    for (m = 0; m<fsize - 1; m++) { 
     if (str[m] == '=' && str[m + 1] == '=') 
      count3++; 
    } 
    for (n = 0; n<fsize - 4; n++) { 
     if (str[n] == 'e' && str[n + 1] == 'l' && str[n + 2] == 's' && str[n + 3] == 'e') 
      count4++; 
    } 
    for (l = 0; l<fsize - 2; l++) { 
     if (str[l] == 'i' && str[l + 1] == 'f') 
      count5++; 
    } 

    int br; 
    br = count4 + count5; 
    printf("\nOccurence of character '%c' : %d", '>', count1); 
    printf("\nOccurence of character '%c' : %d", '<', count2); 
    printf("\nOccurence of character == : %d ", count3); 
    printf("\nOccurence of character else : %d ", count4); 
    printf("\nOccurence of character if: %d \n", count5); 
    printf("\nobsht broi if+else: %d \n", br); 

    fclose(fp); 
    return 0; 
} 
+0

'gets'は*明示的*推奨されなくなりました。そして 'gcc -Wall -g'はそれについて警告します。 –

+0

このコードはgccを使っても動作するかもしれませんが、あなたの答えはOPのコードでの実際の問題、すなわち間違った関数呼び出しを無視しているようです。 Microsoftのコンパイラは、 'scanf_s()'のような安全な関数を 'scanf()'に置き換えようとすると、気になることがあります。 –

0

以下の調査結果を確認してください。

  1. gets_s(file_name); - >私はこれが正しいとは思わない。 gets_s 2つの引数を受け取り、1ではなくgetsを使用して試してみるか、単純なscanfを使って動作しているかどうかを確認してください。

2._getche()その関数に#includeヘッダファイルを使用してください。それを使用するとこの問題は回避されます。

  1. fopen_s - > Fopenには無効な引数セットがあります。最初の引数としてファイルポインタが必要です。使用した機能を再度フレーム化してください。 2つの議論を持つfopenのほうが良い。

  2. scanf_s - >には、バッファサイズを指定できる引数(パラメータ)があります。したがって上記のコードsacnf_sは、構文的に間違って使用されています。それに応じて変更してください。

よく使用される変更されたコードを確認してください。私はgets_sをgetsに、fopen_sをfopenに、scanf_sをscanfに変更してビルドする自由を取ってきました。エラーはありません。あなたの参照のための下記のコードを見つけてください。時間の任意の時点で

#include <stdio.h> 
#include <stdlib.h> 
#include<conio.h> 
FILE *fp; 
int main() 
{ 
int k, j, m, n, l, z, count1 = 0, count2 = 0, count3 = 0, count4 = 0, count5 = 0, count6 = 0; 
char str[10000], chh, chhh; 
char ch, file_name[75]; 


printf("Enter the name of file you wish to see with extension .c or .txt\n"); 
gets(file_name); 

fp = fopen(file_name, "r"); // reads the file 

if (fp == NULL) 
{ 
    perror("Error while opening the file.\n"); 
    getchar(); 
    exit(EXIT_FAILURE); 
} 

printf("The contents of %s file are :\n", file_name); //prints out the text 
int i = 0; 
while ((ch = fgetc(fp)) != EOF) { 
    printf("%c", ch); 
    str[i] = ch; 
    i++; 
} 
int fsize = i; 
// code above opens up the symbols of the file, code below searches for specific symbols 
int count = 0; 
printf("\nEnter the character to be searched : "); //which symbol to search 
scanf_s("%c", &chh); 
for (i = 0; i < fsize; i++) { 
    if (str[i] == chh) 
     count++; 
} 

if (count == 0) 
    printf("\nCharacter '%c' is not present", chh); //if there isn't one 
else 
    printf("\nOccurence of character '%c' : %d", chh, count); //prints their number if there is 

for (k = 0; k<fsize; k++) { 
    if (str[k] == '>') 
     count1++; 
} 
for (j = 0; j<fsize; j++) { 
    if (str[j] == '<') 
     count2++; 
} 
for (m = 0; m<fsize - 1; m++) { 
    if (str[m] == '=' && str[m + 1] == '=') 
     count3++; 
} 
for (n = 0; n<fsize - 4; n++) { 
    if (str[n] == 'e' && str[n + 1] == 'l' && str[n + 2] == 's' && str[n + 3] == 'e') 
     count4++; 
} 
for (l = 0; l<fsize - 2; l++) { 
    if (str[l] == 'i' && str[l + 1] == 'f') 
     count5++; 
} 

int br; 
br = count4 + count5; 
printf("\nOccurence of character '%c' : %d", '>', count1); 
printf("\nOccurence of character '%c' : %d", '<', count2); 
printf("\nOccurence of character == : %d ", count3); 
printf("\nOccurence of character else : %d ", count4); 
printf("\nOccurence of character if: %d \n", count5); 
printf("\nobsht broi if+else: %d ", br); 

fclose(fp); 
return 0; 

}

uはどのような援助が必要な場合は私にpingを実行してください:)ありがとうございます。

関連する問題