AdmDTR
AdmDTR

Reputation: 45

how to scan words and store which line scanned from in C?

im trying to make a program that read words from a file and stores each word and the line it appears at, in a list and then prints the words with the lines appeared in alphabetically, any guidance on how to do that? so far i've put two arrays , words and lines to test my code..but im confused with how to make it read from a file with getting each word and the line it appears in..

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

#define LEN  7


/* Struct for word and lines that appears in */
struct wordStruct {
char *word;
char *lines;
struct wordStruct *next;
};

static int compare_words(const struct wordStruct *a, const struct wordStruct *b) {
 return strcmp(a->word, b->word);
}

static struct wordStruct *insert_sorted(struct wordStruct *headptr, char *word, char *lines) {
/* Struct head */
struct wordStruct **pp = &headptr;
/* Allocate heap space for a record */
struct wordStruct *ptr = malloc(sizeof(struct wordStruct));
if (ptr == NULL) {
    abort();
}

/* Assign to structure fields */
ptr->word = word;
ptr->lines = lines;
ptr->next = NULL;

/* Store words in alphabetic order */
while (*pp != NULL && compare_words(ptr, *pp) >= 0) {
    pp = &(*pp)->next;
}
ptr->next = *pp;
*pp = ptr;

return headptr;
}


int main(int argc, char **argv) {

char *Arr[LEN] = { "jack", "and", "jill", "went", "up", "the", "hill" };
char *Arr2[LEN] = { "22,1,5", "24,7,3", "50", "26,66", "18,23", "32,22", "24,8" };


int i;

/* Snitialize empty list */
struct wordStruct *headptr = NULL;
/* Snitialize current */
struct wordStruct *current;

/* Insert words in list */
for (i = 0; i < LEN; i++) {
    headptr = insert_sorted(headptr, Arr[i], Arr2[i]);
}

current = headptr;
while (current != NULL) {
    printf("%s appears in lines %s.\n", current->word, current->lines);
    current = current->next;
}
return 0;
}

i thoguht about this too, but im not sure how to merge it with my code to make it get the lines of where the word was found and make a change in Lines in wordStruct..

    void read_words (FILE *f) {
char x[1024];
/* assumes no word exceeds length of 1023 */
while (fscanf(f, " %1023s", x) == 1) {
    puts(x);
}
}

Upvotes: 2

Views: 157

Answers (1)

chux
chux

Reputation: 154175

im confused with how to make it read from a file with getting each word and the line it appears in..

Let us define a line: All the characters up to and including a potential terminating '\n'. The first line is line 1. The last line may or may not end with a '\n'.

Let us define a word: A string consisting of non-white-space characters. For practical and security concerns, limit its size.

Using fscanf(..., "%1023s", ...) work for reading words, but since "%s" consume leading white-spaces, any '\n' are lost for counting lines. Simply pre-fscanf, one character at a time looking for '\n'.

char *GetWord1024(FILE *ifile, char *dest, uintmax_t *linefeed_count) {
  // test for bad parameters
  assert(ifile && dest && linefeed_count);

  // consume leading white space and update count of leading line-feeds
  int ch;
  while (isspace(ch = fgetc(ifile))) {
    if (ch == '\n') {
      (*linefeed_count)++;
    }
  }
  ungetc(ch, ifile);  // put back non-whitespace character or EOF

  if (fscanf(ifile, "%1023s", dest) == 1) {
    return dest;
  }

  return NULL;  // No word
}

Sample usage

int main(void) {
  uintmax_t linefeed_count = 0;
  char word[1024];
  while (GetWord1024(stdin, word, &linefeed_count)) {
    printf("Line:%ju <%s>\n", linefeed_count + 1, word);
  }
  return 0;
}

Upvotes: 1

Related Questions