Alex Rivera | Logout

How to use fgets if you don't know the number of characters to be read?

Asked 2010-05-21T04:21:08.200
10

I need to read a file and send the text from it to a string so I can parse it. However, the program won't know exactly how long the file is, so what would I do if I wanted to use fgets(), or is there a better alternative?

Note:

char *fgets(char *str, size_t num, FILE *stream);
Edit
Report

1 Answer

3

You can use fgets iteratively, but a simpler alternative is (stdio.h's) getline. It's in POSIX, but it's not standard C.

Since you're using C++ though, can you use std::string functions like iostream's getline?

answered 2010-05-21T04:25:07.663

Your Answer