This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef WORD_H | |
#define WORD_H | |
/********************************************************** | |
* Read_word: Reads the next word from the input and * | |
* stores it in word. Makes word empty if no * | |
* word could be read because of end-of-file. * | |
* Truncates the word if its length exceeds * | |
* len. * | |
**********************************************************/ | |
void read_word(char *word, int len); | |
#endif |
word.c
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <string.h> | |
#include "word.h" | |
int read_char(void) //抓取需要的字。 | |
{ | |
int ch = getchar(); | |
if (ch == '\n' || ch == '\t') | |
return ' '; | |
return ch; | |
} | |
void read_word(char *word, int len) //將文章的字存入word,沒字可讀時便存入\0。 | |
{ | |
int ch, pos = 0; | |
while ((ch = read_char()) == ' ') //略過第一個字前的空格。 | |
; | |
while (ch != ' ' && ch != EOF) { //讀入字串 | |
word[pos++] = ch; | |
ch = read_char(); | |
} | |
word[pos] = '\0'; | |
if (pos >= len) { //若單字長度超過最大長度,則將超過最大長度的字元輸出至另一個檔。 | |
FILE *out; | |
out = fopen("lost_words", "a+"); | |
pos = len - 1; | |
do { | |
fprintf(out, "%c", word[pos++]); | |
} while (word[pos] != '\0'); | |
fprintf(out, "\n"); | |
word[len] = '\0'; | |
} | |
} |
沒有留言:
張貼留言