2017-06-13 39 views
1

我无法获得以下功能的预期输出。为什么我得到相同的字符串输出?

in.txt文件

-rw-r--r-- 1 air staff StructEx.cpp 
-rw-r--r-- 1 air staff Struct2ex.cpp 
drwxr-xr-x 3 air staff app.dSYM 
-rw-r--r-- 1 air staff MyStruct.cpp 

除外 - 从以上in.txt文件4页的内容打印第1列和列。像下面

-rw-r--r--StructEx.cpp 
-rw-r--r--Struct2ex.cpp 
drwxr-xr-xapp.dSYM 
-rw-r--r--MyStruct.cpp 

实际输出一些东西 -

-rw-r--r--tructEx.cpp 
-rw-r--r--tructEx.cpp 
-rw-r--r--tructEx.cpp 
-rw-r--r--tructEx.cpp 

上面的输出是错的,什么我下面的字符串流功能缺失英寸

void readingFromFile2(){ 

     ifstream inputFile("in.txt"); 
     string line; 
     stringstream entireLine; 
     char s = ' '; 
     string p1, p2, p3, p4, p5; 

     while(!inputFile.fail()){ 
     getline(inputFile, line); 
     entireLine << line; 
     entireLine >> p1 >> s >> p2 >> s >> p3 >> s >> p4 >> s >> p5 ; 
     cout << p1 << p4 << endl ; 
     } 

    } 

从重要的评论编辑,我想下面的功能更有意义,但还是一样的出去放

void readingFromFile2(){ 

    ifstream inputFile("in.txt"); 
    string line; 
    stringstream entireLine; 
    char s = ' '; 
    string p1, p2, p3, p4, p5; 
    char n ='\n'; 

    while(getline(inputFile, line)){ 
    entireLine << line; 
    entireLine >> p1 >> p2 >> p3 >> p4 >> p5 ; 
    cout << p1 << p4 << endl ; 
    } 
} 

更新 - 整个代码

#include <iostream> 
#include <fstream> 
#include <sstream> 

using namespace std; 

void readingFromFile2(); 

int main(){ 
    readingFromFile2(); 
} 

void readingFromFile2(){ 

    ifstream inputFile("in.txt"); 
    string line; 
    stringstream entireLine; 
    string p1, p2, p3, p4, p5; 
    while(getline(inputFile, line)){ 
    entireLine << line; 
    entireLine >> p1 >> p2 >> p3 >> p4 >> p5 ; 
    cout << p1 << p2 << p3 << p3 << p4 << p5 << endl ; 
    } 
} 

现在是工作, 使用stringstream entireLine(line);而不是entireLine << line;

+1

除非你的教授是在使用了iostream的LIB坚持,让您的生活更轻松,并使用正则表达式。并且绝对不要在现实世界中使用C++ iostream库。他们很可怕。 (很明显) – EdH

+0

将entireLine解析为字符串会跳过空格。尝试没有“s”字段。 – Rakurai

+1

流已经解析了空的空间。通过提供's',他们不需要“额外的帮助”。 – PaulMcKenzie

回答

5

在使用>>流输入你应该使用

entireLine >> p1 >> p2 >> p3 >> p4 >> p5 ; 

因为空间(' '\t\n)将被忽略。

UPDATE:

为什么 “-RW-R - R-- 1个空军参谋StructEx.cpp” 给出tructEx.cpp而不是staff当使用entireLine >> p1 >> s >> p2 >> s >> p3 >> s >> p4 >> s >> p5 ;

让我们来看看一步一步:

1)entireLine >> p1

串,直到第一空间被读取到p1-rw-r--r--p1

2)entireLine >> s

跳过空间1转到s

3)entireLine >> p2

跳过空间和air进入p2

4)entireLine >> s

跳过空间和s(的staff开头)转移到s

5)entireLine >> p3

只读taffp3

6)entireLine >> s

跳过空间和S(的StructEx.cpp开始)去s

7)entireLine >> p4

最终我们价值p4,这是tructEx.cpp,因为在步骤6)采取Ss

更新2

简化的演示:

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

using namespace std; 

int main() { 
    string line = "-rw-r--r-- 1 air staff StructEx.cpp"; 
    stringstream entireLine; 
    char s = ' '; // not required 
    string p1, p2, p3, p4, p5; 
    char n = '\n'; // not required 

    entireLine << line; 
    entireLine >> p1 >> p2 >> p3 >> p4 >> p5; 
    cout << p1 << ", " << p2 << ", " << p3 << ", " << p4 << ", " << p5 << endl; 

    return 0; 
} 

enter image description here

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

using namespace std; 

int main() { 
    string line = "-rw-r--r-- 1 air staff StructEx.cpp"; 
    stringstream entireLine; 
    char s = ' '; // initialization has no sense 
    string p1, p2, p3, p4, p5; 
    char n = '\n'; // not required 

    entireLine << line; 
    entireLine >> p1 >> s >> p2 >> s >> p3 >> s >> p4 >> s >> p5; // HERE!!! 
    cout << p1 << ", " << p2 << ", " << p3 << ", " << p4 << ", " << p5 << endl; 

    return 0; 
} 

产生

enter image description here

最后更新

void readingFromFile2(){ 

    ifstream inputFile("in.txt"); 
    string line; 
    //stringstream entireLine; 
    string p1, p2, p3, p4, p5; 
    char str[51]; 
    while (getline(inputFile, line)){ 
     stringstream entireLine(line); //entireLine << line; 
     entireLine >> p1 >> p2 >> p3 >> p4 >> p5; 
     cout << p1 << p4 << endl; 
    } 
} 
+0

它是否适合你?它不工作。我试了两次,没有谢谢 – irobo

+0

在'while'语句中改变条件 – VolAnd

+0

很好的解释了我的详细问题,但问题是当我从文件的下一行读取它没有改变时,我改变了,而b ut仍然,它得到相同的输出 – irobo

相关问题