-3
A
回答
0
可以使用fgets功能,這樣
char word[100][100];
int i = 0;
while(fgets(word[i++], 80, file))
;
0
試試這個
char *my_strchr(char * string_ptr, char find)
{
while (*string_ptr != find) {
/* Check for end */
if (*string_ptr == '\0')
return (NULL); /* not found */
++string_ptr;
}
return (string_ptr); /* Found */
}
int words_count(char * string_ptr, char find)
{
int i = 0;
while (*string_ptr != '\0') {
/* Check for end */
if (*string_ptr == find)
++i; /* not found */
++string_ptr;
}
return (i+1); /* Found */
}
int main()
{
char line[80]; /* The input line */
char *first_ptr; /* pointer to the first name */
char *last_ptr; /* pointer to the last name */
// char words[3][80];
char **words;
int cnt;
// fgets(line, sizeof(line), stdin);
static const char filename[] = "C:\\Diginity-Works\\LineReader\\debug\\data.txt";
FILE *file = fopen (filename, "r");
while (fgets (line, sizeof line, file) != NULL) // read a line
{
/* Get rid of trailing newline */
line[strlen(line)-1] = '\0';
cnt = words_count(line, ' ');
// for the "first level"
words = (char **) malloc(cnt * sizeof *words);
// In the second level
for(int i=0;i<cnt;++i)
words[i]=(char*)malloc(80);// i assume that max word length is 80
last_ptr = line; /* last name is at beginning of line */
for(int i=0; i < cnt; ++i){
first_ptr = my_strchr(line, ' '); /* Find space */
/* Check for an error */
if (first_ptr == NULL) {
strcpy(words[i], last_ptr);
break;
}
*first_ptr = '\0'; /* Zero out the slash */
++first_ptr; /* Move to first character of name */
strcpy(words[i], last_ptr);
strcpy(line, first_ptr);
}
for(int i=0; i < cnt; ++i)
printf("%d : %s \n", i, words[i]);
}
return (0);
}
相關問題
- 1. C#閱讀文本文件
- 2. 閱讀文本文件 - C++
- 3. C閱讀文本文件
- 4. 閱讀文本文件C#
- 5. 閱讀文本文件C
- 6. 閱讀文本文件C++
- 7. 閱讀並從文本文件C++
- 8. I/O C++從文本文件閱讀
- 9. 閱讀從文本文件
- 10. 閱讀ASCII文本文件,使用C++
- 11. 閱讀從文本文件的int到用C
- 12. C++閱讀從文件
- 13. 從文件(C++)閱讀
- 14. 從JSON文件C#閱讀
- 15. C#閱讀從逗號分隔文本文件到Windows窗體
- 16. 閱讀C++中的文本文件
- 17. 閱讀文本文件列明智c + +
- 18. 閱讀C++中的文本文件
- 19. C,閱讀多行文本文件
- 20. 閱讀文本分隔文件C
- 21. 閱讀文本使用C
- 22. 閱讀從文本文件到字典python的所有行
- 23. Powershell閱讀文本文件逐字
- 24. 閱讀文本文件法國字符
- 25. 閱讀縱橫字謎文本文件
- 26. 閱讀文本文件高達字符
- 27. 從文本文件簡單閱讀
- 28. JSONP閱讀文本從exernal文件
- 29. 閱讀文本文件
- 30. jquery - 閱讀文本文件?
你有什麼迄今所做? –
我試過很多不同的東西,但沒有任何工作? –
「直到換行」是什麼意思?換行在哪裏?在每個單詞後還是在結尾? – MetallicPriest