0

I am trying pass a char array to a function. The array is initialized by the user's input. The input is a file name that will be opened and read in the function to determine how many of the integers in the file match the user's target integer (the file only contains integers). I am getting the user's input with fgets(). But when I pass the char array with the user's file name to the function, I get a NULL file pointer, even though the file I've tried to pass is in the same directory as my executable file.

Also, I am trying to get two more file name inputs from the user for another function. The first file is a source file, and the second file is the destination file where the contents of the first file will be copied to. I'm using fgets() for this as well, but my output that is asking the user for the second file name is being read by the first fgets() that is trying to read the first file name.

Here is my code:

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

int countOccurrences (const char* filename, const int target);
int copyFile(const char* src_file, const char* dest_file);

main()
{
    printf("**********countOccurrences Test**********\n");
    char buffer[15];
    int userTarget;

    printf("Enter input file name: ");
    fgets(buffer, 15, stdin);

    printf("Enter target: ");
    scanf("%d", &userTarget);

    int count = countOccurrences(buffer, userTarget);
    printf("Occurrences of %d in \"%s\": %d\n", userTarget, buffer, count);

    printf("\n**********copyFile Test**********\n");
    char source[15];
    char destination[15];

    printf("Enter source file name: ");
    fgets(source, 15, stdin);

    printf("\nEnter destination file name: ");
    fgets(destination, 15, stdin);

    copyFile(source, destination);
    return 0;
}

int countOccurrences (const char* filename, const int target)
{
    FILE * numFile;

    numFile = fopen(filename, "r");

    if(numFile == NULL)
    {
        printf("Error opening file.\n");
        exit(0);
    }

    int currentNum;
    int targetCount = 0;

    while(!feof(numFile))
    {
        fscanf(numFile, "%d", &currentNum);
        if(currentNum == target)
        {
            targetCount++;
        }
    }

    fclose(numFile);

    return targetCount;
}

int copyFile(const char* src_file, const char* dest_file)
{
    FILE *source, *destination;
    char c;

    source = fopen(src_file, "r");
    destination = fopen(dest_file, "w");

    if((source == NULL) || (destination == NULL))
    {
        printf("Error opening file.\n");
        return 0;
    }

    while(!feof(source))
    {
        c = fgetc(source);
        fputc(c, destination);
    }

    fclose(source);
    fclose(destination);
    return 0;
}
  • 2
    "`while (!feof(source))`" [Why is "while(!feof(file))" always wrong?](https://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong) tl;dr: use `int ch; while((ch = fgetc(source)) != EOF) /* do stuff */;` –  Oct 24 '18 at 21:40
  • 2
    "`while(!feof(numFile)) { fscanf(numFile, "%d", &currentNum);`" ==> `while(fscanf(numFile, "%d", &currentNum) == 1) /* do stuff*/ ;` –  Oct 24 '18 at 21:43
  • instead of const char use char * filename as parameter. – Stefano Raneri Oct 25 '18 at 12:08

0 Answers0