Thread Tools Search this Thread
Top Forums Programming fgets()
# 1  
Old 09-03-2002

does anyone knows how to accept a command from a user.. i was wondering to use fgets(), but got no idea how to start it...
# 2  
Old 09-03-2002
If you are planning to read from the 'stdin' [Keyboard];
fgets(l_str, 255, stdin);

# 3  
Old 09-03-2002
If you're wanting to get input from a user (interactive script)...then you can use 'print and read'
see man pages for detail.
# 4  
Old 09-11-2002
why complicate the simple stuff...?
why don't u use :

scanf("%s",&x );

where x is declared as pointer to char (char* x)
I assume that the command is in string format, if it is a int, it will look like:


where x is a int

good luck!
by wolk!
# 5  
Old 09-11-2002
in defense of fgets

Originally posted by wolk
why complicate the simple stuff...?
why don't u use :

scanf("%s",&x );
I certainly agree with the sentiment "why complicate the simple stuff?". Still I have to say that fgets is, to me, a much simpler routine to understand than scanf. And even if it wasn't, scanf will do more work than fgets to acquire that string. Finally, the fgets solution will prevent buffer overflow. You will need to use something like:
scanf("%255s", &x);
to render this safe. And if the buffer is not a constant size you will need to dynamically build the format string that you pass to scanf.

scanf is great when you have a complex set of data, but to just read a single string, I would always go with fgets. Even for your second example, I might tend to go with fgets and atoi. But if it was, say, two integers and a string, I would use the scanf routine.
Login or Register to Ask a Question

Previous Thread | Next Thread

6 More Discussions You Might Find Interesting

1. Programming

fgets read file line with "\n" inside

Hi, I have a string like this, char str ="This, a sample string.\\nThis is the second line, \\n \\n, we will have one blank line"; if I want to use strtok() to seperate the string, which token should I use? I tried "\n", "\\n", either not working. peter (1 Reply)
Discussion started by: laopi
1 Replies

2. Programming

fgets problems newline

hello, i'm trying to write a C-program that reads a file line by line. (and searches each line for a given string) This file is an special ASCII-database-file, with a lot of entries. I checked the line with most length, and it was about 4000 characters. With google i found several... (4 Replies)
Discussion started by: p1cm1n
4 Replies

3. Programming

fgets problems

I've been having trouble with reading past the end-of-file in C. Can anyone find my stupid mistake? This is the minimal code needed to cause the error for me: FILE *f = fopen(name, "r"); if (!f) return; pari_sp ltop = avma; char line; while(fgets(line, 1100, f) != NULL) printf(".");... (23 Replies)
Discussion started by: CRGreathouse
23 Replies

4. Programming

[C] fgets problem with SIGINT singlal!!!

Hi all, I have this method to read a string from a STDIN: void readLine(char* inputBuffer){ fgets (inputBuffer, MAX_LINE, stdin); fflush(stdin); /* remove '\n' char from string */ if(strlen(inputBuffer) != 0) inputBuffer = '\0'; } All work fine but if i... (1 Reply)
Discussion started by: hurricane86
1 Replies

5. Programming

Question about NULL Character & fgets()

Assume client send the message " Hello ", i get output such as Sent mesg: hello Bytes Sent to Client: 6 bytes_received = recv(clientSockD, data, MAX_DATA, 0); if(bytes_received) { send(clientSockD, data, bytes_received, 0); data = '\0';... (2 Replies)
Discussion started by: f.ben.isaac
2 Replies

6. Programming

Problem with fgets and rewind function ..

Hello Friends, I got stuck with fgets () & rewind() function .. Please need help.. Actually I am doing a like, The function should read lines from a txt file until the function is called.. If the data from the txt file ends then it goes to the top and then again when the function is called... (1 Reply)
Discussion started by: user_prady
1 Replies
Login or Register to Ask a Question