Hello,
I have two very simple questions.

1. I want to use fgets(text, MAX_WORDS, stdin) to read in a string file, where my array is text[MAX_WORDS]. But is there a way to make stdin a pointer instead to a 2nd array which can be a buffer?

2.Whats the difference between fgets(text, MAX_WORDS, stdin) and fgets(text, sizeof(MAX_WORDS), stdin)?

Thanks

Recommended Answers

All 8 Replies

1. I want to use fgets(text, MAX_WORDS, stdin) to read in a string file, where my array is text[MAX_WORDS]. But is there a way to make stdin a pointer instead to a 2nd array which can be a buffer?

You mean some kind of sgets that reads from a string rather than a stream? Not directly, but you can get that behavior with sscanf:

if (sscanf(src, "%[^\n]", text) == 1) {
    /* Successful read */
}

2.Whats the difference between fgets(text, MAX_WORDS, stdin) and fgets(text, sizeof(MAX_WORDS), stdin)?

Assuming that you meant the latter to be fgets(text, sizeof(text), stdin), the difference could be significant if text is a pointer rather than an array. Otherwise, there's not really an practical difference beyond personal preference.

1. stdin is a file descriptor for console input, thus you can't use it to read from a file. You have to open a file with fopen(). For example:

//...
FILE* f = fopen("file.txt", "r");
fgets(text, MAX_WORDS, f);
//...
fclose(f);

2. MAX_WORDS is the value of an integer (or is it a preprocessor definition?), while sizeof(MAX_WORDS) is the amount of bytes that an integer uses up in the memory.

stdin is a file descriptor for console input, thus you can't use it to read from a file.

Sure you can, and it's a surprisingly powerful idiom. Ever heard of command line redirection?

Sure you can, and it's a surprisingly powerful idiom. Ever heard of command line redirection?

Where did he mention he is redirecting anything? Sorry, but I didn't translate "string file" to command < file.txt.
I think that a limited amount of information is the right amount of information.

Where did he mention he is redirecting anything? Sorry, but I didn't translate "string file" to command < file.txt.

What the OP said is irrelevant. I was correcting your statement that stdin can't be used to read from a file.

I was referring to his situation. Anyway, that doesn't matter. Off topic ends here.

Great. I will look at sscanf and go with my former in fgets() because otherwise I'll forget and write it the way I did. I saw and earlier post that mentioned something about a buffer overflow even with fgets() in the first manner and to use sizeof().

Venomxxl, MAX_WORDS is a #define so I can change the size of my array per the project demands.

I saw and earlier post that mentioned something about a buffer overflow even with fgets() in the first manner and to use sizeof().

That's a possibility because you need to match the macro in both the array declaration and the call to fgets, whereas sizeof grabs the declared size of the array directly. You would only need to worry about the size itself in the array declaration. As long as you're careful not to fall into the following trap, I advocate sizeof as well:

char *p = malloc(size);

/* sizeof p returns the size of a pointer, not the allocated "array" size */
fgets(p, sizeof p, in);

Note that this trap also rears its head with function parameters:

void foo(char a[])
{
    /* Same bug, a is actually a pointer, not an array */
    fgets(a, sizeof a, in);
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.