2

I need to read a number of unsigned character from the input(ASCII value>127). SO i used-

unsigned char S[102];

but when i take input like this-

 gets(S);

compiler show error:

error: invalid conversion from 'unsigned char*' to 'char*'

So my question is how read unsigned character array using gets() or any other function exist.

1
  • 5
    The best way to do this is not to ever use gets for anything, ever, under any circumstances. If someone holds a gun to your head and says, "Use gets in your code," you should be able to accept your fate for the greater good of humanity. Commented May 28, 2011 at 7:36

2 Answers 2

3

First, you should never use gets. It cannot be used safely under any circumstances. My comment may have been a bit hyperbolic for humorous effect, but the point still stands.

The function you should use instead of gets is fgets. fgets takes a FILE * pointer (but you'll probably just want to pass stdin) and a maximum length, so it knows not to overflow your buffer. Unlike gets, fgets isn't guaranteed to read in an entire line - the line might be too big for your buffer to hold. You can, however, easily recover from this and write code to correctly process the input in a variety of ways (whereas with gets you just have to hope your buffer is long enough, and if it's not, who knows what will happen.)

I'm a little unclear on what kind of data you intend to read. If it's text, fgets is probably what you're looking for. It still takes a char *, so to make this work you'll need to cast S to a char * when you pass it to the function. Normally casts are a bad thing, especially casts from signed to unsigned types, but in this case nothing bad will (likely) happen.

If you're actually reading binary data into your unsigned array, you should look into fread. Unlike fgets it takes a void * to accommodate whatever kind of data you need to read, so no cast will be necessary. Note that fread reads chunks rather than lines, so if you're wanting textual input you should look elsewhere - it won't try to read an entire line, nor will it stop reading into a buffer when it sees a line ending.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for your answer.But can fgets() take character as input that has ascii value greater than 127 means where i need unsigned character,since fgets((char ,) (int ), (FILE *)),here i see it take char input, not (unsigned char*)????
@russell - If it's greater than 127, a signed char * (and probably char * on your platform) will interpret it as a negative number, but the same binary data will be written into your array, so when your array interprets that pattern as unsigned data, they'll be numbers greater than 127. Try this: int main(void) { unsigned short u = 65535, *up = u; /* signed */ short *sp = (short *)up, s = *sp; printf("Unsigned: %hu\nSigned: %hi\n", u, s); return 0; }
So,this means that i don't need to use unsigned char S[102]; using char S[102] is enough?. It can process still read,process & write character greater than 127??
No. signed char * will treat binary 11111111 as -1. unsigned char will treat that binary as 255. If you want the data to be interpreted as unsigned, use unsigned. Just know that if you have to pass it to, say, fgets as a signed type, the underlying binary data will work the way you expect it to.
1

Instead of gets you can use:

  • fgets (s, MAX_BUFFER, stdin);
  • scanf ("%[^\n]%*c", s);

Both will work when taking ASCII characters with values greater than 127 like : .±┼┤.␤▒⎽␤ .␍≤┼⎽≤└ .␍≤┼⎽├⎼ .±┼┤.┴␊⎼⎽␋⎺┼ .±┼┤.┴␊⎼⎽␋

gets will also do the thing. You do not need to pass your array as unsigned char *.

2 Comments

Thanks.The main question was whether or not gets() can take ASCII>127 valued character.Now i know it can.But when we compare 2 string(ASCII value--> 0-255) using strcmp() can it corrrectly sort these 2 string??
Your scanf example is just as bad, since it performs no bounds-checking. scanf can do bounds checking but it's so hard to correctly use scanf for this purpose, and so hard to recover from bad situations, that it's far better to stick to fgets for this purpose.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.