Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Start from a position and to end...

Status
Not open for further replies.

bgreen

Programmer
Feb 20, 2003
185
CA
Hi,

I want to print from a certain position and print to the end how do I do this?

memset(buf2, ' ', sizeof(buf2));
buf2[76] = NULL;
memcpy(buf2, cheque.sp_amount, sizeof(buf2));

ptr1 = strrchr(buf2, ' ');
*ptr1 = NULL;
ptr1++;
fprintf(fd, "%s --\r\n", buf2);

diff = sizeof(buf2) - strlen(ptr1);

here I want to take the cheque.sp_amount and start from position diff and print to the end of the line. I am having difficulty figuring this one out. Help would be appreciated. Thanks.

ptr = ;

fprintf(fd, "%s\r\n", ptr);
fprintf(fd, "%s\r\n", cheque.sp_amount);

memset(buf, '*', sizeof(buf));
buf[76] = NULL;
memcpy(buf, ptr1, strlen(ptr1));
POS(1.5);
fprintf(fd, "%s\r\n\r\n", buf);
 
Please, read more about printf family format specifiers in man/help or any C book. Use precision specifier of %.ds for C strings (max number of printed characters for s format).
Don't use NULL macros for end-of-string zero terminator. NULL macros id (deprecated) surrogate for null pointer. Use constant 0 or #define EOS 0 (style and taste issue, of course)...
 
maybe I am no being clear. Please note that I am a very in experienced C programmer.

Here is my string:
Seventy Seven Million Seven Hundred Seventy Seven Thousand Seven Hundred Seventy Seven Dollars And 77 Cents

I want to go to the 74th position and print the remainder of the string:

Seventy Seven Dollars And 77 Cents

How can I do this?
 
The quick answer to your question is:
Code:
fprintf(fd, "%s\r\n", &cheque.sp_amount[76]);

The longer answer to what you are trying to do is the following example modification of your code which I think is what you are trying to do:

Code:
#include <stdio.h>
#include <string.h>

int main(void) {

  char sp_amount[120] = "Seventy Seven Million Seven Hundred Seventy Seven Thousand Seven Hundred Seventy Seven Dollars And 77 Cents";
  char buf[120];
  char buf2[120];
  int diff;
  char *ptr1;

  strcpy(buf2,sp_amount);
  buf2[76] = 0;
  ptr1 = strrchr(buf2,' ');
  *ptr1 = 0;

  printf("%s --\r\n", buf2);
  diff = strlen(buf2)+1;

  memset(buf, '*', sizeof(buf));
  buf[76] = 0;
  memcpy(buf, &sp_amount[diff], strlen(&sp_amount[diff]));
  printf("%s\r\n\r\n", buf);

  return 0;
}

Hope this helps to answer your question.
 
Some additions: in C (and C++) language an array name treats as a pointer to the 1st (index zero;) array element except in sizeof op argument position. You may refer to any place in your array if you add (using pointer arith) an index value to starting array pointer (i.e. array name):
Code:
char array[128];
...
char* prest = array + 100;
...
printf("...%s...",prest);
Of course, you may get a pointer with a construct
Code:
&array[index]
It is exactly same as
Code:
array+index
 
Thanks all for the help.

I figured it out.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top