user-0 提问: C语言读取txt文件中单词

回复:
user: user-1
content: 引用 3 楼 Mathison_Turing 的回复:if((fp=fopen(" ","r"))==NULL)没有文件名啊 兄弟，文件名是你自己需要复制哪个文件就打上。

user: user-2
content: #include <stdio.h> #include <stdlib.h> #include <string.h> #include <ctype.h>  #define WORD_LEN    32  struct word_info {     char word[WORD_LEN];     int cnt;     struct word_info *next;     struct word_info *prev; };   FILE *open_file(const char *filename) {     FILE *fp = NULL;      fp = fopen(filename, "r");     if (!fp) {         fprintf(stderr, "fopen %s error!\n", filename);         return NULL;     }      return fp; }  static struct word_info *create_node(void) {     struct word_info *pnew;      pnew = (struct word_info *)malloc(sizeof(struct word_info));     if (!pnew) {         fprintf(stderr, "malloc error!\n");         return NULL;     }     pnew->cnt = 0;     pnew->next = NULL;     pnew->prev = NULL;      return pnew; }  int insert_nodes(struct word_info **phead, struct word_info *pnew) {     struct word_info *pcur;       pcur = *phead;      if (!(*phead)) {         *phead = pnew;         return 0;     }      /*head node*/     if (strcasecmp((*phead)->word, pnew->word) > 0) {         (*phead)->prev = pnew;         pnew->next = *phead;         *phead = pnew;         return 0;     }     /* from second nodes */     while (pcur->next != NULL) {         if (strcasecmp(pcur->word, pnew->word) == 0) {             pcur->cnt++;             free(pnew);             return 0;         }         if (strcasecmp(pcur->word, pnew->word) > 0) {             pcur->prev->next = pnew;             pnew->prev = pcur->prev;             pnew->next = pcur;             pcur->prev = pnew;             return 0;         }         pcur = pcur->next;     }      pnew->prev = pcur;     pcur->next = pnew;      return 0; }  int read_words(FILE *fp, struct word_info **phead) {     struct word_info *pnew;     int ch, i = 0, flag = 0;     char word[WORD_LEN];      pnew = create_node();     if (!pnew)         return -1;     while ((ch = fgetc(fp)) != EOF) {         if (isalpha(ch)) {             word[i++] = ch;             flag = 0;         } else if ((isblank(ch) || ch == ',' ||                    ch == '!' || ch == '.') && !flag) {             word[i] = 0;             strncpy(pnew->word, word, i+1);             pnew->cnt++;             insert_nodes(phead, pnew);             pnew = create_node();             i = 0;             flag = 1;         }     }     if (i == 0)         free(pnew);      return 0; }  void print_words(struct word_info *phead) {     struct word_info *pcur = phead;      while (pcur) {         printf("%12s: appears %3d times!\n", pcur->word, pcur->cnt);         pcur = pcur->next;     } }  void destroy_link(struct word_info *phead) {     struct word_info *pcur = phead, *ptmp;      while (pcur) {         ptmp = pcur->next;         free(pcur);         pcur = ptmp;     } }    int main(void) {     FILE *fp;     struct word_info *head = NULL;     int ret;      fp = open_file("1.txt");     if (!fp)         return -1;      ret = read_words(fp, &head);     if (ret < 0)         fprintf(stderr, "read words error!\n");      print_words(head);     destroy_link(head);     fclose(fp);     return 0; }   需要创建一个文件1.txt里面放一些英文单词即可。

user: user-3
content: if((fp=fopen(" ","r"))==NULL)没有文件名啊

user: user-1
content: #include<stdio.h> #include<stdlib.h>  int main() { 	FILE *fp; 	char word[10][10]; 	char name[10],ch; 	int i=0,j=0,k;  	printf("enter your file:"); 	scanf("%s",name); 	if((fp=fopen(" ","r"))==NULL) 	{ 		printf("cannot open %s\n",name);//fprintf(stderr,"cannot open %s\n",name); 		exit(1); 	}	 	while((ch=getc(fp))!=EOF&&i<10) 	{ 		word[i][j]=ch; 		j++; 		if(ch==' ') 		{ 			i++; 			word[i][j]='\0'; 			j=0; 		} 	} 	for(k=0;k<i;k++) 		printf("%s",word[i]); 	return 0; } 应该是对的吧，我自己写的，也是刚学还不熟练，多多指教

user: user-4
content: FILE fscanf fgets