Question

So I'm writing this program, and I do not know how to pass by reference or value. I just need to pass for values from one function (processFile) to another function (printReport). Any help on how to do this would be appreciated.

#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;
}
Was it helpful?

Solution

Try changing to this:

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);
    ...

So, in this change, the function processFile now takes four parameters by-pointer. When you access these parameters, use (for example) *lines to dereference the pointer.

In main(), when you call processFile(), you pass addresses to your parameters.

Hope this helps; I think you want to learn about parameter passing in C/C++ (pass-by-value, pass-by-pointer, and pass-by-reference - you might want to Google for those terms).

OTHER TIPS

If you want to read from FILE in your function you're doing it right already.

FILE *fileIn is pointing to some block of memory on the stack. Basically it's a number. When you pass that "number" to another function you're still pointing to the same thing, so you can edit what you're pointing at.

However, if you make fileIn point at something else within your function. For example: fileIn = null; you would have to pass fileIn as a double pointer. It would have to have 2 *'s on it like this: void foo(FILE **bar).

You could start by defining a data structure to hold all the statistics for a given sentence as

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

In main, you could create an object of this data structure as

struct stats stats_obj;

To retrieve the statistics from processFile, you could pass a reference to this object as

processFile(fileIn, &stats_obj);

In processFile, at the end of processing you could store back the results as

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

After processFile returns, you could pass the same to printReport as printReport(stats_obj) and print the same within this function as

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;
}

NOTE: It is always recommended to check the output of fopen and hence you will have to insert a NULL check for fileIn after fileIn = fopen("input09.txt", "r");

You can do like this:

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++;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top