LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-29-2007, 04:25 AM   #1
nesta
Member
 
Registered: Aug 2006
Posts: 100

Rep: Reputation: 15
question in c?


hi all,
i want to make a simple program that reads what i m typing on the keyboard then print it again for me.
here is what i have written:
Code:
#include <stdio.h> 
#include <string.h>

int main ()
{
	char buf[512];
	printf("small test\n");
	printf("if u want to terminate type end\n");
	do
	{
		scanf("%s",buf);
		printf("%s",buf);
	}while(strcmp(buf,"end"));

	
        return 0;
}
when i tried to execute the program by writting:
hello world

i got the following output:
helloworld

it does not sense the space,i want it to print
hello world
as i write exactly.

note that i dont want to use make character by character i.e i dont want to use scanf("%c",buf[i])

thanks in advance
 
Old 03-29-2007, 04:38 AM   #2
kshkid
Member
 
Registered: Dec 2005
Distribution: RHEL3, FC3
Posts: 383

Rep: Reputation: 30
use

fgets and set the stream to STDIN
 
Old 03-29-2007, 04:54 AM   #3
nesta
Member
 
Registered: Aug 2006
Posts: 100

Original Poster
Rep: Reputation: 15
thanks for your help but, now i cant terminate the program.
i want to terminate by writting end
but it does not terminate when i write end
here is my code:
Code:
#include <stdio.h> 
#include <string.h>

int main ()
{
	char buf[512];
	printf("small test\n");
	printf("if u want to terminate type end\n");
	do
	{
		fgets(buf,sizeof(buf),stdin);
		printf("%s\n",buf);

	}while(strcmp(buf,"end"));

        return 0;
}
 
Old 03-29-2007, 04:57 AM   #4
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
%s uses whitespace as it's break point, which is why the spaces are lost. Why don't you want to use %c?

Last edited by graemef; 03-29-2007 at 04:59 AM.
 
Old 03-29-2007, 05:05 AM   #5
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
Quote:
Originally Posted by nesta
thanks for your help but, now i cant terminate the program.
i want to terminate by writting end
but it does not terminate when i write end
Because unlike scanf() fgets() doesn't strip any white space (such as the new line)
 
Old 03-29-2007, 05:32 AM   #6
kshkid
Member
 
Registered: Dec 2005
Distribution: RHEL3, FC3
Posts: 383

Rep: Reputation: 30
both of them works,

Code:
#include <stdio.h>
#include <string.h>

int main ()
{
        char buf[512];
        printf("small test\n");
        printf("if u want to terminate type end\n");
        do
        {
                fgets(buf,sizeof(buf),stdin);
                buf[strlen(buf) - 1] = '\0';
                printf("%s\n",buf);
        }while(strcmp(buf,"end"));

        return 0;
}
or this

Code:
#include <stdio.h>
#include <string.h>

int main ()
{
        char buf[512];
        printf("small test\n");
        printf("if u want to terminate type end\n");
        do
        {
                fgets(buf,sizeof(buf),stdin);
                printf("%s",buf);
        }while(strcmp(buf,"end\n"));

        return 0;
}

man fgets
fgets() reads in at most one less than size characters from stream and stores them into the buffer pointed to by s. Reading stops after an EOF or a newline. If a newline is read, it is stored into the buffer.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Small question about /etc/rc.local - Symbolic link question Arodef Linux - General 4 05-13-2006 02:29 AM
Question, Apples Contribution to Open Source + MacOs file structure question Higgy3k Other *NIX 5 07-25-2005 04:23 AM
Not your regular GRUB question - just a short question for a fried MBR!! ziphem Linux - General 3 01-31-2005 01:51 PM
2 part question: Speeding up MDK9.1/GNOME question wardialer Linux - Newbie 6 10-14-2004 03:16 PM
login prompt question & kde scheme question JustinCoyan Slackware 2 06-09-2004 02:02 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:06 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration