Home   Artículos   Recursos   Foros   
Artíclos recientes publicados en Latindevelopers:

Visual C++: NSDoubleEdit: Un control para el manejo de números decimales en Visual C++.
Visual C++: Implementando una Calculadora en Visual C++
Visual C++: CCommandLine: Una clase para el uso de la linea de comando
Visual C++: Una clase para el manejo del Registro


Asesoria y ayuda

Aqui programadores en la plataforma Win32 con Visual C++ de Microsoft...

Moderador: latindeveloper

Asesoria y ayuda

Notapor daredevil_g el Lun May 28, 2007 3:06 pm

Compañeros, vengo a ustedes en desesperacion, espero alguien me pueda ayudar, pues supongo que el problema es algo sencillo que no logro encontrar donde esta la falla, se supone que el programa (abajo pongo el codigo) debe hacer 1.- Guardar datos en un archivo datos.dat, 2.- Buscar un nombre desde el archivo datos.dat y mostrarlo en pantalla, y como ven en mi codigo ya llevo gran parte resuelto, solo que hasta aqui llego mi conocimiento si alguien me puede hechar la mano, de antemano se lo agradezco muchisimo.

Código: Seleccionar todo
// ProyectoF.cpp : Defines the entry point for the application.
//
#include "stdafx.h"
#include "stdlib.h"
#include "string.h"
#include "stdio.h"
#include "ProyectoF.h"

#define MAX_LOADSTRING 100
#define BAJA -1

///////////////////// Variables  Globales :
struct registro{
   char nombre [40];
   char apellidos [30];
   char calle [30];
   char colonia[20];
   char ciudad[40];
   char estado [15];
   char cp [6];
}reg;



char cadena [15];
void escribe_reg(registro reg, FILE *fp);
registro *lee_reg(FILE *globfp);
void muestra_reg(registro reg);
FILE *arch;
registro *agenda;
registro ax;
registro direccion;
int i;
void Accion_Buscar(char *cadena, FILE *arch);
int BuscaEn(char *buscada, char *cadena);
HDC manejador;
char *dato;
int opcion = 0;




HINSTANCE hInst;                        // current instance
TCHAR szTitle[MAX_LOADSTRING];               // The title bar text
TCHAR szWindowClass[MAX_LOADSTRING];         // the main window class name

// Forward declarations of functions included in this code module:
ATOM            MyRegisterClass(HINSTANCE hInstance);
BOOL            InitInstance(HINSTANCE, int);
LRESULT CALLBACK   WndProc(HWND, UINT, WPARAM, LPARAM);
INT_PTR CALLBACK   About(HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK   Agregar(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam);
LRESULT CALLBACK   Buscar(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam);
LRESULT CALLBACK    ResultadoB(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam);

int APIENTRY _tWinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPTSTR    lpCmdLine,
                     int       nCmdShow)
{
   UNREFERENCED_PARAMETER(hPrevInstance);
   UNREFERENCED_PARAMETER(lpCmdLine);

   // TODO: Place code here.
   MSG msg;
   HACCEL hAccelTable;
   // Initialize global strings
   LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
   LoadString(hInstance, IDC_PROYECTOF, szWindowClass, MAX_LOADSTRING);
   MyRegisterClass(hInstance);

   // Perform application initialization:
   if (!InitInstance (hInstance, nCmdShow))
   {
      return FALSE;
   }

   hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_PROYECTOF));

   // Main message loop:
   while (GetMessage(&msg, NULL, 0, 0))
   {
      if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
      {
         TranslateMessage(&msg);
         DispatchMessage(&msg);
      }
   }

   return (int) msg.wParam;
}



//
//  FUNCTION: MyRegisterClass()
//
//  PURPOSE: Registers the window class.
//
//  COMMENTS:
//
//    This function and its usage are only necessary if you want this code
//    to be compatible with Win32 systems prior to the 'RegisterClassEx'
//    function that was added to Windows 95. It is important to call this function
//    so that the application will get 'well formed' small icons associated
//    with it.
//
ATOM MyRegisterClass(HINSTANCE hInstance)
{
   WNDCLASSEX wcex;

   wcex.cbSize = sizeof(WNDCLASSEX);

   wcex.style         = CS_HREDRAW | CS_VREDRAW;
   wcex.lpfnWndProc   = WndProc;
   wcex.cbClsExtra      = 0;
   wcex.cbWndExtra      = 0;
   wcex.hInstance      = hInstance;
   wcex.hIcon         = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_PROYECTOF));
   wcex.hCursor      = LoadCursor(NULL, IDC_ARROW);
   wcex.hbrBackground   = (HBRUSH)(COLOR_WINDOW+1);
   wcex.lpszMenuName   = MAKEINTRESOURCE(IDC_PROYECTOF);
   wcex.lpszClassName   = szWindowClass;
   wcex.hIconSm      = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));

   return RegisterClassEx(&wcex);
}

