?
Solved

printf

Posted on 2003-03-19
22
Medium Priority
?
687 Views
Last Modified: 2010-04-17
void split(char *pMessage)
{
  int slen, leng;

  char *pE, *pF;

  slen = strlen(pMessage) ;
  pE = pMessage;
  do
  {
            pF = pE;
        leng = slen > MAXLEN ? MAXLEN : slen ;

        pE = pF + leng;

        while (*pE != ' ')
        {
               if (*pE == 0)
         break ;
             pE--;
         leng-- ;
        }

          leng==pE-pF ;

      printf("%*.*s\n", leng, leng, pF) ;

      getch() ;

      transmit();
      slen -= leng;
  } while(slen>0);

}
In the above function,
regarding this command: printf("%*.*s\n", leng, leng, pF) ;
Ques: What this command do in this function? What *.* means?
0
Comment
Question by:bbtss
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 16
  • 3
  • 2
  • +1
22 Comments
 
LVL 30

Expert Comment

by:Mayank S
ID: 8165182
>>  leng==pE-pF

I guess you intend to do:

leng = pE - pF ;

Now, %*.*s will print out 'leng' number of characters from the starting of the pF.

e.g., if pF points to: "YourName" and leng = 5, then:

printf ( "%*.*s\n", leng, leng, pF ) ;

will print:

YourN

Sameway, writing:

printf ( "%*.*s\n", leng, leng - 1, pF ) ;

will print leng characters in total, leng - 1 characters from the string, and the remaining are filled with blanks. In fact, first the blanks are printed, and then the characters, like:

 Your

So, in:

printf ( "%*.*s\n", leng, leng, pF ) ;

The first leng refers to the total number of characters to be printed, the second leng refers to the number of characters to be printed from the string pointed to by pF (and in case this number is smaller, then first, blanks will be printed).

I hope you got it.

Mayank.
0
 

Author Comment

by:bbtss
ID: 8167352
thanks for ur reply.
but is there any way that i can assign a pointer to this segmented string other than directly printf it out?
like the message is Yourname
this function will printf Yourn if leng is 5.
how can i assign a pointer to this segmented message?
0
 

Expert Comment

by:lowmaka
ID: 8168892
It looks like you are trying to "extract" from a space-delimited string and proccess(transmit) them one "element" at a time. The element string should be from pC+1 to pE in the following code:

==========================================
 char *pE, *pF, *pC;

 slen = strlen(pMessage) ;
 slen = slen > MAXLEN ? MAXLEN : slen ;

 pF = pMessage;
 pE = pF + slen - 1;
 pC = pE;

 do
 {
       while (*pC != ' ')
       {
        if (*pC == 0) break ;
        pC--;
       }

       leng = pE - pC;

     printf("%*.*s\n", leng, leng, pC+1);

     getch() ;

     transmit();

     --pC;
     pE = pC;
     slen -= (leng+1);

 } while(slen>0);
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:bbtss
ID: 8168982
#include <stdio.h>
#include <string.h>
#include <conio.h>
#define MAXLEN 50
char *pMsgString = {"HDLC defines three different data .................................."};
void display(char *pMss)
{
 printf("%s",pMss);
 getch();
}
void split(char *pMessage)
{
  int slen, leng;

  char *pE, *pF;

  slen = strlen(pMessage) ;
  pE = pMessage;
  do
  {
            pF = pE;
        leng = slen > MAXLEN ? MAXLEN : slen ;

        pE = pF + leng;

        while (*pE != ' ')
        {
               if (*pE == 0)
         break ;
             pE--;
         leng-- ;
        }

          leng=pE-pF ;

      printf("%*.*s\n", leng, leng, pF) ;
      getch();
      display(??);
      slen -= leng;
  } while(slen>0);

}
void main(void)
{
split (pMsgString);
}

hm... i dun quite get what u mean... actually what i want to know is how i store the segmented message into a pointer so that when i call the function it would only display the segmented part.In simple terms, how can i declare a pointer and place it into the ?? part so that the display function will only display the segmented message?
Hopefully u ppl can understand my english becos i a not quite good in it.
0
 

Author Comment

