c - 我需要将许多函数中使用的全局变量设置为本地变量

标签 c

所以我写了一个程序,它计算、while、do/while 和空行。谁能帮我制作 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();

确保:

  1. main 顶部声明 finfout
  2. 将它们传递给访问全局变量的函数。

关于c - 我需要将许多函数中使用的全局变量设置为本地变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27823386/

相关文章:

c - 从c中的txt文件中获取词汇表

c - C中的char数组内的位交换

无法理解为什么我会因为 strncpy 而出现段错误

c - snprintf,用于 C 中整数到字符串的转换

c - 博耶摩尔算法的实现?

c - 在 linux 中静态链接库

c - 通过 Julia 的 C 接口(interface)为 `*x` 赋值

C编程指针和char数组问题

c - 还有其他类似这个的网站吗?

c - C 中未命名的零长度位域是什么意思?