0

I am trying to write a program for some classwork that reads in a file using fscanf, and then stores each word into an array with one word in each element. Then I need to print each element of the array out on to a new line on the console.

The getty.txt file has the Gettysburg address in it with appropriate spacing, punctuation, and is multiline.

What I think is happening is that the entire text is being stored in the first element of the array, but I am not 100% sure as I am still learning to debug and write in C.

Any advice as to what I am doing wrong would be great! I currently only seem to be getting the last word and some extra characters.

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

void readFile();
void writeFile(char* buffer, int bufferlen);
FILE *fpread;
FILE *fpwrite;

char filebuffer[1000];
int filebufferlen = 0;


int main(int argc, char *argv[]) {
    fpwrite = fopen("csis.txt", "w");

    readFile();
    writeFile(filebuffer, filebufferlen);

    fclose(fpwrite);
    return 0;
}

void readFile() {
    char c;
    filebufferlen = 0;

    if(!(fpread = fopen("getty.txt", "r"))){
        printf("File %s could not be opened. \n", "getty.txt");
        fprintf(fpwrite,"File %s could not be opened. \n",     "getty.txt");
    exit(1);
}

    while (!feof(fpread)) {
        fscanf(fpread, "%s", filebuffer);
        filebufferlen++;
    }
}


void  writeFile(char* filebuffer, int filebufferlen) {
    for (int i = 0; i < filebufferlen; ++i){
            printf("%c\n", filebuffer[i]);
    }       
}
Lew
  • 35
  • 5
  • 4
    `fscanf(fpread, "%s", filebuffer);` You are storing into the same location, `filebuffer`, for every iteration. Also, see [Why is “while ( !feof (file) )” always wrong?](https://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong) – kaylum May 11 '16 at 02:03
  • 1
    Note: an array of *words* is actually a two dimensional array of *characters*. – user3386109 May 11 '16 at 02:10
  • regarding this prototype: `void readFile();`, because the parameter list is not specifically declared as `void`, the code produced allows any number (of any type) parameters, including no parameters. Suggest writing the prototype as: `void readFile( void );` – user3629249 May 11 '16 at 02:34
  • 1
    to put is bluntly: never use `feof()` to control a loop. It does not do what (most) think it does. Strongly suggest using: `while( 1 == fscanf( fscanf(fpread, "%s", filebuffer) )` And since using '%s' does not put any limit on the length of the input. Strongly suggest using a 'max length' modifier, that is 1 less than the length of the input buffer. I.E. `while( 1 == fscanf( fscanf(fpread, "%999s", filebuffer) )` – user3629249 May 11 '16 at 02:38
  • when calling: `fopen()`, always check (!=NULL) the returned value to assure the operation was successful. – user3629249 May 11 '16 at 02:41
  • in the `readFile()` function, before calling `exit()`, should cleanup by calling `fclose( fpwrite );` – user3629249 May 11 '16 at 02:48
  • when compiling, always enable all the warnings, then fix those warnings (for `gcc`, at a minimum use: `-Wall -Wextra -pedantic` I also use: `-Wconversion -std=gnu99` ) – user3629249 May 11 '16 at 02:50
  • 1
    the function: `main()` has two unused parameters; A much better signature for `main()` would be: `int main( void )` – user3629249 May 11 '16 at 02:52

1 Answers1

0

After fixing the compile problems:

the code does not contain any code nor data declarations to contain an array of 'words.' So naturally, nothing but the last word is actually saved so it can be printed out.

user3629249
  • 16,402
  • 1
  • 16
  • 17