473,803 Members | 2,934 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

getchar() reading alphanumeric data

I'm trying to use getchar() to read alphanumeric data as follows:-

char input[150];

/* Take a string of input and remove all spaces therein */
int j = 0;
while ((input[j] = getchar()) != '\n')
{
if (!isspace(input[j]))
j++;
}

/* Append the null character */
input[j] = '\0';

I'm aware there's no bounds checking at present - it's forms part of
test code at present. The problem is, I get unexpected behaviour when
reading digits. With Linux and Solaris, if the data starts with a digit
the programme hangs. With Linux, if the stream begins with an alpha
character, it works as intended but on the Solaris box I get entirely
different characters.

I've looked in books and on Google but nothing is specifically mentioned
about this. I assume that getchar() is intended for alpha data only,
which presumably means that using scanf would be better with a separate
function to remove the whitespace?

Feb 11 '06 #1
1 3723
White Spirit <wh**********@n tlworld.com> writes:
I'm trying to use getchar() to read alphanumeric data as follows:-

char input[150];

/* Take a string of input and remove all spaces therein */
int j = 0;
while ((input[j] = getchar()) != '\n')
{
if (!isspace(input[j]))
j++;
}

/* Append the null character */
input[j] = '\0';

I'm aware there's no bounds checking at present - it's forms part of
test code at present. The problem is, I get unexpected behaviour when
reading digits. With Linux and Solaris, if the data starts with a
digit the programme hangs. With Linux, if the stream begins with an
alpha character, it works as intended but on the Solaris box I get
entirely different characters.

I've looked in books and on Google but nothing is specifically
mentioned about this. I assume that getchar() is intended for alpha
data only, which presumably means that using scanf would be better
with a separate function to remove the whitespace?


getchar() reads a single character from standard input. It doesn't
distinguish between alphanumeric characters and any other characters.

It's difficult to tell what's going wrong with your program because
you haven't shown us your program; you've only posted a code fragment.

getchar() returns either a character value (as an unsigned char
converted to int) *or* the special negative value EOF. You need to
store the result of getchar() in an int, not in a char. You also need
to check whether it returned EOF. In your code fragment, if you hit
end-of-file before seeing a '\n', you'll have an infinite loop.

Read section 12 of the C FAQ, <http://www.c-faq.com/>. If you're
still having problems, post a small, complete, compilable program that
illustrates whatever problems you're having. Be sure you have the
required #include directives for whatever functions you're using
(<stdio.h> for getchar(), <ctype.h> for isspace()).

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Feb 11 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
7627
by: Andy | last post by:
Hello All: I have a field in the database that is an Image. I have no idea how the data is stored in here (Image, compressed, encrypted, plain text, etc). I am trying to write the contents to a text file, image file, etc so I can see if the data is stored in a way we can understand (we have been tasked to write an app and the app needs to read this field, but we don't know what it really contains). How would I go about reading the...
3
1717
by: Adman | last post by:
Hi all. I've done some websearching, and haven't been able to find my question answered, so I thought I'd post. I apologize if this has already been answered. My question seems to be a variation of a FAQ. I'm writing a small online game (client/server). The server spits out a steam of data. The data can be described as a Code, followed by 0 or many Parameters.
2
7384
by: Ed | last post by:
Hope someone can help me out... I have been tasked to read some image data from an sql database and save the files to flat files. OK, sounds easy as I'v used BLOBs before. But this is an old database and I cannot get the image to work. The columns in the database are of type text. Here is one of the images text (in full) in the database (I hope you can see it):
3
3263
by: psbasha | last post by:
Hi , When ever we read any data from file ,we read as a single line string ,and we convert the respective field data available in that string based on the data type ( say int,float ). Please suggest me which is the best way of handling the file data. I- Method: ---------------- Store as single line string data's(upto end of file ) in a list and make use of this string list for the entire application.
3
2112
by: MF AHMED | last post by:
Hello, I am facing problem writing an algorithm/codes. From a C program, I generated 100 data files, now I have to read those files having three columns each. Using every data of each line of all the 100 files I have to determine the average for all the three variables separately. Problem (again): (1) Reading 100 data files (2) There are three variables in every file whose calculated values are written in double format. Utilizing every data...
1
1647
by: chris fellows | last post by:
I have a C# webservice with a web method that accepts a string parameter that contains encrypted data. For some parameter values with non-alphanumeric values then the web method fails with an HTTP status 400. Is there a way to prevent this error from happening? Obviously I can write a method to encode the encrypted parameter so that it only contains alphanumeric data but then I have to do the reverse in the web service.
0
2406
by: KJAYABHA | last post by:
Is It Possible To Convert Alphanumeric Data Into Binary Format And Use It As The Primary Key (defined As Char(5) In Db2
6
2531
by: Eddie Rivera | last post by:
How can I add an unbound text box, in an Access form, to hold alphanumeric data for a specific customer?
3
2184
by: ramahanumanth | last post by:
Hi, I have a table with column char(20). The table has alphanumeric data. When i select max(column) on that table, its giving wrong data. See below more details: Data: db2 "select * from EPSAR_WKLY_DET_UV" R33337A00A3E R33337883E70 select max(RETAIN_USER_ID) from EPSAR_WKLY_DET_UV" =
0
9566
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10555
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10317
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10300
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10069
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6844
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4277
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3802
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2974
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.