java在文本中读取单词_c-如何从文本文件中读取单词并将其添加到字符串数组中?...

本文介绍了一个C语言程序,该程序从文本文件中读取单词并存储到字符串数组中。通过示例代码,展示了如何利用fgets函数逐行读取文件,并将单词保存到数组中。程序还可以处理命令行参数,允许指定输入文件或默认使用标准输入。
摘要由CSDN通过智能技术生成

好吧,您可能使事情变得比他们需要的困难.该评论并不是要接受您的意见,但是您正在使用的任何编译器/ IDE都应该左右散布错误.它完全编译的事实令人惊讶.评论中的建议是正确的.始终,始终编译启用的警告并修复所有警告,然后再认为您的代码可信赖.

话虽这么说,但您有两个方面让您感到困难.您应该传递数组和FILE *指针(或文件名),而不是对函数中的文件名进行硬编码.这使得功能的使用非常有限.您现在所知道的,使用fgets是不正确的.此外,在传递数组后,只需确保将每个单词(假设每行1个)读入数组,同时确保不超过声明的行数即可.

这是一个简短的示例,将从命令行上给出的文件名(或默认情况下从stdin)读取.它利用三元运算符将文件名作为第一个参数,或将fp设置为stdin.试试看,如果您有任何疑问,请告诉我:

#include

#define MAXW 64 /* maximum number of lines to read */

#define MAXC 32 /* longest word in abridged Dict. is 28 char

"Antidisestablishmentarianism" */

size_t getwords (char (*words)[MAXC], FILE *fp);

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

char words [MAXW][MAXC] = {{0}}; /* array to hold words */

size_t nwords = 0; /* number of words read */

size_t i;

/* open argv[1] for reading (default: stdin) */

FILE *fp = argc > 1 ? fopen (argv[1], "r") : stdin;

if (!fp) { /* validate file open */

fprintf (stderr, "error: file open failed '%s'.

", argv[1]);

return 1;

}

nwords = getwords (words, fp);

if (fp != stdin) fclose (fp); /* close file */

printf ("

words read from '%s':

",

argc > 1 ? argv[1] : "stdin");

for (i = 0; i < nwords; i++)

printf (" words[%2zu] : %s", i, words[i]);

return 0;

}

/* function to read words, 1 - per line, from 'fp' */

size_t getwords (char (*words)[MAXC], FILE *fp)

{

size_t idx = 0; /* index of words read */

/* read each line in file into words array

note: includes trailing newline character */

while (idx < MAXW && fgets (words[idx], MAXC, fp)) {

idx++;

/* note you should check if chars remain in line */

}

if (idx == MAXW) /* check word count against MAXW */

fprintf (stderr, "warning: MAXW words read.

");

return idx;

}

gcc -Wall -Wextra -O3 -o bin/fgets_array_words_fn fgets_array_words_fn.c

输入文件

$cat dat/captnjack1.txt

This

is

a

tale

Of

Captain

Jack

Sparrow

A

Pirate

So

Brave

On

the

Seven

Seas.

产量

$./bin/fgets_array_words_fn dat/captnjack1.txt

words read from 'dat/captnjack1.txt':

words[ 0] : This

words[ 1] : is

words[ 2] : a

words[ 3] : tale

words[ 4] : Of

words[ 5] : Captain

words[ 6] : Jack

words[ 7] : Sparrow

words[ 8] : A

words[ 9] : Pirate

words[10] : So

words[11] : Brave

words[12] : On

words[13] : the

words[14] : Seven

words[15] : Seas.

或从stdin中读取:

$./bin/fgets_array_words_fn

words read from 'stdin':

words[ 0] : This

words[ 1] : is

...

要解决这个问题,可以使用Java的文件处理和字符串处理功能。 首先,需要通过Java的文件输入流读取文本文件input.txt的内容。可以使用BufferedReader类来实现该功能。然后,逐行读取文件内容,将每行内容存储在一个字符串变量。 接下来,需要将每行内容拆解为单词。可以使用Java字符串分割方法split(),通过指定空格作为分隔符,将每行内容拆分为一个单词数组。 然后,创建一个集合(例如ArrayList)来存储所有的单词。遍历每个单词,使用add()方法将其添加到集合。 最后,对集合进行排序。可以使用Collections类的sort()方法对集合进行排序,按照默认的字母顺序对单词进行排序。 最后,遍历排序后的集合,将单词逐个输出,得到按字典顺序排列的单词列表。 以下是一个简单的Java代码示例: import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; import java.util.ArrayList; import java.util.Collections; import java.util.List; public class SortWords { public static void main(String[] args) { String filePath = "input.txt"; List<String> words = new ArrayList<>(); try (BufferedReader reader = new BufferedReader(new FileReader(filePath))) { String line; while ((line = reader.readLine()) != null) { String[] splittedWords = line.split(" "); for (String word : splittedWords) { words.add(word); } } } catch (IOException e) { e.printStackTrace(); } Collections.sort(words); for (String word : words) { System.out.println(word); } } } 请注意,以上示例代码仅为演示目的,未做输入验证和异常处理等边界情况处理,请在实际使用根据需要进行完善。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值