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 wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to convert char * to char or int or whatever.

Status
Not open for further replies.

compe2b

Programmer
Jul 13, 2001
3
US
I used strtok() to parse the string that I read in using cin.getline, but now I need to perform opperations on the strings that the *token is pointing to. I experimented quite a bit but I'm not getting what I want.

here's what I have done:

Code:
~~
 char *token1;
 char *token2;
 char *token3;
 char string[100];

 cin.getline(string[], 100);

 token1 = strtok(Array, ",");
 token2 = strtok(NULL, ",");
 token3 = strtok(NULL, ",");

~~

Now I have pointers to the strings, but I need to convert some of the strings to chars and some of them to ints. What can I do? Or should I say, what's the best thing to do?

I've tried things like:

Code:
char temp_token1[] = (char) token1;
char temp_token2[] = (int) token2;
 
looks like token1 is a string and token2 should be an int... here is how I would go about doing it

char temp_token1[MAX_SIZE];
sprintf(temp_token1, token1,strlen(token1));
int value = atoi( token2);

however, I have had problems with strtok in the past and dont know if it contains a pointer to the beginning of the string or at the end. I think it may be at the end of the string.

Matt


 
also at a lower level you can
int x=strlen(token)
int ntok=0;
for(int i=0;i<x;i++)
{
ntok*=10;
ntok+=token-'0';
} John Fill
1c.bmp


ivfmd@mail.md
 
Thanks for you help, I figured that I would have to do something along those lines for getting the char[] out of the char *.

I have another couple of questions though:

1) If I pass the token pointer to a function that puts the value of token into a struct, and the original string is erased, do you lose the pointer?

For example:

Code:
void main()
{
 char String[MaxString] = &quot;Network,1024&quot;;
 char *Token1;
 char *Token2;
 long bits;

 cin.getline(String, (MaxString -1))

 Token1 = strtok(String, &quot;,&quot;);
 Token2 = strtok(NULL, &quot;,&quot;);
 
 bits = atol(Token2);

 Add_List_Node(Token1, bits);

}

void Add_List_Node(char *Name, long bits);
{

  networkdata.NetName = Name;
  networkdata.bits = bits;


  struct networkdata
  {
    char NetName[MaxSize];
    long bits;
  }
}
  
/* My code is actually more complicated than this, I'm creating several linked lists, traversing them and their data structures and insterting data where needed. I just need to know if the pointer is lost when the original string is lost*/

2) Question 2: I need to cat two strings together, but I can't get strcat to work correctly.

I need something like this:

Code:
#include <time.h>
#include <iostream.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>

void main()
{
  int Str_Len = 50;

  time_t t;
  char *s;
  char *Month;
  char *Month_Num;
  char *Day_Name;
  char *Day_Num;
  char *year;
  char *hh;
  char *mm;
  char *ss;
  char zero[Str_Len];
  zero[0] = '0';

  t = time(NULL);
  s = ctime(&t);

  Day_Name  = strtok(s, &quot; &quot;);
  Month     = strtok(NULL, &quot; &quot;);
  Day_Num   = strtok(NULL, &quot; &quot;);
  hh        = strtok(NULL, &quot; :&quot;);
  mm        = strtok(NULL, &quot;:&quot;);
  ss        = strtok(NULL, &quot;: &quot;);
  year      = strtok(NULL, &quot; \n&quot;);

  if(strcmp(Month, &quot;Jan&quot;) == 0) { Month_Num = &quot;01&quot;; }
  if(strcmp(Month, &quot;Feb&quot;) == 0) { Month_Num = &quot;02&quot;; }
  if(strcmp(Month, &quot;Mar&quot;) == 0) { Month_Num = &quot;03&quot;; }
  if(strcmp(Month, &quot;Apr&quot;) == 0) { Month_Num = &quot;04&quot;; }
  if(strcmp(Month, &quot;May&quot;) == 0) { Month_Num = &quot;05&quot;; }
  if(strcmp(Month, &quot;Jun&quot;) == 0) { Month_Num = &quot;06&quot;; }
  if(strcmp(Month, &quot;Jul&quot;) == 0) { Month_Num = &quot;07&quot;; }
  if(strcmp(Month, &quot;Aug&quot;) == 0) { Month_Num = &quot;08&quot;; }
  if(strcmp(Month, &quot;Sep&quot;) == 0) { Month_Num = &quot;09&quot;; }
  if(strcmp(Month, &quot;Oct&quot;) == 0) { Month_Num = &quot;10&quot;; }
  if(strcmp(Month, &quot;Nov&quot;) == 0) { Month_Num = &quot;11&quot;; }
  if(strcmp(Month, &quot;Dec&quot;) == 0) { Month_Num = &quot;12&quot;; }


char temp_day[Str_Len];

if(atoi(Day_Num) < 10)
{
 temp_day = strcat(zero[0], Day_Num);
 Day_Num = temp_day;
}

 cout << &quot;Dir= &quot; << year << &quot;_&quot; << Month_Num << &quot;_&quot; <<  temp_token1 << endl;
}


I have tried a number of differ ways of doing this, but I never can get the correct output. What I'm trying to do is put a zero in front of the day so 9 reads 09. I can cat the zero onto the end so 9 read 90 but not the way I want.

Thanks!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top