简体   繁体   English

如何从char数组中提取数字字符串成员

[英]how to extract a number string member from a char array

i want to extract number string values of a char array. 我想提取一个char数组的数字字符串值。 Actually I want to extract numbers embeded in file names for some file management. 实际上,我想提取嵌入文件名中的数字以进行某些文件管理。 For example if there is a file name as file21 then i want the decimal number 21 from this file name. 例如,如果有一个文件名作为file21,那么我想要此文件名中的十进制数字21

How can i extract these values? 如何提取这些值?

I tried the following but it results in an unexpected value. 我尝试了以下操作,但是会导致意外的值。 I think it is as a result of the implicit typecasting from the char to int while doing the arthimetic operation. 我认为这是由于在进行人工操作时从char到int进行隐式类型转换的结果。

char * fname;
cout<<"enter file name";
cin>>fname;

int filenum=fname[4]%10+fname[5];
cout<<"file number is"<<filenum;

NOTE: The filenamse are strictly in the format fileXX , XX being numbers between 01 and 99 注:filenamse严格的格式fileXX,01和99之间的XX是数字

You're getting undefined behavior because you're never allocating memory for the char* you read into: 您将得到未定义的行为,因为您从未为读入的char*分配内存:

char * fname = new char[16];  //should be enough for your filename format

or better yet 或更好

char fname[16];

Also, what do you expect: 另外,您期望什么:

fname[4]%10+fname[5];

to do? 去做? Magically concatenate the numbers? 神奇地串联数字?

First, you convert the first char to an int , multiply it by 10 , convert the second char to an int and add to the first one. 首先,将第一个字符转换为int ,再乘以10 ,将第二个字符转换为int并加到第一个。 A simple google search for char to int would get you there. 一个简单的谷歌搜索char to int将带您到那里。

You need to subtract '0' to get the decimal value of a digit character: 您需要减去'0'以获取数字字符的十进制值:

int filenum=(fname[4]-'0')*10+(fname[5]-'0');

Better yet, you should use atoi : 更好的是,您应该使用atoi

int filenum = atoi(fname+4);

How can i extract these values? 如何提取这些值?

There are an infinite number of ways. 有无数种方法。 One way is to use std::istringstream : 一种方法是使用std::istringstream

#include <string>
#include <sstream>
#include <iostream>

int main () {
  std::string fname;
  std::cout << "Enter file name: ";
  std::getline(std::cin, fname);

  int filenum;
  std::istringstream stream(fname.substr(4,2));

  if(stream >> filenum)
    std::cout << "file number is " << filenum << "\n";
  else
    std::cout << "Merde\n";
}

Here is the simplest code: 这是最简单的代码:

#include <iostream>
#include <sstream>
#include <string>
using namespace std;
int main()
{
  int filenum;
  string fname;
  cout<<"enter file name";
  cin>>fname;

  string str2 = fname.substr(4,2);
  istringstream(str2) >> filenum;
  cout<<"file number is"<<filenum;
  return 0;
}

If your input is that much defined, the simplest solution is scanf: 如果您的输入定义如此明确,则最简单的解决方案是scanf:

int main()
{
    int theNumber = 0;
    scanf("file%d.txt", &theNumber);
    printf("your number is %d", theNumber);
}

Check it out in action, reading from char* instead of stdio: http://codepad.org/JFqS70yI 实际检查一下,从char *而不是stdio读取: http : //codepad.org/JFqS70yI

scanf (and sscanf) also throws in checking for proper input format: returns number of fields read successfully. scanf(和sscanf)还会抛出检查正确输入格式的信息:返回成功读取的字段数。 In this case if return value is any other than 1, the input was wrong. 在这种情况下,如果返回值不是1,则输入错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM