954,499 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

fgets() and pointers

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

spark69
Newbie Poster
2 posts since Apr 2011
Reputation Points: 10
Solved Threads: 0
 
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.

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

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.

venomxxl
Junior Poster in Training
72 posts since Jan 2009
Reputation Points: 34
Solved Threads: 7
 
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?

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 
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.

venomxxl
Junior Poster in Training
72 posts since Jan 2009
Reputation Points: 34
Solved Threads: 7
 
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 correctingyour statement that stdin can't be used to read from a file.

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

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

venomxxl
Junior Poster in Training
72 posts since Jan 2009
Reputation Points: 34
Solved Threads: 7
 

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.

spark69
Newbie Poster
2 posts since Apr 2011
Reputation Points: 10
Solved Threads: 0
 
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);
}
Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: