2013-05-31 77 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; 
}