-2

I am designing a program that takes a directory list using "dir > music.txt". My goal is to remove the permissions and the date from the file and alphabetize the list of artists. When using "getline(file, input), spaces appear between each character when the "input" variable is sent to the screen. Here is the code:

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

Below is sample of the output:

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  

As you can see, there are spaces included between each character.I have been looking at this for the past hour. I there a more "correct" way to input from a file? Below is the input file that does not contain spaces between each character:

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 Answers1

-1

I am designing a program that takes a directory list using "dir > music.txt".

Don't. If you want to work with the directory contents, work with it directly.

My goal is to remove the permissions and the date from the file and alphabetize the list of artists.

If you wouldn't use dir, you wouldn't have permissions and date information listed.

Also, C++ is not a good tool to use for this task. You seem to be on Windows, so try writing a PowerShell script, or if you have Unix-ish tools installed, bash. It should be pretty simple.

einpoklum
  • 118,144
  • 57
  • 340
  • 684
  • Command prompt encodes the file to: UCS-2 LE BOM. according to Notepad++. I am saving the file and trying again with UTF-8. – Macdaddy4sure May 07 '18 at 21:47