Ir para conteúdo
Fórum Script Brasil
  • 0

OPENFILENAME struct (win32) - problemas


Shariath Dagon

Pergunta

Boa tarde a todos!

Estou com um problema em relação a essa estrutura que cria um Common Dialolg Box de abrir/salvar arquivo. No programa de teste abaixo, estou simplesmente tentando salvar os dados de uma classe e depois lê-los.

#include <iostream>
#include <fstream>
#include <cstdlib>
#include <cstdio>
#include <windows.h>

using namespace std;

void createfile();
void savefile();
void loadfile();

class myclass {
    int a;
    int b;
    int c;
public:
    myclass(int j, int k, int l) { a=j; b=k; c=l; }; 
    int geta() { return a; };
    int getb() { return b; };
    int getc() { return c; };
};

fstream tmpFile;
char cstrFileName [100];

int main()
{
    cstrFileName[0] = '';

    bool b = false;
    int choice;

    do {
        cout << "1 - Create file\n2 - Load\n3 - Exit\n";
        cin >> choice;
        switch(choice)
        {
            case 1: { createfile(); continue; } break;
            case 2: { loadfile();    continue; } break;
            case 3: { exit(0);        continue; } break;
            default: b = true; system("cls");
        }
    } while(b);
    
    system("pause");
}

void createfile()
{
    myclass a(1, 1, 1);

    OPENFILENAME ofn    = {0};
    ofn.hInstance        = NULL;
    ofn.hwndOwner        = NULL;
    ofn.lStructSize        = sizeof(OPENFILENAME);
    ofn.lpstrFilter        = "CL List Files*.clf*";
    ofn.lpstrTitle        = "Save list";
    ofn.lpstrFile        = cstrFileName;
    ofn.nMaxFile        = sizeof(cstrFileName);
    ofn.Flags            = OFN_HIDEREADONLY | OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;


    if(GetSaveFileName(&ofn))
    {
        tmpFile.open(cstrFileName, ios::binary);
        tmpFile.write((char *) &a, sizeof(myclass));
        tmpFile.close();    
    }
}

void loadfile()
{
    myclass b(0, 0, 0);

    OPENFILENAME ofn    = {0};
    ofn.hInstance        = NULL;
    ofn.hwndOwner        = NULL;
    ofn.lStructSize        = sizeof(OPENFILENAME);
    ofn.lpstrFilter        = "CL List Files*.clf*";
    ofn.lpstrTitle        = "Open a file";
    ofn.nMaxFile        = sizeof(cstrFileName);
    ofn.Flags            = OFN_FILEMUSTEXIST            |
                        OFN_HIDEREADONLY    |
                        OFN_NONETWORKBUTTON |
                        OFN_PATHMUSTEXIST;

    if(GetOpenFileName(&ofn))
    {
        tmpFile.open(cstrFileName, ios::binary);
        tmpFile.read((char *) &b, sizeof(class myclass));
        
        cout << endl;
        cout << "A = " << b.geta() << endl;
        cout << "B = " << b.getb() << endl;
        cout << "C = " << b.getc() << endl;
        tmpFile.close();
    }
}

O código executa, não há erros de compilação, mas nenhum arquivo é criado =\

Alguém pode me dar uma luz? Já tentei de tudo e nada parece adiantar.

Bem, obrigado desde já :rolleyes:

Link para o comentário
Compartilhar em outros sites

1 resposta a esta questão

Posts Recomendados

  • 0

Shariath Dagon, veja se seu problema não está relacionado à configuração dos flags em CreateFile(). Voce usou:

ofn.Flags = OFN_HIDEREADONLY | OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;

mas, me parecem estranhos para a criação de um arquivo, que muito provavelmente não vai existir e se existir o provável é que se reescreva ele. Experimente utilizar apenas: OFN_OVERWRITEPROMPT

Veja o que consta da documentação sobre OFN_FILEMUSTEXIST (ref. msdn - OpenFile structure):

Specifies that the user can type only names of existing files in the File Name entry field. If this flag is specified and the user enters an invalid name, the dialog box procedure displays a warning in a message box. If this flag is specified, the OFN_PATHMUSTEXIST flag is also used. This flag can be used in an Open dialog box. It cannot be used with a Save As dialog box.

Abraços

Link para o comentário
Compartilhar em outros sites

Participe da discussão

Você pode postar agora e se registrar depois. Se você já tem uma conta, acesse agora para postar com sua conta.

Visitante
Responder esta pergunta...

×   Você colou conteúdo com formatação.   Remover formatação

  Apenas 75 emoticons são permitidos.

×   Seu link foi incorporado automaticamente.   Exibir como um link em vez disso

×   Seu conteúdo anterior foi restaurado.   Limpar Editor

×   Você não pode colar imagens diretamente. Carregar ou inserir imagens do URL.



  • Estatísticas dos Fóruns

    • Tópicos
      152,2k
    • Posts
      652k
×
×
  • Criar Novo...