使用getline()读取文件时,每个字符之间会出现空格?

时间:2018-05-07 21:10:07

标签: c++ getline

我正在设计一个使用“dir> music.txt”获取目录列表的程序。我的目标是从文件中删除权限和日期,并按字母顺序排列艺术家列表。使用“getline(文件,输入)时,当”输入“变量发送到屏幕时,每个字符之间会出现空格。以下是代码:

#include "stdafx.h"

using namespace std;

void AlphaSort(string (&data)[300], int size);
void PrintArray(string(&data)[300]);

// This program will read in an array, the program will then remove any text 
// that is before 59 characters, then the program will remove any spaces that 
// are not succeeded by letters.
int main()
{
    fstream file;
    string input;
    string data[300];
    file.open("music.txt");

    while (getline(file, input))
    {
        cout << input << endl;
        // Scroll through the entire file. Copy the lines into memory
        for (int i = 0; i <= input.length() - 1; i++)
        {
            // Process input here...
        }
    }

    // The array has been loaded into memory, run the sort
    //AlphaSort(data, 300);
    //PrintArray(data);

    return 0;
}

以下是输出样本:

d - - - - -                   8 / 7 / 2 0 1 7     1 1 : 1 5   A M                                 E i f f e l   6 5                                                                                                                           
 d - - - - -                   8 / 7 / 2 0 1 7     1 1 : 1 9   A M                                 O n e   R e p u b l i c
 d - - - - -                   8 / 7 / 2 0 1 7     1 1 : 1 8   A M                                 M a r o o n   5      
 d - - - - -                   8 / 7 / 2 0 1 7     1 1 : 1 8   A M                                 L u m i n e e r s    
 d - - - - -                   8 / 7 / 2 0 1 7     1 1 : 1 8   A M                                 M y   C h e m i c a l   R o m a n c e
 d - - - - -                   8 / 7 / 2 0 1 7     1 1 : 1 4   A M                                 B o b   M a r l e y  
 d - - - - -                   8 / 7 / 2 0 1 7     1 1 : 1 9   A M                                 P a r a m o r e      
 d - - - - -                   8 / 7 / 2 0 1 7     1 1 : 1 7   A M                                 I n c u b u s        
 d - - - - -                   8 / 7 / 2 0 1 7     1 1 : 1 4   A M                                 C a r p e n t e r s  
 d - - - - -                   8 / 7 / 2 0 1 7     1 1 : 1 5   A M                                 F a i t h   N o   M o r e
 d - - - - -                   8 / 7 / 2 0 1 7     1 1 : 1 2   A M                                 B a s t i l l e      
 d - - - - -                   8 / 7 / 2 0 1 7     1 1 : 1 6   A M                                 F r a n k i e   G o e s   T o   H o l l y w o o d
 d - - - - -                   8 / 7 / 2 0 1 7     1 1 : 1 7   A M                                 H o o b a s t a n k  

正如你所看到的,每个角色之间都有空格。过去一小时我一直在看这个。我有一个更“正确”的方式从文件输入?下面是每个字符之间不包含空格的输入文件:

d-----         8/7/2017  11:15 AM                Eiffel 65                                                             
d-----         8/7/2017  11:19 AM                One Republic                                                          
d-----         8/7/2017  11:18 AM                Maroon 5                                                              
d-----         8/7/2017  11:18 AM                Lumineers                                                             
d-----         8/7/2017  11:18 AM                My Chemical Romance                                                   
d-----         8/7/2017  11:14 AM                Bob Marley                                                            
d-----         8/7/2017  11:19 AM                Paramore                                                              
d-----         8/7/2017  11:17 AM                Incubus                                                               
d-----         8/7/2017  11:14 AM                Carpenters                                                            
d-----         8/7/2017  11:15 AM                Faith No More                                                         
d-----         8/7/2017  11:12 AM                Bastille                                                              
d-----         8/7/2017  11:16 AM                Frankie Goes To Hollywood                                             
d-----         8/7/2017  11:17 AM                Hoobastank                                                            
d-----         8/7/2017  11:21 AM                Young The Giant                                                       
d-----         8/7/2017  11:15 AM                Disturbed                                                             
d-----         8/7/2017  11:12 AM                Authority Zero                 

1 个答案:

答案 0 :(得分:-1)

  

我正在设计一个程序,使用&#34; dir&gt;来获取目录列表。 music.txt&#34;

唐&#39;吨。如果您想使用目录内容,请直接使用它。

  

我的目标是从文件中删除权限和日期,并按字母顺序排列艺术家列表。

如果您不使用dir,则不会 拥有权限和日期信息。

此外,C ++不是用于此任务的好工具。你似乎是在Windows上,所以尝试编写PowerShell脚本,或者如果你安装了Unix-ish工具,请使用bash。它应该很简单。