Page 1 of 1

Problem in my script ..lookig for help

PostPosted: Thu Nov 21, 2013 12:48 am
by bat78
Hi developers!
It's been awhile, since i did nothing..
So anyway i am constantly working on something that requires messing with text in files you know >.> those things are aways messy.
So what i am trying to accomplish is the following:
If the chars in a line are more then (for e.g 10) then the code finds the last char and converts it to a new line. What i have been doing is only this bit of code, with actually works:

Code: Select all
void NewLine(const char * fname, char * textArray, int maxChars)
{
    FILE * pFile = fopen(fname, "r");
    char * line = textArray;
    textArray = line;
    if (strlen(textArray) > maxChars) { line[maxChars-1] = '\n';}
    fclose(pFile);
}


And the result is:
result.png


according to:
Code: Select all
NewLine("test.txt", textArray[0], 6);


So it finds the 6th character with is "r" (from "8 chars(\n)") and it converts it to \n for the reading. But what i want is not that to happen. Maybe seeking for the last space should work.
Maybe i can seek for space with something like:
Code: Select all
int seekChar (const char * str, char chr)
{
  char * pointer;
  pointer=strchr(str, chr);
  while (pointer != NULL)
  {
  sprintf (text, "Order: %d", pointer-str+1);
  pointer=strchr(pointer+1, chr);
  }
  return pointer-str+1;
}



But it must must find the first space before the last allowed char mentioned. Maybe something like:
Code: Select all
void maxchr(const char * FILENAME, char * TEXTARRAY, int MAXCHARS)
{
   FILE * pFile = fopen(fname, "r");
   char * LINE = TEXTARRAY; TEXTARRAY = LINE;
   char * POINTER; POINTER = strchr(LINE, ' ');
   while )(POINTER != NULL)
   {
   if (strlen(TEXTARRAY) > MAXCHARS && MAXCHARS = pointr-str+1)
      {
      line[maxChars] = '\n';
      }
   }
   POINTER = strchr(POINTER+1, ' ');
}

Re: Problem in my script:

PostPosted: Thu Nov 21, 2013 1:35 am
by skydereign
bat78 wrote:But what i want is not that to happen. Maybe seeking for the last space should work.

What does that mean? It sounds like from the rest of the post, it does exactly what you want it to. What should the proper output look like?

Re: Problem in my script:

PostPosted: Thu Nov 21, 2013 1:51 am
by bat78
Okay we have this text document:
8 chars
9 chars
10 chars

The code should do for example:
If the first line have more then 7 characters, the next characters goes to the next line. So far i coded something (but i haven't tested it and i doubt it'll work):
Code: Select all
void maxchr(const char * FILENAME, char * TEXTARRAY, int MAXCHARS)
{
   FILE * pFile = fopen(FILENAME, "r");
   char * LINE = TEXTARRAY; TEXTARRAY = LINE;
   char * POINTER; POINTER = strchr(LINE, ' ');
   while (POINTER != NULL)
   {
   if (strlen(TEXTARRAY) > MAXCHARS && MAXCHARS = pointr-str+1)
      {
      line[MAXCHARS] = '\n';
      }
   }
   POINTER = strchr(POINTER+1, ' ');
}

nvm this above gives more errors then my life ^

Re: Problem in my script:

PostPosted: Thu Nov 21, 2013 2:40 am
by bat78
Generally this script:
Code: Select all
void maxCharLenght(const char * fileName, char * content, int maxChr)
{
   FILE * file = fopen(fileName, "r");
   char * line = content;
   char * pointer = strchr(content, ' ');
   content = line;
   
   //while (pointer != NULL)   {
   if (strlen(content) > maxChr) {
   if (pointer - content + 1 == maxChr) {
   line[maxChr-1] = '\n';
   pointer = strchr(pointer + 1, ' ');
   //}
   }
   }
   fclose(file);
}


Does send the rest chars in the next line if the char is specified BUT:
-It does not recognize spaces or escapes for null (NULL, '\0', ' ')
-It does not send the rest chars in the next line in the next textArray[num]
Shortly i need code that do the following:
Opens a .txt, check if there are more then x chars at the specified line and if they are, it sends the rest of the chars (after x) to the next line with is the next textArray[] And it shouldn't loose information such as another lines or char/chars like in my case the code is doing, by converting char to new line char

Re: Problem in my script ..lookig for help

PostPosted: Thu Nov 24, 2016 11:43 am
by next389
Understood nothing. clarify please!