Czytanie z vectora

0
#include <iostream>
#include <string.h>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <sstream>
#include <vector>
#include <dirent.h>
#include <string.h>
#include <conio.h>
#include <algorithm>
#include <set>
#include <typeinfo>

#include "Mp3TaggedFile.h"
using namespace std;

typedef std::vector<std::string> fileVect;
typedef std::set<std::string> mp3Names;
typedef std::vector<Mp3TaggedFile *> mp3List;

int getTitle(char *file,mp3List &listOfFiles);
fileVect getFiles(std::string directory);
string compare (mp3Names names);
string getStr (mp3Names iter);


/*****************************/
typedef struct
{
    char tag[3];
    char title[30];
    char artist[30];
    char album[30];
    char year[4];
    char comment[30];
    unsigned char genre;
} mp3Tag;





int getTitle(char *file, mp3List &listOfFiles)
{

    FILE *fp = fopen(file, "rb");

    if (!fp)
        {
            perror("File open failed");
            return 1;
        }

    mp3Tag tag;

    // Seek to 128 bytes before the end of the file
    if (fseek(fp, -1 * sizeof(mp3Tag), SEEK_END) == -1)
        {
            perror("fseek failed");
            fclose(fp);

            return 2;
        }

    // Read the tag
    if (fread(&tag, sizeof(mp3Tag), 1, fp) != 1)
        {
            fprintf(stderr, "Failed reading tag\n");
            fclose(fp);

            return 3;
        }

    // Make sure we've got what we expect.
    if (memcmp(tag.tag, "TAG", 3) == 0)
        {

            //Found the tag where we expected
            Mp3TaggedFile *mp3file=new Mp3TaggedFile(tag.title,tag.artist, tag.album,tag.year,tag.comment);
            //cout<<tag.title<<endl;
            mp3file->show();
            listOfFiles.push_back(mp3file);




        }
    else
        {
            fprintf(stderr, "Failed to find TAG\n");
            fclose(fp);
            return 4;
        }

    fclose(fp);
    return 0;
}


 
 class Mp3TaggedFile
{
public:
    Mp3TaggedFile(char title[30],char artist[30],char album[30],char year[4],char comment[30]);
    virtual ~Mp3TaggedFile();
    void show();
protected:
private:


    char *title;
    char *artist;
    char *album;
    char *year;
    char *comment;
    char *pathfile ;

Witam mam problem z vectorem. W funkcji tworze obiekt Mp3TaggedFile ktory zawiera informacji o pliku mp3. Do funkcji przekazuje vector. Gdy chce odczytac z tego vektora (poza funkcja) dostaje jakies smieci, a gdy czytam go tam gdzie uzywam

             listOfFiles.push_back(mp3file);

jest wszystko ok. Co robie zle?

0

To nie problem z vectorem.

W funkcji getTitle() tag jest obiektem lokalnym. Do mp3file przekazujesz wskaźniki do jego pól!
Po wyjściu z funkcji obiekty lokalne są likwidowane i wszelkie wskaźniki do nich przestają być ważne.

Można prościej:

typedef std::vector<mp3Tag *> mp3List;//<--- zm.

int getTitle(char *file, mp3List &listOfFiles)
{
    FILE *fp = fopen(file, "rb");
    if (!fp)
        {
            perror("File open failed");
            return 1;
        }
    mp3Tag* tag = new mp3Tag;//<--- zm.
    if (fseek(fp, -1 * sizeof(mp3Tag), SEEK_END) == -1)
        {
            perror("fseek failed");
            fclose(fp);
            return 2;
        }
    if (fread(tag, sizeof(mp3Tag), 1, fp) != 1)//<--- zm.
        {
            fprintf(stderr, "Failed reading tag\n");
            fclose(fp);
 
            return 3;
        }
    if (memcmp(tag->tag, "TAG", 3) == 0)//<--- zm.
            listOfFiles.push_back(tag);//<--- zm.
    else
        {
            fprintf(stderr, "Failed to find TAG\n");
            fclose(fp);
            return 4;
        }
    fclose(fp);
    return 0;
} 
2
typedef std::vector<Mp3TaggedFile *> mp3List;

Ten wskaźnik to proszenie się o kłopoty. Daj po prostu vector<Mp3TaggedFile>.

int getTitle(char *file,mp3List &listOfFiles);

Jakiś powód, dla którego nie string? (a jeszcze lepiej wstring, bo taki miłośnik anime może mieć pliki z krzaczkami w nazwach).

    char *title;
    char *artist;
    char *album;
    char *year;
    char *comment;
    char *pathfile ;

Jakiś powód dla którego to nie są stringi? sam stwarzasz sobie problemy…

0

n0name_l - Używanie C a może C++. Nie szalej. ToTobie. Azarien - Nie za póżno?

1 użytkowników online, w tym zalogowanych: 0, gości: 1