by:bbtss
ID: 8169003
#include <stdio.h>
#include <string.h>
#include <conio.h>
#define MAXLEN 50
char *pMsgString = {"HDLC defines three different data .................................."};
void display(char *pMss)
{
 printf("%s",pMss);
 getch();
}
void split(char *pMessage)
{
  int slen, leng;

  char *pE, *pF;

  slen = strlen(pMessage) ;
  pE = pMessage;
  do
  {
            pF = pE;
        leng = slen > MAXLEN ? MAXLEN : slen ;

        pE = pF + leng;

        while (*pE != ' ')
        {
               if (*pE == 0)
         break ;
             pE--;
         leng-- ;
        }

          leng=pE-pF ;

      printf("%*.*s\n", leng, leng, pF) ;
      getch();
      display(??);
      slen -= leng;
  } while(slen>0);

}
void main(void)
{
split (pMsgString);
}

hm... i dun quite get what u mean... actually what i want to know is how i store the segmented message into a pointer so that when i call the function it would only display the segmented part.In simple terms, how can i declare a pointer and place it into the ?? part so that the display function will only display the segmented message?
Hopefully u ppl can understand my english becos i a not quite good in it.
0
 

Author Comment

by:bbtss
ID: 8169021
#include <stdio.h>
#include <string.h>
#include <conio.h>
#define MAXLEN 50
char *pMsgString = {"HDLC defines three different data .................................."};
void display(char *pMss)
{
 printf("%s",pMss);
 getch();
}
void split(char *pMessage)
{
  int slen, leng;

  char *pE, *pF;

  slen = strlen(pMessage) ;
  pE = pMessage;
  do
  {
            pF = pE;
        leng = slen > MAXLEN ? MAXLEN : slen ;

        pE = pF + leng;

        while (*pE != ' ')
        {
               if (*pE == 0)
         break ;
             pE--;
         leng-- ;
        }

          leng=pE-pF ;

      printf("%*.*s\n", leng, leng, pF) ;
      getch();
      display(??);
      slen -= leng;
  } while(slen>0);

}
void main(void)
{
split (pMsgString);
}

hm... i dun quite get what u mean... actually what i want to know is how i store the segmented message into a pointer so that when i call the function it would only display the segmented part.In simple terms, how can i declare a pointer and place it into the ?? part so that the display function will only display the segmented message?
Hopefully u ppl can understand my english becos i a not quite good in it.
0
 

Author Comment

by:bbtss
ID: 8169033
#include <stdio.h>
#include <string.h>
#include <conio.h>
#define MAXLEN 50
char *pMsgString = {"HDLC defines three different data .................................."};
void display(char *pMss)
{
 printf("%s",pMss);
 getch();
}
void split(char *pMessage)
{
  int slen, leng;

  char *pE, *pF;

  slen = strlen(pMessage) ;
  pE = pMessage;
  do
  {
            pF = pE;
        leng = slen > MAXLEN ? MAXLEN : slen ;

        pE = pF + leng;

        while (*pE != ' ')
        {
               if (*pE == 0)
         break ;
             pE--;
         leng-- ;
        }

          leng=pE-pF ;

      printf("%*.*s\n", leng, leng, pF) ;
      getch();
      display(??);
      slen -= leng;
  } while(slen>0);

}
void main(void)
{
split (pMsgString);
}

hm... i dun quite get what u mean... actually what i want to know is how i store the segmented message into a pointer so that when i call the function it would only display the segmented part.In simple terms, how can i declare a pointer and place it into the ?? part so that the display function will only display the segmented message?
Hopefully u ppl can understand my english becos i a not quite good in it.
0
 

Author Comment

by:bbtss
ID: 8169060
#include <stdio.h>
#include <string.h>
#include <conio.h>
#define MAXLEN 50
char *pMsgString = {"HDLC defines three different data .................................."};
void display(char *pMss)
{
 printf("%s",pMss);
 getch();
}
void split(char *pMessage)
{
  int slen, leng;

  char *pE, *pF;

  slen = strlen(pMessage) ;
  pE = pMessage;
  do
  {
            pF = pE;
        leng = slen > MAXLEN ? MAXLEN : slen ;

        pE = pF + leng;

        while (*pE != ' ')
        {
               if (*pE == 0)
         break ;
             pE--;
         leng-- ;
        }

          leng=pE-pF ;

      printf("%*.*s\n", leng, leng, pF) ;
      getch();
      display(??);
      slen -= leng;
  } while(slen>0);

}
void main(void)
{
split (pMsgString);
}

hm... i dun quite get what u mean... actually what i want to know is how i store the segmented message into a pointer so that when i call the function it would only display the segmented part.In simple terms, how can i declare a pointer and place it into the ?? part so that the display function will only display the segmented message?
Hopefully u ppl can understand my english becos i a not quite good in it.
0
 

Author Comment

