Pues tengo una duda, estoy empezando a programar en c y quisisera saber si existe algun componente en c que sirva para elegir un archivo, algo asi como el FileChooser de java, encontre que existe OpenFileDialog pero creo q es para c++ o c sharp y tampoco he encontrado mucha info de como ocuparlo, ojala me pudieran ayudar.
.....
Bueno pues ya estube viendo y encontre q para hacer lo q necesito tengo que utilizar el API Win 32, ya encontre algunos ejemplos y mas o menos le voy entendiendo. pero ahora necesito abrir el archivo e ir leyendo de linea en linea, pues convierto el archivo de ascci a binario, tengo otro programa aparte que lo hace con fopen fgets(..) etc, y ahora lo quiero añadir a este q elige el archivo, pero por lo que veo se tiene q usar ReadFile pero no se bien como usarlo, ojala alguien me pueda ayudar.
este es el codigo anterior lo q hace es abrir un archivo y pasarlo a binario:
Código:
y ahora quiero complementarlo con este, q lo q hace es crear una ventana y abrir el "selector" de archivos#include <stdio.h> ... FILE *archivo; FILE *nuevo; float convierteNum(char *f){ float d = atof(f); fwrite(&d, sizeof(float), 1, nuevo); } void divideLinea(char *pal){ char s2[3] = " "; char *ptr; ptr = strtok( pal, s2 ); do{ convierteNum(ptr); }while((ptr = strtok( NULL, s2 )) != NULL );//{ // Posteriores llamadas } void leeArchivo(){ char palabra[100]; char *c; archivo = fopen("AlAurho.RHO.asc", "r"); if (archivo == NULL) { printf("Error al abrir el archivo \n"); exit (EXIT_FAILURE); } c = fgets(palabra, 100, archivo); c = fgets(palabra, 100, archivo); c = fgets(palabra, 100, archivo); c = fgets(palabra, 100, archivo); do { c = fgets(palabra, 100, archivo); if (c != NULL){ divideLinea(palabra); } } while (c != NULL); fclose(archivo); } int main(){ nuevo = fopen("data.bin", "a"); leeArchivo(); fclose(nuevo); system("pause"); return 0; }
Código:
static char g_szClassName[] = "MyWindowClass"; static HINSTANCE g_hInst = NULL; #define IDC_MAIN_TEXT 1001 //en este metodo quiero hacer lo del codigo anterior, sin q se muestre en la ventana BOOL LeeArchivo(HWND hEdit, LPSTR pszFileName) { HANDLE hFile; BOOL bSuccess = FALSE; /*pszFileName es el nombre del archivo. *GENERIC_READ significa que queremos acceso de sólo lectura. *FILE_SHARE_READ significa que otros programas pueden abrir el archivo al mismo tiempo. *pero sólo si quieren leerlo, no queremos que escriban en el archivo mientras estamos leyendo. *OPEN_EXISTING significa que sólo abriremos el archivo si existe. */ hFile = CreateFile(pszFileName, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, 0); if(hFile != INVALID_HANDLE_VALUE) { DWORD dwFileSize; dwFileSize = GetFileSize(hFile, NULL); if(dwFileSize != 0xFFFFFFFF) { LPSTR pszFileText; pszFileText = (LPSTR)GlobalAlloc(GPTR, dwFileSize + 1); if(pszFileText != NULL) { DWORD dwRead; if(ReadFile(hFile, pszFileText, dwFileSize, &dwRead, NULL)) { pszFileText[dwFileSize] = 0; // Null terminator if(SetWindowText(hEdit, pszFileText)) bSuccess = TRUE; // It worked! } GlobalFree(pszFileText); } } CloseHandle(hFile); } return bSuccess; } BOOL SaveFile(HWND hEdit, LPSTR pszFileName) { HANDLE hFile; BOOL bSuccess = FALSE; hFile = CreateFile(pszFileName, GENERIC_WRITE, 0, 0, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0); if(hFile != INVALID_HANDLE_VALUE) { DWORD dwTextLength; dwTextLength = GetWindowTextLength(hEdit); if(dwTextLength > 0)// No need to bother if there's no text. { LPSTR pszText; pszText = (LPSTR)GlobalAlloc(GPTR, dwTextLength + 1); if(pszText != NULL) { if(GetWindowText(hEdit, pszText, dwTextLength + 1)) { DWORD dwWritten; if(WriteFile(hFile, pszText, dwTextLength, &dwWritten, NULL)) bSuccess = TRUE; } GlobalFree(pszText); } } CloseHandle(hFile); } return bSuccess; } BOOL DoFileOpen(HWND hwnd) { OPENFILENAME ofn; char szFileName[MAX_PATH];//tamaño del buffer que contiene el nombre del archivo ZeroMemory(&ofn, sizeof(ofn)); szFileName[0] = 0; ofn.lStructSize = sizeof(ofn); ofn.hwndOwner = hwnd; ofn.lpstrFilter = "Ascci Files (*.asc)\0*.asc\0All Files (*.*)\0*.*\0\0"; ofn.lpstrFile = szFileName; ofn.nMaxFile = MAX_PATH; //extensión por default ofn.lpstrDefExt = "asc"; //OFN_FILEMUSTEXIST solo admite archivos que ya existen //OFN_HIDEREADONLY deshabilita la opcion de solo lectura ofn.Flags = OFN_EXPLORER | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY; if(GetOpenFileName(&ofn)) { if(!LeeArchivo(GetDlgItem(hwnd, IDC_MAIN_TEXT), szFileName)) { MessageBox(hwnd, "Falla en la carga del archivo.", "Error", MB_OK | MB_ICONEXCLAMATION); return FALSE; } } return TRUE; } LRESULT CALLBACK WndProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam) { switch(Message) { case WM_CREATE: CreateWindow("EDIT", "", WS_CHILD | WS_VISIBLE | WS_HSCROLL | WS_VSCROLL | ES_MULTILINE | ES_WANTRETURN, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, hwnd, (HMENU)IDC_MAIN_TEXT, g_hInst, NULL); SendDlgItemMessage(hwnd, IDC_MAIN_TEXT, WM_SETFONT, (WPARAM)GetStockObject(DEFAULT_GUI_FONT), MAKELPARAM(TRUE, 0)); break; case WM_SIZE: if(wParam != SIZE_MINIMIZED) MoveWindow(GetDlgItem(hwnd, IDC_MAIN_TEXT), 0, 0, LOWORD(lParam), HIWORD(lParam), TRUE); break; case WM_SETFOCUS: SetFocus(GetDlgItem(hwnd, IDC_MAIN_TEXT)); break; case WM_COMMAND: switch(LOWORD(wParam)) { case CONVERTIR: DoFileOpen(hwnd); break; case RESTAR: break; case SALIR: PostMessage(hwnd, WM_CLOSE, 0, 0); break; case AYUDA: MessageBox (NULL, "File Editor for Windows !\n Using the Win32 API" , "About...", 0); } break; case WM_CLOSE: DestroyWindow(hwnd); break; case WM_DESTROY: PostQuitMessage(0); break; default: return DefWindowProc(hwnd, Message, wParam, lParam); } return 0; } int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { WNDCLASSEX WndClass; HWND hwnd; MSG Msg; g_hInst = hInstance; WndClass.cbSize = sizeof(WNDCLASSEX); WndClass.style = 0; WndClass.lpfnWndProc = WndProc; WndClass.cbClsExtra = 0; WndClass.cbWndExtra = 0; WndClass.hInstance = g_hInst; WndClass.hIcon = LoadIcon(NULL, IDI_APPLICATION); WndClass.hCursor = LoadCursor(NULL, IDC_ARROW); WndClass.hbrBackground = (HBRUSH)(COLOR_WINDOW+1); WndClass.lpszMenuName = "MAINMENU"; WndClass.lpszClassName = g_szClassName; WndClass.hIconSm = LoadIcon(NULL, IDI_APPLICATION); if(!RegisterClassEx(&WndClass)) { MessageBox(0, "Window Registration Failed!", "Error!", MB_ICONEXCLAMATION | MB_OK | MB_SYSTEMMODAL); return 0; } hwnd = CreateWindowEx( WS_EX_CLIENTEDGE, g_szClassName, "Conversor de archivos RHO", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 320, 240, NULL, NULL, g_hInst, NULL); if(hwnd == NULL) { MessageBox(0, "Fallo en la creacion de la ventana!", "Error!", MB_ICONEXCLAMATION | MB_OK | MB_SYSTEMMODAL); return 0; } ShowWindow(hwnd, nCmdShow); UpdateWindow(hwnd); while(GetMessage(&Msg, NULL, 0, 0)) { TranslateMessage(&Msg); DispatchMessage(&Msg); } return Msg.wParam; }