因此,我编写了一个程序,该程序计数while,do / while和emty行。谁能帮我制作FILE * fin和FILE * fout局部变量。原来,我不能使用全局的,现在我被困住了。这是代码:

#include <stdio.h>
#include <string.h>
FILE *fin;
FILE *fout;
void stats();
void open_file1();
void open_file2();
int main()
{


char menu = 0;
printf("1.Read from file and write stats to another\n\n");
printf("2.Read from file and write stats to screen\n\n");
printf("3.Read from keyboard and write to file\n\n");
printf("4.Read from keyboard and write to display\n\n");
printf("5.Exit\n\n");
do
{

    printf("Choose an option from 1-5: ");
    fflush(stdin);
    scanf("%c", &menu);
    printf("\n");

    switch (menu)
    {
    case '1':
        open_file1();
        open_file2();
        stats(fin, fout);
        fclose(fin);
        fclose(fout);
        break;
    case '2':
        open_file1();
        stats(fin, stdout);
        fclose(fin);
        break;
    case '3':
        open_file2();
        printf("Enter text:\n");
        stats(stdin, fout);
        fclose(fout);
        break;
    case '4':
        printf("Enter text\n");
        stats(stdin, stdout);
        break;
    default:
        if (menu != '5')
            printf("Invalid.");
        break;
    }
} while (menu != '5');
return 0;
}
void stats(FILE *fin, FILE *fout)
{

char string[1000];
int count = 0, fcount = 0, wcount = 0, dcount = 0, empty = 0;


while (fgets(string, 10000, fin) != NULL)
{
    unsigned int i;
    for (i = 0; i < strlen(string); i++)
    {

        if ((string[i] == '"'))
        {
            while (string[i += 1] != '"')
                continue;
        }

        if ((string[i] == '/') && (string[i + 1] == '*'))
        {
            while (string[i += 1])
                continue;
        }

        if ((string[i] == '/') && (string[i + 1] == '/'))
        {
            while (string[i += 1] != '\n')
                continue;
        }


        if (string[i] == 'f' && string[i + 1] == 'o'&& string[i + 2] == 'r'&& string[i + 3] != '*') fcount++;
        if (string[i] == 'w' && string[i + 1] == 'h' && string[i + 2] == 'i' && string[i + 3] == 'l'&& string[i + 4] == 'e'&& string[i + 5] != '*') wcount++;
        if (string[i] == 'd' && string[i + 1] == 'o'&& string[i + 2] != '*')  dcount++;
    }

    for (i = 0; i < strlen(string); i++)
    {
        if (string[i] != ' ' && string[i] != '\n'&& string[i] != '\t')
        {
            count = 0;
            break;
        }
        count = 1;
    }
    if (count == 1)
        empty++;

}

fprintf(fout, " for %d ,while %d и do/while %d\n", fcount, wcount = wcount - dcount, dcount);
fprintf(fout, "The number of emtpy line is: %d\n", empty);

}


void open_file1()
{

char file1[100];
while (file1[strlen(file1) - 1] != 'c'&&file1[strlen(file1) - 2] != '.')
{
    printf("Please enter file to read from. Мust be C file: ");
    scanf("%s", file1);
}
fin = fopen(file1, "r");
if (fin == NULL)
{
    printf("There is no such file or directory\n");
    open_file1();
}
}

void open_file2()
{

char file2[100];
printf("Enter file to write in: ");
scanf("%s", file2);

fout = fopen(file2, "w");
if (fout == NULL)
{
    printf("There is no such file or directory\n");
    open_file2();
}
}

最佳答案

更改

void open_file1();
void open_file2();


打开文件并返回相应的FILE*

FILE* open_file1();
FILE* open_file2();


然后,将其实现更改为:

FILE* open_file1()
{
   FILE* fin = NULL;
   char file1[100];
   while (file1[strlen(file1) - 1] != 'c'&&file1[strlen(file1) - 2] != '.')
   {
      printf("Please enter file to read from. ?ust be C file: ");
      scanf("%s", file1);
   }
   fin = fopen(file1, "r");
   if (fin == NULL)
   {
      printf("There is no such file or directory\n");
   }
   return fin;
}


open_file2进行类似的更改。

然后,更改它们的使用方式。

代替

    open_file1();
    open_file2();


采用

    fin = open_file1();
    fout = open_file2();


确保:


fin的顶部声明foutmain
将它们传递给访问全局变量的函子。

09-30 16:52
查看更多