by:bbtss
ID: 8169065
#include <stdio.h>
#include <string.h>
#include <conio.h>
#define MAXLEN 50
char *pMsgString = {"HDLC defines three different data .................................."};
void display(char *pMss)
{
 printf("%s",pMss);
 getch();
}
void split(char *pMessage)
{
  int slen, leng;

  char *pE, *pF;

  slen = strlen(pMessage) ;
  pE = pMessage;
  do
  {
            pF = pE;
        leng = slen > MAXLEN ? MAXLEN : slen ;

        pE = pF + leng;

        while (*pE != ' ')
        {
               if (*pE == 0)
         break ;
             pE--;
         leng-- ;
        }

          leng=pE-pF ;

      printf("%*.*s\n", leng, leng, pF) ;
      getch();
      display(??);
      slen -= leng;
  } while(slen>0);

}
void main(void)
{
split (pMsgString);
}

hm... i dun quite get what u mean... actually what i want to know is how i store the segmented message into a pointer so that when i call the function it would only display the segmented part.In simple terms, how can i declare a pointer and place it into the ?? part so that the display function will only display the segmented message?
Hopefully u ppl can understand my english becos i a not quite good in it.
0
 

Author Comment

by:bbtss
ID: 8169068
#include <stdio.h>
#include <string.h>
#include <conio.h>
#define MAXLEN 50
char *pMsgString = {"HDLC defines three different data .................................."};
void display(char *pMss)
{
 printf("%s",pMss);
 getch();
}
void split(char *pMessage)
{
  int slen, leng;

  char *pE, *pF;

  slen = strlen(pMessage) ;
  pE = pMessage;
  do
  {
            pF = pE;
        leng = slen > MAXLEN ? MAXLEN : slen ;

        pE = pF + leng;

        while (*pE != ' ')
        {
               if (*pE == 0)
         break ;
             pE--;
         leng-- ;
        }

          leng=pE-pF ;

      printf("%*.*s\n", leng, leng, pF) ;
      getch();
      display(??);
      slen -= leng;
  } while(slen>0);

}
void main(void)
{
split (pMsgString);
}

hm... i dun quite get what u mean... actually what i want to know is how i store the segmented message into a pointer so that when i call the function it would only display the segmented part.In simple terms, how can i declare a pointer and place it into the ?? part so that the display function will only display the segmented message?
Hopefully u ppl can understand my english becos i a not quite good in it.
0
 

Author Comment

by:bbtss
ID: 8169072
#include <stdio.h>
#include <string.h>
#include <conio.h>
#define MAXLEN 50
char *pMsgString = {"HDLC defines three different data .................................."};
void display(char *pMss)
{
 printf("%s",pMss);
 getch();
}
void split(char *pMessage)
{
  int slen, leng;

  char *pE, *pF;

  slen = strlen(pMessage) ;
  pE = pMessage;
  do
  {
            pF = pE;
        leng = slen > MAXLEN ? MAXLEN : slen ;

        pE = pF + leng;

        while (*pE != ' ')
        {
               if (*pE == 0)
         break ;
             pE--;
         leng-- ;
        }

          leng=pE-pF ;

      printf("%*.*s\n", leng, leng, pF) ;
      getch();
      display(??);
      slen -= leng;
  } while(slen>0);

}
void main(void)
{
split (pMsgString);
}

hm... i dun quite get what u mean... actually what i want to know is how i store the segmented message into a pointer so that when i call the function it would only display the segmented part.In simple terms, how can i declare a pointer and place it into the ?? part so that the display function will only display the segmented message?
Hopefully u ppl can understand my english becos i a not quite good in it.
0
 

Author Comment

by:bbtss
ID: 8169087
#include <stdio.h>
#include <string.h>
#include <conio.h>
#define MAXLEN 50
char *pMsgString = {"HDLC defines three different data .................................."};
void display(char *pMss)
{
 printf("%s",pMss);
 getch();
}
void split(char *pMessage)
{
  int slen, leng;

  char *pE, *pF;

  slen = strlen(pMessage) ;
  pE = pMessage;
  do
  {
            pF = pE;
        leng = slen > MAXLEN ? MAXLEN : slen ;

        pE = pF + leng;

        while (*pE != ' ')
        {
               if (*pE == 0)
         break ;
             pE--;
         leng-- ;
        }

          leng=pE-pF ;

      printf("%*.*s\n", leng, leng, pF) ;
      getch();
      display(??);
      slen -= leng;
  } while(slen>0);

}
void main(void)
{
split (pMsgString);
}

