2013-05-31 72 views
0

如何从该字符串中提取碎片?将字符串切成块

我有一个包含文件:

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

我想要做的就是通过输入行的文件行,然后切串入一些碎片。

  • 0065455准备到结构a[0].num
  • 苹果将在结构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$%d\n",&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 %d\n",&a[i].num,a[i].name,&a[i].dollar); 
sscanf(str,"%d %[^$]s%d\n",&a[i].num,a[i].name,&a[i].dollar); 
sscanf(str,"%d %s$%d\n",&a[i].num,a[i].name,&a[i].dollar); 

*/ 

有1点更多的问题:字符串的第一部分是从0开始int,但零没有在INT被接受。怎么做?

这是工作,我现在想,但还是parasing串入一个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 %d\n",&a[i].num,a[i].name,&a[i].dollar); 
sscanf(str,"%d %[^$]s%d\n",&a[i].num,a[i].name,&a[i].dollar); 
sscanf(str,"%d %s$%d\n",&a[i].num,a[i].name,&a[i].dollar); 

*/ 
+0

你的意思是'0065455会在struct a [0] .num'中出现吗? (而不是'a [0] .int') –

+0

sscanf在“[^ $]”命中时“停止扫描”。您需要第二个sscanf,从您离开的位置开始(读取的字符数由sscanf返回),然后在$符号后读取该值。 – Floris

+0

他想提取字符串在三块,逻辑是,他应该找到第一个'space',第二'$''从APPLE 0065445 $ 456',他将得到'0065445','APPLE'和'456' – vikas

回答

0

0's不会出现在打印整数a[i].num

您可以使a[i].num为字符串(char[])或整数数组。使0's显示出来。你可以将它解析为一个整数(通过atoi(str)),如果你需要它被别人使用。

+0

确定0在parase后会保留在那里吗? – UmeRonaldo

+0

好吧,你可以遍历字符串,直到找到第一个非零值,然后从那里开始解析。 –

+0

使用atoi(str)将字符串转换为整数。 http://www.cplusplus.com/reference/cstdlib/atoi/ –

3

基于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直接从流初始化向量:

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

保持前导零的第一个数字最简单的方法可能是它从01变至std::string

+0

Gota学习了很多之前使用此,但谢谢 – UmeRonaldo

2

使用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 %s\n", line, fruit, number); 

    return 0; 
} 

一些样本输出:

$ ./a.out 
0065445 APPLE 4056 

这是你需要什么?

+0

是的谢谢你!我在比赛中尝试了strtok,但我没有正确记住它。谢谢哥们 – UmeRonaldo

+0

但它是坏在C++好使用过 – UmeRonaldo

+0

,C++是C,最好的一个**非常**不同的语言要坚持C++做的事情是这样的方式,如果你可以用C写++。 – Nobilis

0
#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; 
}