将输入从 stdin 读取到一个指针数组中,直到 EOF,但没有任何输出?
Reading input from stdin into an array of pointers until EOF, but nothing comes out as output?
我正在尝试编写一个程序,其中有一个称为 numwords
的整数,它指定从文件中读取的字数。但是,我正在针对一个字数少于用户输入的文件对其进行测试。例如,我有输入
this
should
not
work
其中 numwords
是 5,基于用户输入。我想以 1 的退出代码终止程序,所以我编写了以下代码来帮助我:
当我使用具有适当字数的文件作为用户输入到 numwords
时,似乎没有输出输出(该程序具有使用 wptrs
打印值的其他功能).在我将 while 语句添加到我的代码之前,正在打印输出。我感觉我的scanf语句在while循环中有问题。在我添加到 while 循环之前,我只使用了 for 循环和注释掉的 scanf("%s", unused)
,我的程序运行正常 - 输入被读入,并且使用了适当的输出。然而,我只是想实现一种条件,在这种情况下,上述情况的字数少于 numwords
会失败。
//A huge chunk of memory that stores the null-terminated words contiguously
char chunk[MEMSIZE];
//Location of unused memory
char *unused = chunk;
//Points to words that reside inside of chunk
char *wptrs[MAX_WORDS];
/** Total number of words in the dictionary */
int numwords;
void readwords()
{
int i = 0;
while ((scanf("%s", unused)) != EOF) {
for (i = 0; i < numwords; i++) {
//Read in words and store them in chunk array
//scanf("%s", unused);
wptrs[i] = unused;
unused += mystrlen(wptrs[i]) + 1;
}
}
//Check to see if fewer input than specified
if (numwords > i) {
printf("%d", i);
exit(EXIT_NUM_WORDS_BAD);
}
}
我希望这个案例以1的退出代码退出程序,但我发现它以0代码退出,因为main方法只有return 0
。有没有办法以代码 1 退出,并在有适当数量的单词相当于 numwords
时使我的程序正常工作?提前谢谢你。
修改后的示例:如果满足单词配额或读取 EOF,则跳出 while
循环。
我为 words_expected
任意选择了 5(而不是原始代码中的 numwords
)。读取五行输入后,将打印结果。不需要明确的 EOF。如果在 5 个单词之前遇到 EOF,则打印错误,我们以 return 代码 1.
退出
根据您的评论,我添加了检查给定行是否仅包含数字的功能。如果是,程序将停止处理输入。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define MEMSIZE 1024
#define MAX_WORDS 5
//A huge chunk of memory that stores the null-terminated words contiguously
char chunk[MEMSIZE];
//Location of unused memory
char *unused = chunk;
//Points to words that reside inside of chunk
char *wptrs[MAX_WORDS];
/** Total number of words in the dictionary */
int words_expected = 5;
int contains_only_digits(char *s)
{
int i = 0;
for (i = 0; i < strlen(s); i++) {
if (!isdigit(s[i])) {
return 0;
}
}
return 1;
}
void readwords()
{
int words_read = 0;
while (words_read < words_expected && scanf("%s", unused) != EOF) {
// Read in words and store them in chunk array
wptrs[words_read] = unused;
if (contains_only_digits(wptrs[words_read])) {
break;
}
unused += strlen(wptrs[words_read]) + 1;
words_read++;
}
//Check to see if fewer input than specified
if (words_read < words_expected) {
printf("Expected %d words, but %d were provided\n", words_expected,
words_read);
exit(1);
}
}
void printwords()
{
int i = 0;
for (i = 0; i < words_expected; i++) {
printf("word %d: %s\n", i + 1, wptrs[i]);
}
}
int main(int argc, char **argv)
{
readwords();
printwords();
}
contains_only_digits
函数是一个简单的实现。如果您对确定 C 字符串是否为数字的最佳实践感兴趣,使用 strtol
并检查 errno
可能是明智的。
我正在尝试编写一个程序,其中有一个称为 numwords
的整数,它指定从文件中读取的字数。但是,我正在针对一个字数少于用户输入的文件对其进行测试。例如,我有输入
this
should
not
work
其中 numwords
是 5,基于用户输入。我想以 1 的退出代码终止程序,所以我编写了以下代码来帮助我:
当我使用具有适当字数的文件作为用户输入到 numwords
时,似乎没有输出输出(该程序具有使用 wptrs
打印值的其他功能).在我将 while 语句添加到我的代码之前,正在打印输出。我感觉我的scanf语句在while循环中有问题。在我添加到 while 循环之前,我只使用了 for 循环和注释掉的 scanf("%s", unused)
,我的程序运行正常 - 输入被读入,并且使用了适当的输出。然而,我只是想实现一种条件,在这种情况下,上述情况的字数少于 numwords
会失败。
//A huge chunk of memory that stores the null-terminated words contiguously
char chunk[MEMSIZE];
//Location of unused memory
char *unused = chunk;
//Points to words that reside inside of chunk
char *wptrs[MAX_WORDS];
/** Total number of words in the dictionary */
int numwords;
void readwords()
{
int i = 0;
while ((scanf("%s", unused)) != EOF) {
for (i = 0; i < numwords; i++) {
//Read in words and store them in chunk array
//scanf("%s", unused);
wptrs[i] = unused;
unused += mystrlen(wptrs[i]) + 1;
}
}
//Check to see if fewer input than specified
if (numwords > i) {
printf("%d", i);
exit(EXIT_NUM_WORDS_BAD);
}
}
我希望这个案例以1的退出代码退出程序,但我发现它以0代码退出,因为main方法只有return 0
。有没有办法以代码 1 退出,并在有适当数量的单词相当于 numwords
时使我的程序正常工作?提前谢谢你。
修改后的示例:如果满足单词配额或读取 EOF,则跳出 while
循环。
我为 words_expected
任意选择了 5(而不是原始代码中的 numwords
)。读取五行输入后,将打印结果。不需要明确的 EOF。如果在 5 个单词之前遇到 EOF,则打印错误,我们以 return 代码 1.
根据您的评论,我添加了检查给定行是否仅包含数字的功能。如果是,程序将停止处理输入。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define MEMSIZE 1024
#define MAX_WORDS 5
//A huge chunk of memory that stores the null-terminated words contiguously
char chunk[MEMSIZE];
//Location of unused memory
char *unused = chunk;
//Points to words that reside inside of chunk
char *wptrs[MAX_WORDS];
/** Total number of words in the dictionary */
int words_expected = 5;
int contains_only_digits(char *s)
{
int i = 0;
for (i = 0; i < strlen(s); i++) {
if (!isdigit(s[i])) {
return 0;
}
}
return 1;
}
void readwords()
{
int words_read = 0;
while (words_read < words_expected && scanf("%s", unused) != EOF) {
// Read in words and store them in chunk array
wptrs[words_read] = unused;
if (contains_only_digits(wptrs[words_read])) {
break;
}
unused += strlen(wptrs[words_read]) + 1;
words_read++;
}
//Check to see if fewer input than specified
if (words_read < words_expected) {
printf("Expected %d words, but %d were provided\n", words_expected,
words_read);
exit(1);
}
}
void printwords()
{
int i = 0;
for (i = 0; i < words_expected; i++) {
printf("word %d: %s\n", i + 1, wptrs[i]);
}
}
int main(int argc, char **argv)
{
readwords();
printwords();
}
contains_only_digits
函数是一个简单的实现。如果您对确定 C 字符串是否为数字的最佳实践感兴趣,使用 strtol
并检查 errno
可能是明智的。