Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/146.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 使用strtoks显示文本文件中的文本_C++_String_Vector_Text Files_Strtok - Fatal编程技术网

C++ 使用strtoks显示文本文件中的文本

C++ 使用strtoks显示文本文件中的文本,c++,string,vector,text-files,strtok,C++,String,Vector,Text Files,Strtok,我可以得到一个文本被推回并显示在向量中,我确信我的编码中存在一些问题 代码如下: std::vector<char> myVector; ifstream myReadFile; myReadFile.open("E:/C++/Projects/Textfile project/Textfile project/class_data.txt", ios_base::in); char output[100]; if (myReadFile.i

我可以得到一个文本被推回并显示在向量中,我确信我的编码中存在一些问题

代码如下:

    std::vector<char> myVector;
    ifstream myReadFile;
    myReadFile.open("E:/C++/Projects/Textfile project/Textfile project/class_data.txt", ios_base::in); 
    char output[100];
    if (myReadFile.is_open()) {
        while (!myReadFile.eof()) {
            myReadFile >> output;
        }

    }
    myReadFile.close();

    char* token = NULL;
    char* context = NULL;
    char delims[] = " ,\t\n";


    token = strtok_s(output, delims, &context);

    while (token != NULL)
    {
        if (token != NULL) 
        {
            myVector.push_back(token);
            token = strtok_s(NULL, delims, &context);

        }
    }

    for (int i = 0; i < myVector.size(); i++) {
        cout << myVector[i] << endl;
    }
std::vector myVector;
ifstreammyreadfile;
myReadFile.open(“E:/C++/Projects/Textfile project/Textfile project/class_data.txt”,ios_base::in);
字符输出[100];
如果(myReadFile.is_open()){
而(!myReadFile.eof()){
myReadFile>>输出;
}
}
myReadFile.close();
char*token=NULL;
char*context=NULL;
字符delims[]=“,\t\n”;
令牌=strtok_s(输出、数据和上下文);
while(令牌!=NULL)
{
if(令牌!=NULL)
{
myVector.push_back(令牌);
token=strtok_s(NULL、delims和context);
}
}
对于(int i=0;icout而不是char类型的对象向量
std::vector myVector;
您需要定义
std::string类型的对象向量

std::vector<std::string> myVector;
无效,因为您正在尝试推送指针而不是字符

还有while循环中的if语句

while (token != NULL)
{
    if (token != NULL) 
    {
        myVector.push_back(token);
        token = strtok_s(NULL, delims, &context);

    }
}
是Superfluos,可以移除

while (token != NULL)
{
    myVector.push_back(token);
    token = strtok_s(NULL, delims, &context);

}
在这个循环中还要考虑到这一点

    while (!myReadFile.eof()) {
        myReadFile >> output;
    }

对象输出总是被覆盖。您应该将此循环与将输出拆分为令牌的循环结合起来。否则您将处理文件的最后一条记录。

显示
class_data.txt
class_data.txt中的一些文本…“A00529154 76 79 85 91 75 80 90 56 A00656624 79 85 0 86 76 51 89 92 A02507691 47 94 92 49 77 72 25 25 95 A00612352 41 82 90 58 87 0 50 98 80 A04012435 91 50 78 68 70 60 42 74 85 A00654400 47 94 89 75 80 76 0 71 83 A00577109 44 88 86 89 88 99 100 90 A00580920 41 82 80 90 89 97 93 86 A04028610 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90o莫斯科@Vlad@莫斯科@Vlad。你以前帮过我,你能再帮我一次吗,这次我有一个不同的问题。你能看看stackoverflow.com/questions/23233993/…或stackoverflow.com/questions/23189706/…请,你能告诉我怎么做吗?@Uther Pendragon你展示的参考资料可以不要使用。您好@Vlad,来自莫斯科。很抱歉。这是正确的参考资料:@Uther Pendragon我已经写了关于C++/CLI字体的帖子的答案。
while (token != NULL)
{
    myVector.push_back(token);
    token = strtok_s(NULL, delims, &context);

}
    while (!myReadFile.eof()) {
        myReadFile >> output;
    }