hm... i dun quite get what u mean... actually what i want to know is how i store the segmented message into a pointer so that when i call the function it would only display the segmented part.In simple terms, how can i declare a pointer and place it into the ?? part so that the display function will only display the segmented message?
Hopefully u ppl can understand my english becos i a not quite good in it.
0
 

Author Comment

by:bbtss
ID: 8169107
#include <stdio.h>
#include <string.h>
#include <conio.h>
#define MAXLEN 50
char *pMsgString = {"HDLC defines three different data .................................."};
void display(char *pMss)
{
 printf("%s",pMss);
 getch();
}
void split(char *pMessage)
{
  int slen, leng;

  char *pE, *pF;

  slen = strlen(pMessage) ;
  pE = pMessage;
  do
  {
            pF = pE;
        leng = slen > MAXLEN ? MAXLEN : slen ;

        pE = pF + leng;

        while (*pE != ' ')
        {
               if (*pE == 0)
         break ;
             pE--;
         leng-- ;
        }

          leng=pE-pF ;

      printf("%*.*s\n", leng, leng, pF) ;
      getch();
      display(??);
      slen -= leng;
  } while(slen>0);

}
void main(void)
{
split (pMsgString);
}

hm... i dun quite get what u mean... actually what i want to know is how i store the segmented message into a pointer so that when i call the function it would only display the segmented part.In simple terms, how can i declare a pointer and place it into the ?? part so that the display function will only display the segmented message?
Hopefully u ppl can understand my english becos i a not quite good in it.
0
 

Author Comment

by:bbtss
ID: 8169130
#include <stdio.h>
#include <string.h>
#include <conio.h>
#define MAXLEN 50
char *pMsgString = {"HDLC defines three different data .................................."};
void display(char *pMss)
{
 printf("%s",pMss);
 getch();
}
void split(char *pMessage)
{
  int slen, leng;

  char *pE, *pF;

  slen = strlen(pMessage) ;
  pE = pMessage;
  do
  {
            pF = pE;
        leng = slen > MAXLEN ? MAXLEN : slen ;

        pE = pF + leng;

        while (*pE != ' ')
        {
               if (*pE == 0)
         break ;
             pE--;
         leng-- ;
        }

          leng=pE-pF ;

      printf("%*.*s\n", leng, leng, pF) ;
      getch();
      display(??);
      slen -= leng;
  } while(slen>0);

}
void main(void)
{
split (pMsgString);
}

hm... i dun quite get what u mean... actually what i want to know is how i store the segmented message into a pointer so that when i call the function it would only display the segmented part.In simple terms, how can i declare a pointer and place it into the ?? part so that the display function will only display the segmented message?
Hopefully u ppl can understand my english becos i a not quite good in it.
0
 

Author Comment

by:bbtss
ID: 8169153
#include <stdio.h>
#include <string.h>
#include <conio.h>
#define MAXLEN 50
char *pMsgString = {"HDLC defines three different data .................................."};
void display(char *pMss)
{
 printf("%s",pMss);
 getch();
}
void split(char *pMessage)
{
  int slen, leng;

  char *pE, *pF;

  slen = strlen(pMessage) ;
  pE = pMessage;
  do
  {
            pF = pE;
        leng = slen > MAXLEN ? MAXLEN : slen ;

        pE = pF + leng;

        while (*pE != ' ')
        {
               if (*pE == 0)
         break ;
             pE--;
         leng-- ;
        }

          leng=pE-pF ;

      printf("%*.*s\n", leng, leng, pF) ;
      getch();
      display(??);
      slen -= leng;
  } while(slen>0);

}
void main(void)
{
split (pMsgString);
}

hm... i dun quite get what u mean... actually what i want to know is how i store the segmented message into a pointer so that when i call the function it would only display the segmented part.In simple terms, how can i declare a pointer and place it into the ?? part so that the display function will only display the segmented message?
Hopefully u ppl can understand my english becos i a not quite good in it.
0
 

Expert Comment

by:lowmaka
ID: 8169526
Don't worry, English is my second language, too and sorry to confuse you. I was trying to work into your programming logic. Have you tried "strtok"? Some example code for you, good luck to you.



/* strtok example */
#include <stdio.h>
#include <string.h>

int main ()
{
  char str[] = "This is a sample string,just testing.";
  char * pch;
  printf ("Splitting string \"%s\" in tokens:\n",str);
  pch = strtok (str, " ");
  while (pch != NULL)
  {
    printf ("%s\n",pch);
    pch = strtok (NULL, " ,.");
  }
  return 0;
}
0
 

