-4

所以我正在编写这个程序,我不知道如何通过引用或值传递。我只需要将值从一个函数(processFile)传递给另一个函数(printReport)。任何有关如何做到这一点的帮助将不胜感激。

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

void printInstructions();
int processFile(FILE *fileIn);
void printReport();

int main(void)
{
   FILE *fileIn;
   int words = 0,
        lines = 0,
        characters = 0,
        sentences = 0;

   printInstructions();

   fileIn = fopen("input09.txt", "r");

   if(fileIn == NULL)
      printf("\n\nERROR\n\n");

   processFile(fileIn);

   printReport(words, lines, characters, sentences);

}

void printInstructions()
{
   printf("\n  Program reads a file and returns the number of  \n");
   printf("lines, words, characters, and sentences in the file.\n\n");

   return;
}

int processFile(FILE *fileIn)
{
        int sentences = 0,
             words = 0,
             lines = 0,
             characters = 0,
             ch;

   while(ch != EOF)
   {
            ch = fgetc(fileIn);

            if(ch == '\n' || ch == 60)
                    lines++;

            if(ch == '.')
                    sentences++;

            if(ch != '.' || ch != ' ' || ch != '\n')
                    characters++;

            if(ch == ' ')
                    words++;
   }

   return 0;
}

void printReport()
{
   printf("This file contains NUMBER lines.\n");
   printf("This file contains NUMBER words.\n");
   printf("This file contains NUMBER    characters.\n");
   printf("This file contains NUMBER sentences.\n\n");

   return;
}
4

4 回答 4

0

如果你想在你的函数中从 FILE 中读取,你已经做对了。

FILE *fileIn 指向堆栈上的某个内存块。基本上它是一个数字。当您将该“数字”传递给另一个函数时,您仍然指向同一事物,因此您可以编辑您所指向的内容。

但是,如果您使 fileIn 指向函数中的其他内容。例如:fileIn = null;您必须将 fileIn 作为双指针传递。它必须像这样有 2 个 * void foo(FILE **bar):.

于 2013-04-11T02:14:10.887 回答
0

您可以首先定义一个数据结构来保存给定句子的所有统计信息

typedef struct stats {
    int words;
    int lines;
    int characters;
    int sentences;
}stats;

主要是,您可以创建此数据结构的对象

struct stats stats_obj;

要从中检索统计信息processFile,您可以将对此对象的引用传递为

processFile(fileIn, &stats_obj);

processFile中,在处理结束时,您可以将结果存储为

stats_obj->words = words;
stats_obj->lines = lines;
.........

返回后processFile,您可以将相同的内容传递给printReportasprintReport(stats_obj)并在此函数中打印相同的内容

void printReport(struct stats stats_obj)
{
    printf("This file contains %d lines.\n", stats_obj.lines);
    printf("This file contains %d words.\n" , stats_obj.words);
    printf("This file contains %d characters.\n", stats_obj.characters);
    printf("This file contains %d sentences.\n\n", stats_obj.sentences);

    return;
}

注意:始终建议检查输出,fopen因此您必须在之后插入NULL检查fileInfileIn = fopen("input09.txt", "r");

于 2013-04-11T02:15:24.313 回答
0

你可以这样做:

int main(void)
{
  ...
  int sentences = 0,
         words = 0,
         lines = 0,
         characters = 0,
         ch;

  processFile(fileIn, &words, &lines, &characters, &sentences);

  printReport(words, lines, characters, sentences);
}

int processFile(FILE *fileIn, int *pWords, int *pLines, 
                              int *pLines, int *pSentences)
{
       ...
       *pLines++;
       *pSentences++;
       *pCharacters++;
       *pWords++;
}
于 2013-04-11T02:17:59.123 回答
0

尝试更改为:

int processFile(FILE *fileIn, int *sentences, int *words, int *lines, int *characters)
{
    int ch;
    ...

        if(ch == '\n' || ch == 60)
                *lines++;
    ...

int main(void)
{
    ...
    processFile(fileIn, &sentences, &words, &lines, &characters);
    ...

因此,在此更改中,该函数processFile现在按指针接收四个参数。当您访问这些参数时,使用(例如)*lines取消引用指针。

main()中,当您调用 时processFile(),您将地址传递给您的参数。

希望这可以帮助; 我认为您想了解 C/C++ 中的参数传递(按值传递、按指针传递和按引用传递——您可能想在 Google 上搜索这些术语)。

于 2013-04-11T02:21:31.797 回答