0

My Program Works Mostly, except for When I try to read the Total Data that was Entered

#include <stdio.h>
#include <string.h>
#define bufferSize 300
char name[50], gift[50], list[300], end[50], *result;
int i;
int main()
{
    FILE *appendPlace = fopen("NamesAndGifts.txt", "a");
    FILE *readData = fopen("NamesAndGifts.txt", "r"); //my place for reading data
    printf("This is a Gift Entering System for 3 People Only\nThe Name of the Person then their Gift Description will be taken\nThe Gift Description is Basically the Gift Name");
    while (strcmp(end, "END") != 0) {
        printf("\nEnter Name of Person %d or type 'END' to Exit Sequence\n", i + 1);
        scanf("%s", &end);
        if (strcmp(end, "END") != 0) {
            strcpy(name, end);
            printf("Now Enter Gift Description (Gift Name) of Person %d\n", i + 1);
            scanf("%s", &gift);
            strcat(list, "\n");
            strcat(list, "Name: ");
            strcat(list, name);
            strcat(list, "\n");
            strcat(list, "Gift: ");
            strcat(list, gift);
            strcat(list, "\n");
        }
        i++;
    }
    printf("The Gift Entering System (Names and their respective Gifts) is Below:\n");
    printf("%s", list);

    fputs(list, appendPlace);
    fclose(appendPlace);
    //below I attempt to read file Data to be able to print file's Data into running program
    fscanf(readData, "%s", result);
    printf("\n\n\n\n\n\n\nTotal Data in File Below:\n%s", result);
    fclose(readData);
}

I tried out doing just file reading, and it seems that reading from the file like that can only read data that is not separated by (space bar) or (enter) Is there a way to Solve this?

The Bomb Squad
  • 4,192
  • 1
  • 9
  • 17

1 Answers1

2

So there are 2 problems in your code.

  1. result has no memory allocated to it. Since it is a global variable, it is initialized to 0, aka a NULL pointer. So your scanf() sees that and the reading fails and so does printf() and prints "(null)". The solution there is to allocate memory in result either by making it a static array or by using malloc().

  2. Even if you fix the first problem, however, it will still not work as expected as fscanf() will stop reading input after the first whitespace is encountered. Since you want the whole (file) input to be read, you have four options:

    • Read character by character (not advisable for performance reasons but perhaps the easiest to implement)
    • read line by line (fairly a standard way)
    • read chunk by chunk given some pre-allocated buffer or
    • read the whole file at once (not advised for big files)

The functions to use are fgetc(), getline(), fread(). Additionally, you can find the size of the file by following this question

kyriakosSt
  • 1,754
  • 2
  • 15
  • 33
  • 1
    fseek(readData, 0, SEEK_SET); fread(result, 1001, 1, readData); printf("\n\n\n\n\n\n\nTotal Data in File Below:\n%s", result); fclose(readData); – The Bomb Squad Oct 11 '19 at 22:38
  • 1
    That is the data at the end of my code that solved the problem. Thanks thought guys. I was troubled with displaying the file's data for quite a while now – The Bomb Squad Oct 11 '19 at 22:40