相关疑难解决方法(0)

如何将字符串转换为 uint32_t

我有一个程序,它逐行读取文件的内容,将每一行存储到字符串向量中,然后打印向量的内容。

\n\n

将文件数据读入字符串向量后,我尝试将每一行stringuint32. 文件的每一行都由32-bit数字组成。输入数据文件的示例(input_file.dat)

\n\n
31401402\n67662718\n74620743\n54690001\n14530874\n13263047\n84662943\n09732679\n13839873\n
Run Code Online (Sandbox Code Playgroud)\n\n

我想将这些字符串中的每一个转换为uint32_t, 对于我编写的另一个程序,该程序将这些数字转换为 ASCII 格式(该程序需要用于uint32_t转换)。

\n\n

到目前为止我的计划:

\n\n
#include <iostream>\n#include <fstream>\n#include <string>\n#include <vector>\n\n/*\n * Program to iterate through all lines in file and put them in given vector\n *\n */\nbool getFileContent(std::string fileName, std::vector<std::string> & vecOfStrs)\n{\n\n    // Open the File\n    std::ifstream in(fileName.c_str());\n\n    // Check if object is valid\n    if(!in)\n    {\n        std::cerr << "Cannot open the File : "<<fileName<<std::endl;\n        return false;\n    }\n\n    std::string …
Run Code Online (Sandbox Code Playgroud)

c++ io types casting file-read

7
推荐指数
1
解决办法
4万
查看次数

c ++将字符串转换为int

//sLine is the string
for(int l = 0; l < sLine.length(); l++)
{
    string sNumber;
    if(sLine[l] == '-')
    {   
        sNumber.push_back(sLine[l]);
        sNumber.push_back(sLine[l + 1]);
        l++;
    }
    else if(sLine[l] != '\t')
    {
        sNumber.push_back(sLine[l]);
    }
    const char* testing = sNumber.c_str();
    int num = atoi(testing);
    cout << num;
}
Run Code Online (Sandbox Code Playgroud)

我有这个for循环,它检查字符串的每个字符,并将此字符串中的每个数字转换为int.但由于某种原因,atoi功能正在做两次,所以当我玩它时,由于某种原因显示它两次......为什么会这样?

例如:INPUT 3 3 -3 9 5
-8 -2 9 7 1
-7 8 4 4 -8
-9 -9 -1 -4 -8

输出3030-309050 -80-20907010
-70804040-80
-90-90-10-40-80

c++ string int

0
推荐指数
1
解决办法
804
查看次数

标签 统计

c++ ×2

casting ×1

file-read ×1

int ×1

io ×1

string ×1

types ×1