把绳子切成碎片

Cutting a string into pieces

本文关键字:碎片      更新时间:2023-10-16

如何从这个字符串中提取片段?

我有一个文件包含:

0065445 APPLE$456
089464 MANGO$489
0012389 GUAVA$744

我要做的是逐行输入文件,然后将字符串切成几段。

  • 0065455将进入结构体a[0].num
  • APPLE将进入结构体a[0].name
  • 456将进入a[0].dollar结构

其他行也一样。

一切正常,但没有成功地将美元部分放入变量。

代码如下:

#include<cstdlib>
#include<iostream>
using namespace std ;
int main(){
FILE *fp;
fp = fopen("input.txt","r");
char str[80] ;
struct abc{
int num;
char name[20];
int dollar;
};
int i = 0;
while(fgets(str,79,fp)!=NULL){
struct abc a[i] ;
sscanf(str,"%d %[^$]s$%dn",&a[i].num,a[i].name,&a[i].dollar);
cout <<i+1 <<") Number : "<<a[i].num<<" Name :  "<< a[i].name <<" Dollar : "<< a[i].dollar << endl ;
i++;
}
return 0 ;
}
/* These didn't work too.
sscanf(str,"%d %[^$]s %dn",&a[i].num,a[i].name,&a[i].dollar);
sscanf(str,"%d %[^$]s%dn",&a[i].num,a[i].name,&a[i].dollar);
sscanf(str,"%d %s$%dn",&a[i].num,a[i].name,&a[i].dollar);
*/

还有一个问题:string的第一部分是一个以0开头的整型,但是整型中不接受0。怎么做呢?

这是我现在想要的工作,但仍然在将字符串解析为int后,我没有得到零:

#include<cstdlib>
#include<iostream>
#include<cstring>
using namespace std ;
int main(){
    FILE *fp;
    fp = fopen("input.txt","r");
    char str[80] ;
    char temp[80] ;
    struct abc{
        int num;
        char name[20];
        int dollar;
    };
    int i = 0;
    int j = 0 ;
    while(fgets(str,79,fp)!=NULL){
        i = 0;
        j = 0 ;
        struct abc a[i] ;
        char* ptr = 0; // this is used as a helper variable to strtok
        ptr = strtok(str, " $n"); // we specify the delimiters here
        while (ptr != NULL) 
        {
            if (j == 0){
                strcpy(temp, ptr);
                a[i].num = atoi(temp);
            }
            if (j == 1)
                strcpy(a[i].name, ptr);
            if (j == 2){
                strcpy(temp, ptr);
                a[i].dollar = atoi(temp);
            }
            ptr = strtok(NULL, " $n");
            j++;
        }
        cout <<i+1 <<") Number : "<<a[i].num<<" Name :  "<< a[i].name <<" Dollar : "<< a[i].dollar << endl ;
        i++;
    }
    return 0 ;
}
/* These didn't work either.
sscanf(str,"%d %[^$]s %dn",&a[i].num,a[i].name,&a[i].dollar);
sscanf(str,"%d %[^$]s%dn",&a[i].num,a[i].name,&a[i].dollar);
sscanf(str,"%d %s$%dn",&a[i].num,a[i].name,&a[i].dollar);
*/

基于c++标记,我会做一些不同的事情。首先,我将重载abc类型的流提取操作符:

std::istream &operator>>(std::istream &is, abc &a) { 
    is >> a.num;
    std::getline(is, a.name, '$');
    return is >> a.dollar;
}

然后您可以使用它来读取记录文件,例如:

abc temp;
std::vector<abc> a;
std::ifstream in("input.txt");
while (in >> temp)
    a.push_back(temp);

或者,您可以使用istream_iterator直接从流初始化vector:

std::vector<abc> a((std::istream_iterator<abc>(in)),
                    std::istream_iterator<abc>());

保持第一个数字前导零的最简单方法可能是将其从int更改为std::string

使用strtok:

这是一个简单的代码(仅C语言),分别打印字符串(我在另一篇文章中推荐了类似的解决方案)。

#include <stdio.h>
#include <string.h> // for strcpy and strtok
#include <stdlib.h> // for atoi
int main()
{
    char input [25] = "0065445 APPLE$4056"; // input string
    // storage for the separate parts of the string
    char line[10]; 
    char fruit[10];
    char number[10];
    char* ptr = 0; // this is used as a helper variable to strtok
    ptr = strtok(input, " $n"); // we specify the delimiters here
    int i = 0;
    // I'm using i here as a control variable so that during each iteration different part
    // of the string is saved
    while (ptr != NULL) 
    {
        if (i == 0)
            strcpy(line, ptr);
        if (i == 1)
            strcpy(fruit, ptr);
        if (i == 2)
            strcpy(number, ptr);
        ptr = strtok(NULL, " $n");
        i++;      
    }
    printf("%s %s %sn", line, fruit, number);
    return 0;
}

一些示例输出:

$ ./a.out 
0065445 APPLE 4056

这是你需要的吗?

当您打印整数a[i].num时,0's将不显示。

您可以将a[i].num设置为字符串(char[])或整数数组。让0's出现如果需要使用它,可以将其解析为整数(通过atoi(str))。

#include <iostream>
#include <fstream>
#include <sstream>
struct abc{ int num; std::string name; int dollar; };
int main(int argc, char* argv[]) {
    std::ifstream file("input");
    abc st1;
    std::string l;
    while (file >> st1.num >> l) {
        if (size_t p = l.find_first_of('$')) {
            st1.name = l.substr(0, p);
            std::istringstream(l.substr(p+1)) >> st1.dollar;
            std::cout << st1.num << " : "
                << st1.name << " : " << st1.dollar << std::endl;
        }
    }
    return 0;
}