0

I want to be able to scan in a text file into my C program so I can search and store words containing capital letters. My issue is scanning in the file.

I've tried to create a string by using fseek to determine the length of the text file, and using char[] to create the array. Then I've tried using fgetc to scan in each character into the array, but that doesn't seem to work. The for loop at the end it to verify that the scanning has worked by printing it out.

#include <stdio.h>

int main() {

    FILE *inputFile;

    inputFile = fopen("testfile.txt", "r");

    //finds the end of the file
    fseek(inputFile, 0, SEEK_END);

    //stores the size of the file
    int size = ftell(inputFile);

    char documentStore [size];

    int i = 0;

    //stores the contents of the file on documentstore
    while(feof(inputFile))
    {
        documentStore[i] = fgetc(inputFile);
        i++;
    }

    //prints out char
    for (int j = 0; j < size; j++)
    {
        printf("%c", documentStore[j]);
    }

    return 0;
}

Currently I'm getting lots of random ascii characters and I'm not sure why. I'd expect for the for loop to print out the whole txt file.

  • @xing just tried that, and i still get a lot of ascii character :-( Am using fseek the wrong way? – nutellasandwich May 21 '19 at 12:39
  • 4
    `int i = 0; while(feof(inputFile)) { documentStore[i] = fgetc(inputFile);` -> `int i = 0, c; while((c = fgetc(inputFile)) != EOF) { documentStore[i] = c;` See [Why is “while (!feof(file))” always wrong?](https://stackoverflow.com/q/5431941/3049655). Also, `for (int j = 0; j < size; j++)` -> `for (int j = 0; j < i; j++)` as you should loop until where the last character was assigned instead of the whole array – Spikatrix May 21 '19 at 12:46
  • 3
    read this [why-is-while-feoffile-always-wrong](https://stackoverflow.com/questions/5431941/why-is-while-feoffile-always-wrong) – kiran Biradar May 21 '19 at 12:47
  • 1
    There must be some homewoek going on, there have been several questions lately about reading files, storing text and dealing with capital letters. – Jabberwocky May 21 '19 at 12:57
  • `char documentStore [size];` beware of large local arrays, use rather `malloc` for dynamic memory allocation. – Jabberwocky May 21 '19 at 12:59
  • 3
    Hint: you know the size of the file you want to read, so why are you reading the file char by char? You should read the whole file in one using `fread`. – Jabberwocky May 21 '19 at 13:00
  • 1
    And don't bother with the fseek to determine the file size. Start with a reasonably sized buffer (BUFSIZE, or 4096 or 8192). Read some data. If you fill the buffer, grow it and read more. – William Pursell May 21 '19 at 13:04
  • 3
    "Currently I'm getting lots of random ... characters" --> Verify `inputFile` is not `NULL`. – chux - Reinstate Monica May 21 '19 at 13:11
  • 1
    1) the file is left at the end when you plan to start reading, 2) the read loop condition is inverted, 3) regardless of #2, using `feof` to determine EOF while reading is wrong, instead check the return value of the function that reads data – Arkku May 21 '19 at 13:13
  • You're almost certainly never even calling fgetc! The while condition you're using is ... wrong. – William Pursell May 21 '19 at 13:47
  • @Spikatrix thanks! i've made the changes and it works now – nutellasandwich May 22 '19 at 04:04

1 Answers1

2

You need to make the following changes

  1. After int size = ftell(inputFile); add fseek(inputFile, 0, SEEK_SET); as suggested by xing

  2. Make documentStore a char pointer and allocate memory using malloc for size value

  3. while(feof(inputFile)) must be changed to while(!feof(inputFile))

Ravi S
  • 139
  • 3