0

I want the program to read from an input file input.txt, identify each word, and save each word as a string in an array, named A.

I have tried following the other suggestions to no avail, my current code can print out each word in an array, but seems to save the word to every item in an array, instead of just one.

Any help would be much appreciated.

Sample input file:

Well this is a nice day.

Current output:

Well
this
is
a
nice
day.
This output should be the first word, not the last: day.

My code:

#include <stdio.h>

const char * readInputFile(char inputUrl[]) {
    char inputLine[200];
    char inputBuffer[200];
    const char * A[1000];
    int i = 0;

    FILE *file = fopen(inputUrl, "r");
    if(!file) {
        printf("Error: cannot open input file.");
        return NULL;
    }

    while(!feof(file)) {
        fscanf(file,"%[^ \n\t\r]",inputLine); // Get input text
        A[i] = inputLine;
        fscanf(file,"%[ \n\t\r]",inputBuffer); // Remove any white space
        printf("%s\n", A[i]);
        i++;
    }
    printf("This output should be the first word, not the last: %s\n", A[0]);
    fclose(file);

}

int main() {
    readInputFile("input.txt");


    return(0);
}
hardanger
  • 2,349
  • 1
  • 16
  • 23
  • 3
    Try changing `A[i] = inputLine;` to `A[i] = strdup(inputLine);`. Assigning `inputLine` to `A[i]` just duplicates the pointer, not the string. Also, `while(!feof(file))` is [usually wrong](http://stackoverflow.com/q/5431941/1679849). – r3mainer Feb 15 '17 at 20:10
  • 1
    Not that it particularly matters here, but a call to `free(A[i])` would be a good idea after finishing with these strings. – r3mainer Feb 15 '17 at 20:15

1 Answers1

0

What your are copying into each element of your array is actually the address of the string pointed to by inputLine. Each iteration of the loop changes the string pointed to and all your array elements point to the same string.

You need to create a copy of the string by allocating memory space to hold it (malloc) and then copying it letter by letter into the new location (strcpy) ...

[edit] You also probably want to remove the whitespace from the string before copying it ;) [/edit]

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

const char * readInputFile(char inputUrl[]) {
    char inputLine[200];
    char inputBuffer[200];
    char * A[1000];
    int i = 0;

    FILE *file = fopen(inputUrl, "r");
    if(!file) {
        printf("Error: cannot open input file.");
        return NULL;
    }

    while(!feof(file)) {
        fscanf(file,"%[^ \n\t\r]",inputLine); // Get input text
        A[i] = malloc(strlen(inputLine)+1);
        strcpy(A[i], inputLine);
        fscanf(file,"%[ \n\t\r]",inputBuffer); // Remove any white space
        printf("%s\n", A[i]);
        i++;
    }   
    printf("This output should be the first word, not the last: %s\n", A[0]    );
    fclose(file);

}

int main() {
    readInputFile("input.txt");

    return(0);
}
Chris M
  • 1
  • 1