Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/visual-studio/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 写入(文件)和打印统计数据_C - Fatal编程技术网

C 写入(文件)和打印统计数据

C 写入(文件)和打印统计数据,c,C,我的任务是创建一个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;

我的任务是创建一个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;
}
#包括
#包括
int main()
{
整数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,文件名[75];
文件*fp;
printf(“输入您希望看到的扩展名为.c或.txt的文件名\n”);
获取(文件名);
fp=fopen_s(文件名,“r”);//读取文件
如果(fp==NULL)
{
perror(“打开文件时出错。\n”);
_getche();
退出(退出失败);
}
printf(“文件%s的内容是:\n”,文件名);//打印文本
int i=0;
而((ch=fgetc(fp))!=EOF){
printf(“%c”,ch);
str[i]=ch;
i++;
}
int fsize=i;
//上面的代码打开文件的符号,下面的代码搜索特定符号
整数计数=0;
printf(“\n输入要搜索的字符:”);//要搜索的符号
扫描单位(“%c”和“chh”);
对于(i=0;i对于(k=0;k使用GCC,我可以通过改变一些方法来编译它

  • 将gets_s更改为gets(文件名),这会产生警告,表明这是一个不安全的函数
  • 将_getche()更改为getchar()
  • 将scanf_s()更改为scanf()
  • 将fopen_s()更改为fopen()
这段代码在Linux上使用GCC编译和运行

#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;
}
#包括
#包括
int main()
{
整数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,文件名[75];
文件*fp;
printf(“输入您希望看到的扩展名为.c或.txt的文件名\n”);
获取(文件名);
fp=fopen(文件名,“r”);//读取文件
如果(fp==NULL)
{
perror(“打开文件时出错。\n”);
getchar();
退出(退出失败);
}
printf(“文件%s的内容是:\n”,文件名);//打印文本
int i=0;
而((ch=fgetc(fp))!=EOF){
printf(“%c”,ch);
str[i]=ch;
i++;
}
int fsize=i;
//上面的代码打开文件的符号,下面的代码搜索特定符号
整数计数=0;
printf(“\n输入要搜索的字符:”);//要搜索的符号
scanf(“%c”、&chh);
对于(i=0;i对于(k=0;k请在下面找到我的发现

  • gets_s(file_name);->我认为这不正确。gets_s占用2个参数,而不是1个参数。请使用gets,然后试用它或简单扫描以检查它是否工作
  • 2._getche()请为该函数使用#include头文件。使用它可以避免此问题

  • fopen_s->fopen具有无效的参数集。您需要一个文件指针作为第一个参数。请重新设置所用函数的框架。最好选择具有2个参数的fopen

  • 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;
    
    #包括
    #包括
    #包括
    文件*fp;
    int main()
    {
    整数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,文件名[75];
    printf(“输入您希望看到的扩展名为.c或.txt的文件名\n”);
    获取(文件名);
    fp=fopen(文件名,“r”);//读取文件
    如果(fp==NULL)
    {
    perror(“打开文件时出错。\n”);
    getchar();
    退出(退出失败);
    }
    printf(“文件%s的内容是:\n”,文件名);//打印文本
    int i=0;
    而((ch=fgetc(fp))!=EOF){
    printf(“%c”,ch);
    str[i]=ch;
    i++;
    }
    int fsize=i;
    //上面的代码打开文件的符号,下面的代码搜索特定符号
    整数计数=0;
    printf(“\n输入要搜索的字符:”);//要搜索的符号
    扫描单位(“%c”和“chh”);
    对于(i=0;i对于(k=0;kSo,您到底有什么问题?当您说Visual Studio在抱怨时,这是否意味着您设法在另一个系统上编译了它?首先,所有这些“参数太少”应该通过使用正确的参数调用这些函数来修复错误。看起来您缺少了
    \include
    。您不能仅仅用fopen、get、scanf等函数的安全替换项替换fopen、get、scanf等函数,因为这些函数具有不同的函数签名。@gEshAdve:您的“修复我的代码”问题已被取消-这里的主题。你最好自己做作业。为了纠正错误,你最好阅读你正在调用的函数的文档(比如…),并花更多的时间阅读C编程语言。
    get
    被明确地否决了。而
    gcc-Wall-g
    将被否决