C/C++ main读取命令行很大的argv输入参数时的转化问题

126 阅读1分钟
#include <cfloat>
#include <ctime>
#include <iostream>
#include <omp.h>
#include <random>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
typedef long long int lli;

lli n;

int main(int argc, char **argv) {

// 方法0:范围有限
n = atoi(argv[1]);

// 方法1:假设输入是1后面跟很多个0
int l = strlen(argv[1]) - 1;  // 输入的0的个数
int l2 = sizeof(argv[1]) - 1; // 每次都是7,不能用这个
cout << l << "  " << l2 << endl;
n = 1;
while (l--)
    n *= 10;
cout << n << endl;  // 可以使得和输入一样

// 方法2
sscanf(argv[1], "%lld", &n); // 方法2
cout << n << endl;

// 方法3
n = strtoll(argv[1], NULL, 10);
cout << n << endl;

// 方法4
n = strtold(argv[1], NULL);
cout << n << endl;

return 0;
}