【问题标题】:c++, get phone number from txt filec++,从txt文件中获取电话号码
【发布时间】:2009-12-03 16:34:54
【问题描述】:

我正在尝试将格式为 555-555-5555 的电话号码输入到具有三个 int 的结构中。我尝试使用带有“-”分隔符的 getline,但我不断收到错误消息:“无法将参数 1 从 'int' 转换为 'char *'”。

我尝试创建一个临时 char* 变量来存储数字,然后将其类型转换为 int,但这不起作用。

我该怎么做呢?

谢谢

编辑:

这里是一些代码:

void User::Input(istream& infile) {

    char* phone_temp;

    ...

    infile.getline(phone_temp, sizeof(phoneNum.areaCode), "-");
    phoneNum.areaCode = (int)phone_temp;

    ...
}

【问题讨论】:

  • 你能告诉我们一些你的代码吗?
  • 那可能会崩溃。您尚未为 phone_temp 分配任何内存。

标签: c++


【解决方案1】:

由于您将其发布为 c++ 问题,而不是 c 问题,请使用 istringstream http://www.cplusplus.com/reference/iostream/istringstream/

在我看来,你的代码会变成这样:

std::string sPhoneNum("555-555-5555");
struct
{
   int p1;
   int p2;
   int p3;
} phone;
char dummy;

std::istringstream iss(sPhoneNum);
iss >> phone.p1; // first part
iss >> dummy;    // '-' character
iss >> phone.p2; // second part
iss >> dummy;    // '-' character
iss >> phone.p2; // last part

编辑: 现在您已经发布了示例代码,我看到您已经从 istream 开始,您可以直接使用 >> 运算符,无需创建另一个 istringstream 运算符。查看示例:http://www.cplusplus.com/reference/iostream/istream/operator%3E%3E/

此外,如果您不需要,请远离使用 char * 和 atoi 的 c 样式转换方法,使用 std::stringistreams 是“正确的”C++ 方式。它避免了内存泄漏和其他令人讨厌的问题。

【讨论】:

    【解决方案2】:

    从流中读取电话号码:

    假设数字格式正确:

    void User::Input(istream& infile)
    {    
    
        int part1;
        int part2;
        int part3;
        char dash1; 
        char dash2; 
    
        infile >> part1 >> dash1 >> part2 >> dash2 >> part3;
    
        /*
         * !infile will return false if the file is in a bad state.
         *         This will happen if it fails to read a number from
         *         the input stream or the stream ran out of data.
         *
         * Both these conditions constitute an error as not all the values will
         * be set correctly. Also check that the dash[12] hold the dash character.
         * Otherwise there may be some other formatting problem.
         */ 
        if ((!infile) || (dash1 != '-') || (dash2 != '-'))
        {
             throw int(5); // convert this to your own exception object.
        }
    }
    

    【讨论】:

      【解决方案3】:

      如果我理解正确,请尝试使用 atoi() 或 stringstream 将 char* 转换为 int

      【讨论】:

        【解决方案4】:

        请参阅example,了解如何标记该行。这个question 也会有所帮助。

        然后使用 atoi 将字符串转换为 int。

        【讨论】:

        • 对其进行标记将是 imo 的最佳方式。
        【解决方案5】:

        您不能将 char* 转换为 int 并期望得到正确的值。 char* 是内存中的地址,因此当您将其转换为 int 时,您将在 int 中获得内存地址。您需要调用一个函数,例如atoi(),通过算法将char*指向的数据转换为整数。

        【讨论】:

          【解决方案6】:

          另一个可行的选择是:

          char a[10],b[10],c[10];
          scanf("%d-%d-%d", a, b, c);
          

          【讨论】:

            【解决方案7】:

            您似乎正在尝试将 char 转换为整数,在这种情况下,您需要使用 atoi 函数或字符串流。

            【讨论】:

              【解决方案8】:

              不要使用infile.getline(),而是使用带有std::string的独立版本:

              getfile(infile, buffer);
              

              之后,如果您愿意,可以添加getline()

              istringstream phonenumber(buiffer);
              string areacode = getline(phonenumber, part1. '-');
              

              或者你可以使用提取器>>(这就是它的用途!)

              int areacode;
              phonenumber >> areacode;
              

              请注意:如果您使用的是char*,请确保为它分配空间,或者至少指向分配的空间。

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 2012-01-01
                • 1970-01-01
                • 2014-05-16
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2012-10-27
                相关资源
                最近更新 更多