LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
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 09-25-2010, 04:55 AM   #1
austinium
Member
 
Registered: Nov 2007
Location: India
Distribution: Debian Stable, Debian Testing
Posts: 209

Rep: Reputation: 27
Question scanf reading newline into char array while reading 1 char at a time


The following code should read 4(and adds a '\0') into the character array str. As i debugged the code i figured that it reads newline into the str[index+1] after reading into str[index] after i hit Enter upon entering the desired character.

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

int main(void)
{
        char str[5];
        int cntr=0;

        memset(str,'\0',strlen(str));

        for(cntr=0;cntr<4;cntr++)
        {
                fflush(stdin);
                scanf("%c",&str[cntr]);
        }

        printf("\n%s\n",str);

        return 0;
}
Output

Code:
1
2
3

1
2
3�]
i've not been C much off late, don't know what i am missing here.

I know i can check each character and make sure newline characters don't make it into the array, but that would be an overkill.

help!

Last edited by austinium; 09-25-2010 at 04:56 AM.
 
Old 09-25-2010, 06:13 AM   #2
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 453Reputation: 453Reputation: 453Reputation: 453Reputation: 453
Quote:
Originally Posted by austinium View Post
The following code should read 4(and adds a '\0') into the character array str. As i debugged the code i figured that it reads newline into the str[index+1] after reading into str[index] after i hit Enter upon entering the desired character.

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

int main(void)
{
        char str[5];
        int cntr=0;

        memset(str,'\0',strlen(str));

        for(cntr=0;cntr<4;cntr++)
        {
                fflush(stdin);
                scanf("%c",&str[cntr]);
        }

        printf("\n%s\n",str);

        return 0;
}
Output

Code:
1
2
3

1
2
3�]
i've not been C much off late, don't know what i am missing here.

I know i can check each character and make sure newline characters don't make it into the array, but that would be an overkill.

help!
Reading '\n' is by construction, so what help are you asking for ?
 
Old 09-25-2010, 08:59 AM   #3
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian
Posts: 2,396

Rep: Reputation: 814Reputation: 814Reputation: 814Reputation: 814Reputation: 814Reputation: 814Reputation: 814
man scanf(3)
Quote:
* A sequence of white-space characters (space, tab, newline, etc; see isspace(3)). This directive matches any amount of white space, including none, in the input.
Code:
        char str[5];
        int cntr=0;

        memset(str,'\0',strlen(str));
At your call to strlen(), str's contents are undefined so strlen could return anything, you should use sizeof instead.
 
Old 09-26-2010, 12:12 AM   #4
austinium
Member
 
Registered: Nov 2007
Location: India
Distribution: Debian Stable, Debian Testing
Posts: 209

Original Poster
Rep: Reputation: 27
Quote:
Originally Posted by Sergei Steshenko View Post
Reading '\n' is by construction, so what help are you asking for ?
I am trying to read characters into an array using scanf, its not working like it should...i.e., when i hit enter i expect the index to be incremented so that the next character can be input, what is happening here is that '\n' is also being treated as character and is being stored into the array. may be this is how it is meant to be in the C Standard, and maybe this is not the right way to read characters into a an array?

Quote:
Originally Posted by ntubski View Post
man scanf(3)
At your call to strlen(), str's contents are undefined so strlen could return anything, you should use sizeof instead.
thanks

Last edited by austinium; 09-26-2010 at 12:14 AM.
 
Old 09-26-2010, 12:53 AM   #5
paulsm4
Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Hi -

1. There are several problems with your original code. First, here's sample output from the code you originally posted (it happens to "work", despite the two "strlen()" errors):
Code:
$ g++ -o x -Wall -pedantic test.c
$ ./test
9876543210

9876
2. The first problem is "memset(str,'\0',strlen(str));"
As you already saw, "str" isn't terminated, so strlen() is a bug.
One workaround is "memset (str, 0, sizeof(str));".
Perhaps a better solution might be to add the terminator after you've added all the characters.

3. The second problem is the "fflush()". You don't need it.

4. You're trying to read a character at a time. Fine.

But the OS gives you a LINE at a time.

In other words, you don't get ANY characters from the keyboard to your program until you hit <Enter>. Then you get them all at once.

This is the whole point behind "buffered input".

If you don't want this behavior (if you actually DO need to read a character at a time), then you need to look at a library like curses (which does text-mode screen control), or SDL (which lets you read individual events from a mouse, keyboard or joystick).

Here's a modified version of your code, and the corresponding output:
Code:
#include <stdio.h>
#include <string.h>

#define MAXCHAR 4

int
main  (int argc, char *argv[])
{
  char str[MAXCHAR+1];
  int i=0;

  for (i=0; i<MAXCHAR; i++)
  {
    scanf ("%c", &str[i]);
  }
  str[MAXCHAR] = '\0';

  printf ("\n%s\n", str);
  return 0;
}
Code:
$ g++ -o x -Wall -pedantic test.c
$ ./test
9876543210

9876
 
1 members found this post helpful.
Old 09-26-2010, 01:14 AM   #6
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 453Reputation: 453Reputation: 453Reputation: 453Reputation: 453
Quote:
Originally Posted by austinium View Post
.. its not working like it should ...
There is a manpage of 'scanf'. 'scanf' should work as the manpage (and related manpages) describe. If it doesn't work as the manpage(s) prescribe, you've found a bug in 'scanf', which a is a part standard "C" library ('glibc'), so file a bug against it.
 
Old 09-26-2010, 11:27 PM   #7
austinium
Member
 
Registered: Nov 2007
Location: India
Distribution: Debian Stable, Debian Testing
Posts: 209

Original Poster
Rep: Reputation: 27
thanks for your replies ppl...they've been very helpful

@Sergei ... notice that should was in italics, by that i meant that it wasn't working like how I thought it would...i'll refer to the man pages ASAP...gotta get reading

Last edited by austinium; 09-26-2010 at 11:31 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Programing Question: Reading file line by line.. then char by char in each line adriumroot Programming 8 05-30-2010 01:55 AM
C++ help Dynamic array and "invalid conversion from ‘char’ to ‘char*’" heathf Programming 2 04-25-2009 09:20 PM
read command in shell script what to do for reading a particular char Ashok_mittal Linux - Newbie 11 01-21-2008 05:01 AM
Need to Insert Newline Char amh06 AIX 6 09-15-2006 02:35 AM
reading a char string of variable size in C introuble Programming 3 05-08-2005 01:07 PM


All times are GMT -5. The time now is 04:12 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration