char 'space' ?

Talk about making games.

char 'space' ?

Postby bat78 » Sat Nov 23, 2013 10:44 am

Hey,
Usually my posts are pretty self-confusing, so i'll only try to ask easier. I hope to get some luck.

The problem is in my tries to figure out how to set condition if a char is "space".
It works with any other characters like 'r' but with space, it doesn't.
I tried character escape commands like '/0'
Also tried with just ' '
But nothing recognize as a space character.
The future of "Game-Editor" here
User avatar
bat78
 
Posts: 816
Joined: Sun Dec 14, 2008 9:13 pm
Location: Bulgaria, Sofia
Score: 88 Give a positive score

Re: char 'space' ?

Postby DarkParadox » Sat Nov 23, 2013 6:30 pm

Just using single quotes with a space in the middle (' ') should work, as it works in my code. You can try comparing it to the number 32 instead like "if(mychar == 32)", since that's the ASCII code for the letter. If that doesn't work either, then there's probably something wrong with your code otherwise.
User avatar
DarkParadox
 
Posts: 457
Joined: Mon Jan 08, 2007 11:32 pm
Location: USA, Florida.
Score: 84 Give a positive score

Re: char 'space' ?

Postby bat78 » Sun Nov 24, 2013 2:05 pm

DarkParadox wrote:Just using single quotes with a space in the middle (' ') should work, as it works in my code. You can try comparing it to the number 32 instead like "if(mychar == 32)", since that's the ASCII code for the letter. If that doesn't work either, then there's probably something wrong with your code otherwise.


Hi,
I'm using strchr to compare chars. It works with the alphabetic ones.. like 'r' but if ' ' works i wouldn't make this topic.. i will try with ASCII number but strchr just works on that way..
The future of "Game-Editor" here
User avatar
bat78
 
Posts: 816
Joined: Sun Dec 14, 2008 9:13 pm
Location: Bulgaria, Sofia
Score: 88 Give a positive score

Re: char 'space' ?

Postby skydereign » Sun Nov 24, 2013 11:03 pm

bat78 wrote:I'm using strchr to compare chars.

strchr returns a pointer to a string. It doesn't actually return the character that it finds, but the pointer to it. Are you making sure to dereference the pointer? Though if the alphabetic characters work, then you probably are.
Code: Select all
char* substring = strchr(text, ' ');
if(substring[0] == ' ')
{
     // though really strchr returns null, so if all you want to know is if the space was found
     // you can can just use if(substring != NULL) instead
}
User avatar
skydereign
 
Posts: 3510
Joined: Mon Jul 28, 2008 8:29 am
Score: 589 Give a positive score

Re: char 'space' ?

Postby bat78 » Mon Nov 25, 2013 7:43 am

Well basically i tried many ways. And it all works, but not on the way i expected tho.
Code: Select all
    int checksum; checksum = 1;
void flinem(const char * fileName, char * id, int fmax)
{
    FILE * file = fopen(fileName, "r");
    char database[256];

    if (id[fmax] == 32 && checksum == 1) {database[0] = 1; checksum = 0;} else { checksum = 1; }
    if (id[fmax-1] == 32 && checksum == 1) {database[1] = 1; checksum = 0;} else { checksum = 1; }
    if (id[fmax-2] == 32 && checksum == 1) {database[2] = 1; checksum = 0;} else { checksum = 1; }
    if (id[fmax-3] == 32 && checksum == 1) {database[3] = 1; checksum = 0;} else { checksum = 1; }
    if (id[fmax-4] == 32 && checksum == 1) {database[4] = 1; checksum = 0;} else { checksum = 1; }
 
    if (strlen(id) > fmax) {
        if (database[0] == 1) id[fmax] = '\n';
        if (database[1] == 1) id[fmax-1] = '\n';
        if (database[2] == 1) id[fmax-2] = '\n';
        if (database[3] == 1) id[fmax-3] = '\n';
        if (database[4] == 1) id[fmax-4] = '\n';
}
}


The little code should find the last spaces behind the last 5 characters in a line of a text file. And if it finds the first, it does not look for another.
That should work but it still does not. I'm trying to figure out why the heck that is..
That does not work, because it sends sometimes only one character to a new line..
The future of "Game-Editor" here
User avatar
bat78
 
Posts: 816
Joined: Sun Dec 14, 2008 9:13 pm
Location: Bulgaria, Sofia
Score: 88 Give a positive score

Re: char 'space' ?

Postby skydereign » Mon Nov 25, 2013 8:59 am

bat78 wrote:The little code should find the last spaces behind the last 5 characters in a line of a text file.

So you know, that code doesn't actually load a line of text from the file. If id is supposed to be the line that is already read in, there is no need to pass in the fileName variable. Now from what you are doing, it sounds like you could just loop backwards from the last character in the line of text back 5 characters. During each iteration of the loop you check the character to see if it is a space and then escape the loop.
User avatar
skydereign
 
Posts: 3510
Joined: Mon Jul 28, 2008 8:29 am
Score: 589 Give a positive score

Re: char 'space' ?

Postby bat78 » Mon Nov 25, 2013 11:46 am

I'm not sure what you meant...but yeah
fName "text.txt"
id textArray[20]
fmax - the max characters allowed

the code should stop seek for spaces if he finds the first in those 5 chars. so i think you are right, and i could explain it ^^
The future of "Game-Editor" here
User avatar
bat78
 
Posts: 816
Joined: Sun Dec 14, 2008 9:13 pm
Location: Bulgaria, Sofia
Score: 88 Give a positive score

Re: char 'space' ?

Postby skydereign » Mon Nov 25, 2013 8:44 pm

Still not sure how you handle the fileName and FILE*, so I just had it read in the first line.
Code: Select all
void flinem(const char* fileName, char* id, int fmax)
{
  FILE* f = fopen(fileName, "r");
  int i;
  int limit;

  fgets(id, 129, f); // read first line into id

  limit = strlen(str)-6; // last 5 characters (last character is null)
  for(i = strlen(str); i>=limit; i--) // loop backwards for 5 characters
  {
    if(str[i] == 32) // if current character is a space
    {
      str[i] = '\n'; // turn it into a newline
      break; // and exit the loop
    }
  }
  // the id string now turned the last space in the end of the string into a newline
}
User avatar
skydereign
 
Posts: 3510
Joined: Mon Jul 28, 2008 8:29 am
Score: 589 Give a positive score

Re: char 'space' ?

Postby bat78 » Mon Nov 25, 2013 8:52 pm

Oh i'm using the gE default file reading code
The future of "Game-Editor" here
User avatar
bat78
 
Posts: 816
Joined: Sun Dec 14, 2008 9:13 pm
Location: Bulgaria, Sofia
Score: 88 Give a positive score

Re: char 'space' ?

Postby bat78 » Tue Nov 26, 2013 1:29 pm

Okay, it says, on line 10 there is beyond the allowed area
limit = strlen(id)-6; // last 5 characters (last character is null)

I thought the last char is new line - \n
It does not works again x;

[UPDATE]:
Okay i coded this.. will it works:
Code: Select all
void newLine (const char * fileName, char * lineArray, int maxChars)
{
   FILE * file = fopen(fileName, "wb");
   
   if (strlen(lineArray) > maxChars)
   {
   
   char base[7];
   char * patch; patch = strchr(base, 32);
   
   base[0] = lineArray[maxChars];
   base[1] = lineArray[maxChars-1];
   base[2] = lineArray[maxChars-2];
   base[3] = lineArray[maxChars-3];
   base[4] = lineArray[maxChars-4];
   base[5] = lineArray[maxChars-5];
   base[6] = lineArray[maxChars-6];
   
   lineArray[maxChars] = base[0];
   lineArray[maxChars-1] = base[1];
   lineArray[maxChars-2] = base[2];
   lineArray[maxChars-3] = base[3];
   lineArray[maxChars-4] = base[4];
   lineArray[maxChars-5] = base[5];
   lineArray[maxChars-6] = base[6];
   
   fseek(file, patch - base + 1 , SEEK_SET );
   fputs("\n", file);
   }
}
The future of "Game-Editor" here
User avatar
bat78
 
Posts: 816
Joined: Sun Dec 14, 2008 9:13 pm
Location: Bulgaria, Sofia
Score: 88 Give a positive score


Return to Game Development

Who is online

Users browsing this forum: Google [Bot] and 1 guest

cron