c++斯特托克问题

发布于 2024-08-20 23:32:43 字数 1661 浏览 11 评论 0原文

我正在尝试创建一个 word==>drow 的地图,例如 polindrom... 问题出在“strtok”的最后一级...... 首先我将其拆分,然后在后续调用中执行 strtok(NULL," "); 时工作正常。 问题是当我添加第二个字符串“poly_buffer”时...似乎它可以工作...

#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);
}

我做错了什么?

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 ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

枉心 2024-08-27 23:32:43

这两个 strtok 调用

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

互相干扰,您需要一一处理它们 - 您不能同时执行它们,因为它们在运行时库中共享静态内存。即首先执行 strtok(buffer," ") strtok(NULL, " ") 直到结束,然后执行 strtok( poly_buffer, " ")///

请参阅 strtok 的运行时参考文档

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

旧城烟雨 2024-08-27 23:32:43

如果您使用 C++,为什么要使用 strtok?使用字符串流进行标记并使用向量来包含单词:

#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;
  }
}

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;
  }
}
送君千里 2024-08-27 23:32:43

从 strtok、strtok_r 的手册页中:

"Avoid using these functions."

From the man page for strtok, strtok_r:

"Avoid using these functions."
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文