开发者

c++ strtok problem

i'm trying to create a map of word==>drow, like polindrom... the problem is at the final level at "strtok"... first i split it, then in subsequent call when doing strtok(NULL," "); it works ok. the problem is when i add the second string "poly_buffer"... seems it works on it....

#include "stdafx.h"
#include <iostream>
#include <cstdio>
#include <string>
#include <map>
#include <string>
using namespace std;

void poly(char *buffer)
{
 char temp;
 for (int i=0; i<=strlen(buffer); i++)
 {
  int word_start = i, word_stop = i;

  while (buffer[i] != 32 && buffer[i] != '\0') { i++; word_stop++; }
  word_stop--;

  //swap chars until the middle of word
  while (word_stop >= word_start)
  {
   //swap the chars
   temp = buffer[word_stop];
   buffer[word_stop] = buffer[word_start];
   buffer[word_start] = temp;
   word_stop--;
   word_start++;
  }
  word_start = i;

 }
}


void main()
{
 FILE *fp;
 char *buffer;
 char *poly_buffer;
 开发者_运维百科long file_size;
 map<string,string> map_poly;

 fp = fopen("input.txt", "r");

 if (fp == NULL) { fputs("File Error",stderr); exit(1); }

 //get file size
 fseek(fp,1,SEEK_END);
 file_size = ftell(fp);
 rewind(fp);

 //allocate memory
 buffer = new char[file_size+1];
 poly_buffer = new char[file_size+1];

 //get file content into buffer
 fread(buffer,1, file_size,fp);
 strcpy(poly_buffer,buffer);

 buffer[file_size] = '\0';
 poly_buffer[file_size] = '\0';

 poly(buffer);

 buffer = strtok(buffer," ");
 poly_buffer = strtok(poly_buffer," ");

 while (buffer != NULL)
 {
  map_poly[buffer] = poly_buffer;
  printf("%s ==> %s\n", buffer, poly_buffer);
  buffer = strtok(NULL," ");
  poly_buffer = strtok(NULL," ");
 }

 fclose(fp);
 while(1);
}

what am i doing wrong ?


the both strtok calls

buffer = strtok(buffer, " ");
poly_buffer = strtok(poly_buffer," ");

are interfering with each other, you need to process them one by one - you cannot do them at the same time because they are sharing static memory in the runtime library. i.e. first do strtok(buffer," ") strtok(NULL, " ") until end, then do strtok( poly_buffer, " ")///

see runtime reference doc for strtok


If you are using C++, why on Earth would you use strtok? Use a stringstream to tokenise and a vector to contain the words:

#include <string>
#include <sstream>
#include <iostream>
#include <vector>
using namespace std;

int main() {
  istringsream is( "here are some words" );
  string word;
  vector <string> words;
  while( is >> word ) {
    words.push_back( word );
  }
  for ( unsigned int i = 0; i < words.size(); i++ ) {
    cout << "word #" << i << " is " << words[i] << endl;
  }
}


From the man page for strtok, strtok_r:

"Avoid using these functions."
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