Public Group

[C] read characters *one by one*

This topic is 4256 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

Hi(Z) again! Here's my code:
int main()
{
bool settings[4];
int sett=getsettingsFromfile();//this function will open
//"s.bin" and it will read the first 4 numbers.
//each character is used from the program for different
//settings.
//First line looks like this:
//1010
//character at position 1 : say or not hello
//character at position 2 : say or not die
//character at position 3 : say or not hey
//character at position 4 : say or not -_-/

if (set[0]==1)
{
settings[0]=1;
}

//blah blah
}


In this part of the code:
if (set[0]==1)


Im getting this error:
     D:\Dev-Cpp\settingsTEST\main.c subscripted value is neither array nor pointer


Any help ?

Share on other sites
I might be misunderstanding what you are trying to do, as it seems an odd way to read four bytes, but I think you need to cast to bool.

if (((bool*)&set)[0] == 1){ etc...

Is there a reason your getSettings function reads an int rather than just a bool at a time? This setup seems to be asking for headaches later.

Share on other sites
int sett=getsettingsFromfile();//this function will open if (set[0]==1)

Simple typo: either too many or too few t's.

Share on other sites
I posted wrong code...i've already fixed this.

Also the boolean array is INT.

So , thats not the problem !.

What im trying to do , is to access the 1,2,3,4 positions of the number.

For example:

int LOL=514;printf("%d",LOL[0]);//character 0 ...its "5" in our case

Share on other sites
Quote:
 Original post by 3DgonewildI posted wrong code...i've already fixed this.Also the boolean array is INT.So , thats not the problem !.What im trying to do , is to access the 1,2,3,4 positions of the number.For example:*** Source Snippet Removed ***

You could write the number to a string, and then use the string as an array:
char buffer[50];snprintf(buffer,50,"%d",num);printf("%c",buffer[0]);

However, a much better way would be to read the values in from a file one by one, after all you did say you were reading the values from a file.
int commands[100];char buffer[100];int num;num = fread(file,buffer,100);// check if ascii number, then convert to intfor( int i = 0 ; i < num ; ++i ){   if( buffer >= '0' && buffer <= '9' )   {       commands = (int)(buffer - '0');   }}// use commands[0], etc...if( commands[0] == 1 ){    printf("hello");}else{    printf("not hello");}

Then again, I'm not 100% sure what you are asking anyway.

Share on other sites
How many characters long a number is depends on what base it is being written in, the nth character of an int is pretty meaning less without a base. (eg var could be 50 in base 10, 32 in base 16, 62 in base 8)

The easiest way to get what you want is probably to convert it to a character string. The function itoa will do this for you.

Share on other sites
Quote:
 Original post by FlaxHow many characters long a number is depends on what base it is being written in, the nth character of an int is pretty meaning less without a base. (eg var could be 50 in base 10, 32 in base 16, 62 in base 8) The easiest way to get what you want is probably to convert it to a character string. The function itoa will do this for you.

Quote:
 Source: your linkPortabilityThis function is not defined in ANSI-C and is not part of C++, but is supported by some compilers.

Share on other sites
You seem to be trying to subscript a scalar? You can't actually do that, as integers aren't stored as strings. You could try casting it to a char *, and reading the value, but that would still vary depending on endianness, and most certainly wouldn't be 5...
In C, especially with the C99 standard, it is usually encouraged that you read straight into the destination buffer with fread, fgetc, etc. than casting back and forth, possibly causing alignment and aliasing issues.

This *seems* to be what you're trying to do (based on the similarities between the bool and char snippets), but won't give the results you expect it to (and is discouraged):
int Something = 512;printf("%c\n", *((char *)Something));

Again, converting to a string will give the results you expect, as would directly reading from the file.

EDIT: Beaten by rip-off while restoring my session after Firefox accidentally got terminated. Looks like my answer wasn't necessary.

Share on other sites
Quote:
Original post by rip-off
Quote:
 Original post by 3DgonewildI posted wrong code...i've already fixed this.Also the boolean array is INT.So , thats not the problem !.What im trying to do , is to access the 1,2,3,4 positions of the number.For example:*** Source Snippet Removed ***

You could write the number to a string, and then use the string as an array:
*** Source Snippet Removed ***

However, a much better way would be to read the values in from a file one by one, after all you did say you were reading the values from a file.
*** Source Snippet Removed ***

Then again, I'm not 100% sure what you are asking anyway.

THANKS!

Finally i can read the settings!!

Share on other sites
There's something else that i need to know.
The size of the string(how many characters it has)...!
Anyway to do it in C?
strlen( <string.h> ) will work in c?..

1. 1
2. 2
3. 3
4. 4
Rutin
15
5. 5

• 13
• 26
• 10
• 11
• 9
• Forum Statistics

• Total Topics
633722
• Total Posts
3013550
×