//
//   FUNCTION: InitInstance(HINSTANCE, int)
//
//   PURPOSE: Saves instance handle and creates main window
//
//   COMMENTS:
//
//        In this function, we save the instance handle in a global variable and
//        create and display the main program window.
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
   HWND hWnd;

   hInst = hInstance; // Store instance handle in our global variable

   hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
      CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);

   if (!hWnd)
   {
      return FALSE;
   }

   ShowWindow(hWnd, nCmdShow);
   UpdateWindow(hWnd);

   return TRUE;
}

//
//  FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM)
//
//  PURPOSE:  Processes messages for the main window.
//
//  WM_COMMAND   - process the application menu
//  WM_PAINT   - Paint the main window
//  WM_DESTROY   - post a quit message and return
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
   int wmId, wmEvent;
   PAINTSTRUCT ps;
   HDC hdc;


   arch=fopen("datos.dat","r+b");
   if(arch==NULL)
   {
      arch=fopen("datos.dat","w+b");
      
   }


   switch (message)
   {
   case WM_COMMAND:
      wmId    = LOWORD(wParam);
      wmEvent = HIWORD(wParam);
      // Parse the menu selections:
      switch (wmId)
      {
      case IDD_AGREGAR:
         DialogBox(hInst, (LPCTSTR) IDD_AGREGAR, hWnd, (DLGPROC) Agregar);
         InvalidateRect(hWnd, NULL, TRUE);
         UpdateWindow(hWnd);
         break;
      case ID_FILE_BUSCARTRABAJADOR:
         DialogBox(hInst, (LPCTSTR) IDD_BUSQUEDA, hWnd, (DLGPROC) Buscar);
         InvalidateRect(hWnd, NULL, TRUE);
         UpdateWindow(hWnd);
         opcion=10;
         break;
      case IDM_ABOUT:
         DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
         break;
      case IDM_EXIT:
         MessageBox(hWnd,"Gracias por utilizar el sistema", NULL, MB_ICONEXCLAMATION|MB_OK);
         DestroyWindow(hWnd);
         break;
      default:
         dato = 0;
         return DefWindowProc(hWnd, message, wParam, lParam);
      }
      break;
   case WM_PAINT:
      hdc = BeginPaint(hWnd, &ps);
      // TODO: Add any drawing code here...
      manejador = hdc;         
      switch(opcion){
   case 10:
          TextOutA(hdc, 60, 60, dato, strlen(dato));
      EndPaint(hWnd, &ps);
      break;
   case WM_DESTROY:
      PostQuitMessage(0);
      break;
   default:
      return DefWindowProc(hWnd, message, wParam, lParam);
   }
   return 0;
}

// ---------------------------MANEJADOR DE MENSAJE PARA LA CAJA ACERCA DE ...
INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
   UNREFERENCED_PARAMETER(lParam);
   switch (message)
   {
   case WM_INITDIALOG:
      return (INT_PTR)TRUE;

   case WM_COMMAND:
      if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
      {
         EndDialog(hDlg, LOWORD(wParam));
         return (INT_PTR)TRUE;
      }
      break;
   }
   return (INT_PTR)FALSE;
}
// ********************* FUNCION PARA AGREGAR TRABAJADORES *****************
LRESULT CALLBACK Agregar(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
   switch(message){
      case WM_INITDIALOG:
         return false;
      case WM_COMMAND:
         switch(wParam)
         {
         case IDOK:
            GetDlgItemTextA(hDlg, IDC_EDIT1, reg.nombre, 40);
            GetDlgItemTextA(hDlg, IDC_EDIT2, reg.apellidos, 30);
            GetDlgItemTextA(hDlg, IDC_EDIT3, reg.calle, 30);
            GetDlgItemTextA(hDlg, IDC_EDIT4, reg.colonia, 20);
            GetDlgItemTextA(hDlg, IDC_EDIT5, reg.ciudad, 40);
            GetDlgItemTextA(hDlg, IDC_EDIT6, reg.estado, 15);
            GetDlgItemTextA(hDlg, IDC_EDIT7, reg.cp, 6);
            EndDialog(hDlg, TRUE);
            escribe_registro(reg, arch);
            break;
         case IDCANCEL:
            EndDialog(hDlg, TRUE);
            break;
         default:
            return FALSE;
         }
         break;
      default:
         return FALSE;
   }
   return TRUE;
}
//****************** FIN DE LA FUNCION PARA AGREGAR TRABAJADORES******


