尝试将输入文件打开到数组

时间:2015-10-12 01:42:03

标签: c++ segmentation-fault fstream

对于我正在尝试创建的这个简单的音乐排序应用程序,我遇到了一些问题。我已经和它搏斗了一段时间并尝试了不同的东西,但不管我一直在分段错误。

#include <iostream>                 
#include <string>    
#include <cctype>
#include <iomanip>
#include <fstream>

using namespace std;

//Constants
const int MAX_CHAR = 101;
const int SONG_CAPACITY = 100;

struct song {
char title[MAX_CHAR];
char artist[MAX_CHAR];
char album[MAX_CHAR];
char duration[MAX_CHAR];
};

//Function Prototypes
void dispLibrary(song library[], int& index);
void loadLibrary(const char fileName[], song library[], int& index);
void addEntry(const char fileName[], song library[], int& index);

int main(){
int command;
song library[SONG_CAPACITY];
int index = 0;
char fileName[] = "songs.txt";

loadLibrary(fileName, library, index);
while(command != 5){
    cout << "Please select an action (1-5): ";
    cin >> command;
    switch(command){
        case 1:
             dispLibrary(library, index);
             break;
        case 2:
             break;
        case 3:
             break;
        case 4:
             break;
        case 5:
             break;
    }
}
}

void addEntry(const song& entry, song library[], int& index)
{
strcpy(library[index].title, entry.title);
strcpy(library[index].artist, entry.artist);
strcpy(library[index].album, entry.album);
strcpy(library[index].duration, entry.duration);
index++;
}

void loadLibrary(const char fileName[], song library[], int& index)
{
ifstream        songFile;
char            title[MAX_CHAR];
char            artist[MAX_CHAR];
char            album[MAX_CHAR];
char            duration[MAX_CHAR];
song            entry;

songFile.open(fileName);
if(!songFile)
{
    songFile.clear();
    cerr << endl << "Fail to open " << fileName << " for input!" << endl     << endl;
    //exit(1);
}

songFile.get(title, MAX_CHAR, ';');
while (!songFile.eof())
{
    songFile.get();                                     
    songFile.get(title, MAX_CHAR, ';');
    songFile.get(artist, MAX_CHAR, ';');
    songFile.get(album, MAX_CHAR, ';');
    songFile.get(duration, MAX_CHAR, '\n');
    songFile.ignore(100, '\n');             

    strcpy(entry.title, title);
    strcpy(entry.artist, artist);
    strcpy(entry.album, album);
    strcpy(entry.duration, duration);

    addEntry(entry, library, index);

    songFile.get(title, MAX_CHAR, ';');     //start the next record
}
songFile.close();
}

void dispLibrary(song library[], int& index){
int i;
cout << setw(10) << "Title" << setw(10) << "Artist" << setw(10) << "Album" << setw(10) << "Duration" << endl;
for(i=0; i<index; i++)
{
    cout << setw(10) << library[i].title << setw(10) << library[i].artist << endl;
}
}

我真的想知道为什么这段代码会因为分段错误而崩溃,感谢任何输入,谢谢。

1 个答案:

答案 0 :(得分:0)

您的一个问题是,您get标题的次数远远超过文件中显示的次数。假设您的文件包含:

title;artist;album;duration
title2;artist2;album2;duration

...我会在从文件中读取的每一行之后添加一些注释,以显示尚未读取的内容:

songFile.get(title, MAX_CHAR, ';'); // Read the first title.

/* artist;album;duration
 * title2;artist2;album2;duration
 */

while (!songFile.eof())
{
    songFile.get();  // Read the 'a' in "artist".                        

/* rtist;album;duration
 * title2;artist2;album2;duration
 */

    songFile.get(title, MAX_CHAR, ';'); // Read "rtist;", overwriting the title var that has already been read.

/* album;duration
 * title2;artist2;album2;duration
 */

    songFile.get(artist, MAX_CHAR, ';'); // Read "album;" into the artist variable.

/* duration
 * title2;artist2;album2;duration
 */
    songFile.get(album, MAX_CHAR, ';'); // Read "duration\ntitle2;" into the album variable.
                                        // Ignores the newline while it searches for the next semicolon.

/* artist2;album2;duration
 */
    songFile.get(duration, MAX_CHAR, '\n'); // Read until the second newline.

/* EOF */

    songFile.ignore(100, '\n');       // Read past the EOF. 
                                      // If there was a third record, this would skip it.

    strcpy(entry.title, title);
    strcpy(entry.artist, artist);
    strcpy(entry.album, album);
    strcpy(entry.duration, duration);

    addEntry(entry, library, index);

    songFile.get(title, MAX_CHAR, ';');     // Read past EOF again.
                                            // If there was a fourth record,
                                            // this would create the same
                                            // problem that was caused by
                                            // reading the title before
                                            // entering the while loop.
}
songFile.close();
}