c - 如何扫描单词并存储从 C 中扫描的哪一行?

标签 c file

我正在尝试制作一个程序,从文件中读取单词并将每个单词及其出现的行存储在列表中,然后打印单词,其中的行按字母顺序出现,关于如何做到这一点的任何指导? 到目前为止,我已经放置了两个数组、单词和行来测试我的代码。但是我对如何通过获取每个单词及其出现的行来从文件中读取它感到困惑。

#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;
}

我也想过这个问题,但我不确定如何将它与我的代码合并以使其获取找到单词的行并更改 wordStruct 中的行..

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

最佳答案

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

让我们定义一个:所有字符直到并包括一个潜在的终止符'\n'。第一行是 line 1。最后一行可能以也可能不以 '\n' 结尾。

让我们定义一个:一个由非空白字符组成的字符串。出于实际和安全考虑,请限制其大小。

使用 fscanf(..., "%1023s", ...) 可以读取单词,但由于 "%s" 会消耗前导空格,任何 '\n' 都将丢失以计算行数。只需在 fscanf 之前,一次查找一个字符 '\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
}

示例用法

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;
}

关于c - 如何扫描单词并存储从 C 中扫描的哪一行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44604438/

相关文章:

c - 如何在每次 TCP 读取时只从 TCP 流中读取单个命令

java - 安卓 : calling Java class from C++ Native Activity

c++ - 无法从文件中读取

javascript - 当我克隆文件输入时,克隆的输入不会保留上传的文件。原始输入确实

python - Django 文件上传

c - 如何将 (void**) 传递给函数并有效地取消引用/使用任何类型?

C拼图: Make a fair coin from a biased coin

java - 如何从文件中定义给定大小的数组?

Java获取父对象的父对象

用sublime text 3编译C文件