// ****************************** FUNCION PARA BUSCAR TRABAJADORES ********
LRESULT CALLBACK Buscar(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
   switch(message){
      case WM_INITDIALOG:
         return false;
      case WM_COMMAND:
         switch(wParam)
         {
         case IDOKNOMBRE:
            GetDlgItemTextA(hDlg, IDC_EDIT1, cadena, 15);
            dato=Accion_Buscar(cadena,arch);
            break;
         case IDOKAPELLIDO:
            GetDlgItemTextA(hDlg, IDC_EDIT2, cadena, 15);
            Accion_Buscar(cadena,arch);
            break;
         case IDCANCEL:
            EndDialog(hDlg, TRUE);
            break;
         default:
            return FALSE;
         }
         break;
      default:
         return FALSE;
   }
   return TRUE;
}
// ****************** FIN DE LA FUNCION PARA BUSCAR TRABAJADORES *********




void escribe_reg(registro reg, FILE *fp)
{
   fwrite(&reg, sizeof(reg),1,fp);
}



registro *lee_reg(FILE *fp)
{
   registro tmp;
   fread(&tmp, sizeof(tmp),1,fp);
   return &tmp;
}



void muestra_reg(registro reg)
{
   printf("\n%s",reg.nombre);
   printf("\n%s",reg.calle);
   printf("\n%s",reg.ciudad);
   printf("\n%s",reg.estado);
   printf("\n%s",reg.cp);
}

// *********/////////////***********----------- DECLARACION DE FUNCIONES PARA LA BUSQUEDA

void Accion_Buscar(char *cadena, FILE *arch)
{
   registro reg;
   int regreso;
   do{
      fread(&reg, sizeof(reg), 1,  arch);
      regreso = BuscaEn(cadena, reg.nombre);
      if(regreso==1) break;
      if(feof(arch)) break;
   }while (regreso != 1);
   if (regreso == 1)
      return reg.nombre;
   else
      return 0;
}
int BuscaEn(char *buscada, char *cadena)
{
   int i;
   int j=0;
   int bandera = 100;
   for (i=0; i<=strlen(cadena); i++)
   {
      if(cadena[i]==buscada[0])
         for(j=0; j<strlen(buscada); j++)
         {
            if(buscada[j] == cadena[i+j])
               bandera = true;
            else bandera = false;
         }
         if((j == strlen(buscada)) && (bandera == 1))
            return bandera;
   }
   return bandera;
}

// *************-------------------- FUNCION PARA IMPRIMIR LOS RESULTADOS DE BUSQUEDA EN UNA DIALOG BOX

LRESULT CALLBACK ResultadoB(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
   switch(message){
      case WM_INITDIALOG:
         return false;
      case WM_COMMAND:
         switch(wParam)
         {
         case IDOKNOMBRE:
            GetDlgItemTextA(hDlg, IDC_EDIT1, cadena, 15);
            Accion_Buscar(cadena,arch);
            break;
         case IDOKAPELLIDO:
            GetDlgItemTextA(hDlg, IDC_EDIT2, cadena, 15);
            EndDialog(hDlg, TRUE);
            break;
         case IDCANCEL:
            EndDialog(hDlg, TRUE);
            break;
         default:
            return FALSE;
         }
         break;
      default:
         return FALSE;
   }
   return TRUE;
}
daredevil_g
Novato
Novato
 
Mensajes: 2
Registrado: Lun May 28, 2007 1:56 pm

Recomendacion

Notapor oscargim el Mié May 30, 2007 12:10 pm

Te recomiendo que seas mas especifico en tus preguntas, no creo que alguien haga el trabajo por vos.
Talves te lleve mas tiempo pero hace tus preguntas concretas y te aseguro que mas de uno te va a querer ayudar, pero una cosa es ayudar otra cosa es que hagan el trabajo por vos.
Suerte....
oscargim
Usuario Muy Activo
Usuario Muy Activo
 
Mensajes: 113
Registrado: Mar May 09, 2006 5:53 am

Perdon

Notapor daredevil_g el Dom Jun 03, 2007 11:05 pm

Je, sorry, si se me paso, lo que pasa es que necesito aprender, no necesariamente que me expliquen en el ejemplo, como imprimir en pantalla el resultado de por ejemplo 2+2, en modo consola es facil, pero en modo grafico como debo hacer eso ? y todavia mejor aun, desde una funcion que regresa una cadena de texto, ¿como puedo imprimir la cadena que regresa?, espero haber sido mas especifico, y una disculpa, nunca pretendi que me hicieran la tarea :(
daredevil_g
Novato
Novato
 
Mensajes: 2
Registrado: Lun May 28, 2007 1:56 pm


Volver a Visual C++

¿Quién está conectado?

Usuarios navegando por este Foro: No hay usuarios registrados visitando el Foro y 0 invitados