我需要在本地的许多函数中使用全局变量

时间:2015-01-07 15:58:43

标签: c

所以,我写了一个程序,其中包括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();
}
}

2 个答案:

答案 0 :(得分:3)

更改

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. fin的顶部声明foutmain
  2. 将它们传递给访问全局变量的functoins。

答案 1 :(得分:1)

查看下面的代码

FILE *open_file1();   

    int main()
        {
         FILE *fin=NULL;  
         FILE *fout=NULL;  

         fin = open_file1();
         // Rest of your code
         return 0;
        }

        FILE *open_file1()
        {
        FILE *fp=NULL;
        // open the file and 

        return fp;
        }