【问题标题】:storing a substr into another variable for further assignments将 substr 存储到另一个变量中以进行进一步分配
【发布时间】:2018-07-27 23:48:48
【问题描述】:

我有这样的文字

学生 = 321321

姓名 = 詹妮弗·劳伦斯

课程 = 公关

电话号码 = 790-3233

我只想将等号后的数据存储到student->idstudent->namestudent->coursestudent->phone_no

Student *student[100];
string  str, line;
char * temp;
ifstream inFile;
inFile.open(fileName);

if (!inFile.is_open())
    return false;
else
{
    for(int i=0; i<100;i++)
    {
        for (int j=0; getline(inFile, line) && j < 4; j++)
        {
            if (line.compare(0, 7, "Student") == 0)
            {                   
                size_t pos = line.find("=");
                temp = line.substr(pos + 2);
                strcpy(student[i]->id, temp);
            }
            else if (line.compare(0, 4, "Name") == 0)
            {
                size_t pos = line.find("=");
                temp = line.substr(pos + 2);
                strcpy(student[i]->name, temp);
            }
            else if (line.compare(0, 6, "course") == 0)
            {                   
                size_t pos = line.find("=");
                temp = line.substr(pos + 2);
                strncpy(student[i]->course, temp);
            }
            else if (line.compare(0, 5, "Phone") == 0)
            {                   
                size_t pos = line.find("=");
                temp = line.substr(pos + 2);
                strcpy(student[i]->phone_no, temp);
            }
        }
    }
    return true;
}

错误发生在temp = line.substr(pos + 2); 行 它说: no suitable conversion function from "std::basic_string<char, std::char_traits<char>, std::allocator<char>>" to "char *" exists"

【问题讨论】:

  • 将错误信息放在您的问题中,而不是在图片中。同时创建一个Minimal, Complete, and Verifiable example
  • 对不起,我已经编辑了,请检查一下
  • 请添加Student的定义。更好的是,把它变成minimal reproducible example
  • 只需使用std::stringchar* 是一种 c-ism,你应该尽可能避免。
  • 警告:Student *student[100]; 是一个包含 100 个指向 Student 的指针的数组。您绝不会分配任何Students。考虑用Student student[100];std::vector&lt;Student&gt; student; 替换它,并使用push_backemplace_back 方法填充vector

标签: c++ string stl


【解决方案1】:

std::string::substr() 返回 std::string,而不是 char*。您可以使用std::string::c_str() 来获得strcpy 需要的const char*,而无需担心临时:

strcpy(student[i]->id, line.substr(pos + 2).c_str());

不过,我没有看到您在哪里为 Student::id 分配了存储空间,它是一个数组吗?即使你已经分配了存储空间,你也永远不会检查你是否有足够的空间。我强烈建议不要这样做,而是将Student::id 也设为std::string,这样很容易:

student[i]->id = line.substr(pos + 2);

【讨论】:

  • 我尝试使用 strcpy_s(student[i]->phone_no, line.substr(pos + 2).c_str()) 并发生运行时错误,我必须单击 break 来停止它
  • @BrianLee 查看您的更新,您还有其他与您的问题无关的错误。您已经创建了一个包含 100 个学生 指针 的数组。您根本没有创建任何 Student 对象。
【解决方案2】:

您必须为学生 [i] 管理内存分配和空闲。

1) 用nullptr初始化指针数组:

Student * student[100]{ nullptr };

2) 分配student[i] = new Student 对象,然后再为其赋值。

3) & 在退出之前,释放分配:

for (int i=0; i<100; i++) {
    If (student[i] != nullptr)
        delete student[i];
}

-

更好的做法是使用智能指针:

#include <array>
#include <memory>

// Declare student array:
std::array<std::unique_ptr<Student>, 100> student;

// Allocate a new Student object (since C++14):
student[i] = std::make_unique<Student>(); // Preferred, over C++11 method

// (Or instead,) allocate a new Student object (since C++11):
student[i].reset(new Student); // If no support for C++14

// Now, it's OK to assign to student[i]:
// strcpy(student[i]->id, temp);

// ** No need to free allocations. **

-

(除了@zdan 给你写的。)

【讨论】:

    猜你喜欢
    • 2019-04-03
    • 1970-01-01
    • 1970-01-01
    • 2017-02-01
    • 2018-07-20
    • 2015-01-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多