I don't actually have a lot of code to show here, but I can NOT seem to get a realistic answer on this: How can i take in multiple line input from a user?
for example i might want a user to say something like...
name: command
command
command
command
name: command
command
command
(the number of commands is NOT known. actually that would really have to do with the # of lines.) I just have no idea where to begin because there doesnt seem to be many resources on the matter)
Pseudo code:
do {
read a line and put it into String variable s
Push s into an array
} while (s is not empty)
Remove the last element of the array
As I haven't written C for months, this is what I can do now.
enum { MAX_LINES = 100 };
char *lines[MAX_LINES];
int nlines;
char buffer[4096];
while (fgets(buffer, sizeof(buffer), fp) != 0 && nlines < MAX_LINES)
{
if (buffer[0] == '\n')
break;
if ((lines[nlines++] = strdup(buffer)) == 0)
...memory allocation failed...
}
The lines of commands are in lines[0]
.. lines[nlines-1]
. If you don't like the hard-wired limit on the number of lines, dynamically allocate the lines
array of pointers (exercise for the reader).
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.