字符串末尾的垃圾字符?

时间:2010-08-21 07:54:59

标签: c++ string char text-extraction

您好,我正在阅读一个字符串并打破每个单词并将其分类为姓名电子邮件和电话号码。使用字符串joe bloggs joeblog@live.com 12345。但是,一旦我打破了一切,包含名称,电子邮件和电话号码的个别分隔变量在它们的末尾都有垃圾字符。我无法弄清楚原因。

测试文件

//test file
#include <iostream>
#include <string>
#include "iofunc.h"
using namespace std;
int main(){
    string str1 = "joe bloggs joeblog@live.com 12345";

    iofunc func;
    cout<<"|-----------------------getname DEMONSTRATION------------------|\n" << endl;
    func.getName(str1);

    cout<<"the names are: " << func.glob_name << endl;

    cout<<"\n|-----------------------getphone DEMONSTRATION------------------|\n" << endl;
    func.getPhone(str1);
    cout<<"the phone number is:" << func.glob_phone << endl;

    cout<<"\n|-----------------------getemail DEMONSTRATION------------------|\n" << endl;
    func.getEmail(str1);
    cout<<"the email address is:" << func.glob_email << endl;


    return 0;
}

这是我的获取名称功能,该类太大而无法滚动:)

void iofunc::getName(string arg){
    lineProcess(arg); 
    //make sure to call this depending on what function u are using

    int name_count = 0;
    int wspace_count = 0;
    int arg_len = arg.length();
    //int char_len = 0;
    char name_temp[80];

    name_count = numberofNames(); 
    //line process was called before so this will work, 
    //make sure you call line process before using this function

    //for special, condition when there is no space in front of names
    if (special_condition == true){
        int i = 0;
        while(i < arg_len){
            name_temp[i] = arg[i];
            i++;
        }
        glob_name = string(name_temp);

    }

    if (special_condition == false){
        if (name_count == 1){
            int i = 0;
            while (arg[i] != ' '){
                name_temp[i] = arg[i];
                i++;
            }
            glob_name = string(name_temp);
        }

        //for 2 names
        if (name_count == 2){
            for (int i = 0; i < arg_len;i++){
                if (arg[i] == ' '){
                    wspace_count++;
                }
                if (wspace_count !=2){
                    name_temp[i] = arg[i];
                }
            }
            glob_name = string(name_temp);
        }
        //for 3 names
        if (name_count == 3){
            for (int i = 0; i < arg_len;i++){
                if (arg[i] == ' '){
                    wspace_count++;
                }
                if (wspace_count !=3){
                    name_temp[i] = arg[i];
                }
            }
            glob_name = string(name_temp);
        }
    }

}

所有的基本要点是,我使用名为lineProcess的函数来判断参数字符串中是否有电子邮件,电话和名称,而numberofNames函数给出了多少名称,以便我可以采取相应的行动。

我必须使用char name_temp来复制字符串中的名称,以便我可以只提取它并将其分配给名为string的{​​{1}}变量。它复制了我需要的所有东西,但它在每个提取的字符串之后给了我垃圾。

任何想法?。

EDITED

glob_name

4 个答案:

答案 0 :(得分:2)

添加到每个新字符串'\ 0'结束字符串符号

答案 1 :(得分:0)

字符串末尾的垃圾字符可能表示您没有空字符串终止字符串(以0x00字节结束)。这会导致字符串继续读取,直到下一个空字符,该字符实际上是字符串内存结束的位置。在某些情况下,这甚至可能导致分段错误。

您可以通过将'\0'添加到您创建的每个新字符串的末尾来解决此问题。请注意,您现在必须分配一个大一个字节的字符串,以保存新的结束字符。

答案 2 :(得分:0)

当你做这样的事情时:

    while(i < arg_len){ 
        name_temp[i] = arg[i]; 
        i++; 
    } 

您正在将字符串的字符复制到name_tmp,而不是将终止字符串的末尾的0复制。

答案 3 :(得分:0)

其他人指出了你正确的方向,你没有适当地终止你的c弦。声明一个长度为80的char数组只指向一块内存,它不会以任何方式初始化数组,这意味着除非你/ 0终止你复制到它的字符串,否则你将得到所有废话最后是80个字符。

我在15年内没有写过C ++,所以下面的代码可能不会起作用,但希望它会为你提供一些更优雅和可维护的解决方案的想法。

void iofunc::getName(string arg){
    lineProcess(arg); 
    //make sure to call this depending on what function u are using

    int name_count = 0;
    int wspace_count = 0;
    int arg_len = arg.length();
    //int char_len = 0;
    string name_temp;

    // Let's assemble a c-str version if the inbound arg string
    char* cstr;
    cstr = new char [arg.size()+1];
    strcpy (cstr, arg.c_str());

    name_count = numberofNames(); 
    //line process was called before so this will work, 
    //make sure you call line process before using this function

    //for special, condition when there is no space in front of names
    if (special_condition == true){
        glob_name = arg;
    }

    if (special_condition == false){
        // Assuming there's at least 1 name, which we have to otherwise the original
        // code may never set glob_name, let's use the C String function strtok
        // to tokenise our newly created c string at each " ".
        // Grab the first name.
        name_temp = string(strtok(cstr, " "));
        for (int i = 1; i < name_count; i++) {
            // Grab names 2 to name_count as required and append them to name_temp
            // We need to reinsert the space as strtok doesn't grab it.
            name_temp += " " + string(strtok(NULL, " "));
        }
        // Assign our final name to glob_name
        glob_name = name_temp;
    }

}
相关问题