Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/125.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 使用getline()读取文件时,每个字符之间是否会出现空格?_C++_Getline - Fatal编程技术网

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

C++ 使用getline()读取文件时,每个字符之间是否会出现空格?,c++,getline,C++,Getline,我正在设计一个程序,使用dir>music.txt获取目录列表。我的目标是从文件中删除权限和日期,并按艺术家列表的字母顺序排列。使用getlinefile、input时,输入变量发送到屏幕时,每个字符之间都会出现空格。代码如下: #include "stdafx.h" using namespace std; void AlphaSort(string (&data)[300], int size); void PrintArray(string(&data)[300]);

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

#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/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                 
我正在设计一个程序,使用dir>music.txt获取目录列表

不要。如果要使用目录内容,请直接使用它

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

如果不使用dir,则不会列出权限和日期信息


也是,C++不是一个很好的工具。您似乎是在Windows上,所以请尝试编写PowerShell脚本,或者如果您安装了Unix ish工具,请尝试编写bash。这应该很简单。

这是cout的输出,可能是一个不相关的错误:输入可能是非ASCII编码吗?用二进制编辑器查看文件。它可能使用多字节编码,C++不理解。为什么不读取目录而不是解析“DRE”命令提示的结果将文件编码为:UCS-2 LE BOM。根据Notepad++。我正在保存文件并使用UTF-8重试。
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