Author Comment

by:bbtss
ID: 8169894
#include <stdio.h>
#include <string.h>
#include <conio.h>
#define MAXLEN 50
char *pMsgString = {"HDLC defines three different data .................................."};
void display(char *pMss)
{
 printf("%s",pMss);
 getch();
}
void split(char *pMessage)
{
  int slen, leng;

  char *pE, *pF;

  slen = strlen(pMessage) ;
  pE = pMessage;
  do
  {
            pF = pE;
        leng = slen > MAXLEN ? MAXLEN : slen ;

        pE = pF + leng;

        while (*pE != ' ')
        {
               if (*pE == 0)
         break ;
             pE--;
         leng-- ;
        }

          leng=pE-pF ;

      printf("%*.*s\n", leng, leng, pF) ;
      getch();
      display(??);
      slen -= leng;
  } while(slen>0);

}
void main(void)
{
split (pMsgString);
}

hm... i dun quite get what u mean... actually what i want to know is how i store the segmented message into a pointer so that when i call the function it would only display the segmented part.In simple terms, how can i declare a pointer and place it into the ?? part so that the display function will only display the segmented message?
Hopefully u ppl can understand my english becos i a not quite good in it.
0
 

Author Comment

by:bbtss
ID: 8170339
#include <stdio.h>
#include <string.h>
#include <conio.h>
#define MAXLEN 50
char *pMsgString = {"HDLC defines three different data .................................."};
void display(char *pMss)
{
 printf("%s",pMss);
 getch();
}
void split(char *pMessage)
{
  int slen, leng;

  char *pE, *pF;

  slen = strlen(pMessage) ;
  pE = pMessage;
  do
  {
            pF = pE;
        leng = slen > MAXLEN ? MAXLEN : slen ;

        pE = pF + leng;

        while (*pE != ' ')
        {
               if (*pE == 0)
         break ;
             pE--;
         leng-- ;
        }

          leng=pE-pF ;

      printf("%*.*s\n", leng, leng, pF) ;
      getch();
      display(??);
      slen -= leng;
  } while(slen>0);

}
void main(void)
{
split (pMsgString);
}

hm... i dun quite get what u mean... actually what i want to know is how i store the segmented message into a pointer so that when i call the function it would only display the segmented part.In simple terms, how can i declare a pointer and place it into the ?? part so that the display function will only display the segmented message?
Hopefully u ppl can understand my english becos i a not quite good in it.
0
 

Author Comment

by:bbtss
ID: 8170474
Wow i didnt know i was pasting so much time...
well strtok seems to be only able to terminate at a null term. in my case i am suppose to send the characters 50 by 50. so i doubt i can use that.
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 8171658
>> strtok seems to be only able to terminate at a null term

No, it'll terminate at whichever character you want. e.g., if you want it to terminate at a comma:

char str[] = "Eagles, Guns n' Roses, Aerosmith, Metallica" ;
char * p ;

p = strtok ( str, "," ) ;

while ( p != NULL )
{
  printf ( "%s\n", p ) ;
  p = strtok ( NULL, "," ) ;

} // end while

It'll print:

Eagles
<space>Guns n' Roses
<space>Aerosmith
<space>Metallica

Hope that helps!

Mayank.
 
0
 
LVL 30

Accepted Solution

by:
Mayank S earned 1160 total points
ID: 8171679
If you want to store that segmented part in another 'pointer' (as you said), I'd say that you prefer to keep an array of strings and keep copying your segmented parts (substrings) as:

char str[] = "Eagles, Guns n' Roses, Aerosmith, Metallica" ;
char * p ;
char temp[10][80] ; // assuming 10 substrings
int count = 0 ;

p = strtok ( str, "," ) ;

while ( p != NULL )
{
  strcpy ( temp[count++], p ) ;
  p = strtok ( NULL, "," ) ;

} // end while

for ( int i = 0 ; i < count ; i ++ )
  printf ( "%s\n", temp[i] ) ; // end for


Hope that helps!

Mayank.
0
 
LVL 5

Expert Comment

by:isprabu
ID: 8173012
You can try something like this:
Declare a local array variable inside the split function

     char buff[MAXLEN+1];

and then instead of printf use sprintf:

     sprintf(buff,"%*.*s\n", leng, leng, pF) ;
     display(buff);
0

Featured Post

Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
This is about my first experience with programming Arduino.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question