把绳子切成碎片
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;
}
相关文章:
- OpenGL在启用深度测试时不会丢弃我的碎片
- 避免碎片化的ClientHellos with OpenSSL (DTLS)
- 在嵌入式系统中读取文件的内存碎片
- 使用 ' ios::sync_with_stdio(0)' 测试运行时的差异时,为什么我的输出是碎片化的?
- 内存碎片会减慢新/malloc
- 最终的ARM Linux内存碎片与NEON Copy,但不是memcpy
- 如果有一个几何设计着色器,则如何将信息从顶点着色器传递到碎片着色器
- 内存碎片是否导致内存异常
- 颜色设置在碎片着色器中,未显示GLSL 1.30
- 在数组中移动碎片?C
- 更少的顶点与更少的碎片
- 没有碎片stl容器的大容量内存
- 碎片MP4-在浏览器中播放时出现问题
- 打印我们在最佳切割后获得的2尺寸杆切割的碎片
- 在调用GllinkProgram()之前,Vertex着色器和碎片着色器(S)无法编译
- 奇怪的 GLSL 碎片着色器颜色不起作用
- 无法在OpenGL碎片着色器中正确检索16位整数
- 是否有可能解决 CUDA 内存碎片问题
- 如果我使用碎片着色器绘制纹理,如何绘制红线
- visual C++堆碎片分配错误