读取C中字母或数字以外的字符 [英] Reading characters other than letters or digits in C
问题描述
下面一段代码从文件中读取,然后在屏幕上打印输出。
#define _CRT_SECURE_NO_WARNINGS
#include stdafx.h
int main()
{
char c;
int i;
for (i = 0 ;(c!= EOF); ++ i)
{
c = getchar();
printf( %c,c);
}
}
当我执行.exe文件并将输入重定向到ReadMe.txt并输出到通过在cmd中给出以下命令来执行tmp.txt:
file.exe< ReadMe.txt> tmp.txt
程序在文件的末尾打印一个有趣的字符,即ÿ。为了避免这种情况,我想提一下循环中有效字符的范围,这意味着如果此范围之外的任何字符遇到循环迭代,程序应该终止。我知道,
字母范围:: - 'a'到'z','A'到'Z'<前lang =text>(c< 'A')&& (c>'Z')&& (c<'a')&& (c>'z')
数字范围:: - '0'到'9'<前lang =text>(c<'0')& ;&安培; (c>'9')
现在我想要其他字符的范围,即〜!@#$%^& * () - + = _ [] {} \ |:;'<>,。?/
我想知道是否有人可以帮助我那个。
%^& *() - + = _ [] {} \ |:;'<>,。?/ < br $> b $ b
我想知道是否有人可以帮助我。
不是你想要的:'A'少于' Z'和小于'a'!
if (( (c> = ' A')&&(c< = ' Z'))||((c> = ' a')&&(c< = ' z')))
{
// 这是一个Al pha
}
if (( c> = ' 0')&& (c< = ' 9'))
{
// 这是一个数字
}
你可以很容易地检查其他可打印字符。
但是......
if ((c> = ' ')&&(c< ; = ' 〜'))
{
// 可打印。
}可能更容易。
Following piece of code reads from a file and then prints output on the screen.
#define _CRT_SECURE_NO_WARNINGS
#include "stdafx.h"
int main ()
{
char c;
int i;
for (i=0; (c!=EOF); ++i)
{
c=getchar();
printf("%c",c);
}
}
When I execute .exe file and redirect the input to ReadMe.txt and output to tmp.txt by giving following command in cmd:
file.exe < ReadMe.txt > tmp.txt
program prints a funny character at the end of the file i.e., ÿ. To avoid this I want to mention the range of valid character in the loop which means program should terminate if any character outside this range hits the loop iteration. I know,
Letters range::- 'a' to 'z' , 'A' to 'Z'
(c<'A') && (c>'Z') && (c<'a') && (c>'z')
Digits range::- '0' to '9'
(c<'0') && (c>'9')
Now I want the range of other characters i.e., ~!@#$%^&*()-+=_[]{}\|:;"'<>,.?/
I was wondering if someone can help me in that.
%^&*()-+=_[]{}\|:;"'<>,.?/
I was wondering if someone can help me in that.
Not quite what you want: 'A' is less that 'Z' and less than 'a'!
if ( ((c >= 'A') && (c <= 'Z')) || ((c >= 'a') && (c <= 'z'))) { // It's an Alpha }
if ((c >= '0') && (c <= '9')) { // It's an Digit }
And you can check for the other print able characters quite easily.
But...
if ((c >= ' ') && (c <= '~')) { // It's printable. }Might be easier.
这篇关于读取C中字母或数字以外的字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!