scanf("%s",str) won't do it. It will stop reading at the first space. gets(str) doesn't work either when the string is large. Any ideas?
相关问题
- Multiple sockets for clients to connect to
- how to split a list into a given number of sub-lis
- What is the best way to do a search in a large fil
- glDrawElements only draws half a quad
- Generate string from integer with arbitrary base i
To read string with space you can do as follows:
When do you want to stop reading? At EOF, at a specific character, or what?
You can read a specific number of characters with %c
You can read specific characters (or up to excluded ones) with %[
Create your own function to read a line. Here's what you basically have to do:
The implementation may be a bit tricky :-)
You need to think about what you need to pass to your function (at the very least the address of the array and its size); and what the function returns when everything "works" or when there is an error. You need to decide what is an error (is a string 10Gbytes long with no '\n' an error?). You need to decide on how to grow the array.
Edit
Actually it may be better to
fgetc
rather thanfgets
Try this
or this
use fgets with STDIN as the file stream. Then you can specify the amount of data you want to read and where to put it.