Afficher la xeme ligne d'un texte

Description

Il s'agit simplement de choisir le path d'un fichier text, puis de dire quelle ligne on veut voir et cette dernière sera affichée dans un MessageBox! Utile pour traîter le contenu de fichiers...

Source / Exemple :


#include <windows.h>

#include "resource.h"

HINSTANCE hInst;
HWND hWnd;

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow);
BOOL CALLBACK myWindow(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
void OnBrowse(HWND hParent,BOOL bOpen, int idc_name);
BOOL GetLine(HWND hWnd);

void OnBrowse(HWND hParent,BOOL bOpen,int idc_name){
	OPENFILENAME sOpenFile;
	char szPath[MAX_PATH + 1];
	BOOL bRet;
	HWND hCtrl;

	szPath[0] = '\0';
	
	ZeroMemory(&sOpenFile,sizeof(OPENFILENAME));
	sOpenFile.lStructSize = sizeof(OPENFILENAME);
	sOpenFile.hwndOwner = hParent;
	sOpenFile.lpstrFilter = "Fichiers Text (*.txt)\0*.txt\0\0";
	sOpenFile.nFilterIndex = 1;
	sOpenFile.lpstrFile = szPath;
	sOpenFile.nMaxFile = MAX_PATH + 1;
	sOpenFile.Flags = OFN_EXPLORER | OFN_PATHMUSTEXIST | 
		((bOpen)? OFN_FILEMUSTEXIST : OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT);

	if(bOpen)
	{
		hCtrl = GetDlgItem(hParent,idc_name);
		bRet = GetOpenFileName(&sOpenFile);
		SetWindowText(hCtrl,szPath);
	}
}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
	return (int)DialogBox(hInstance,MAKEINTRESOURCE(IDD_DIALOG1),NULL,myWindow);
}

BOOL CALLBACK myWindow(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam){

	switch(message) {

	case WM_INITDIALOG:
			return TRUE;

	case WM_COMMAND:
		switch(wParam) {
	
			case IDC_BUTTON1:
				OnBrowse(hWnd,TRUE, IDC_EDIT1);
				return TRUE;

			case IDC_BUTTON2:
				DestroyWindow(hWnd);
				return TRUE;

			case IDC_EXECUTE:
				GetLine(hWnd);
				return TRUE;

			default:
				return FALSE;
		}
	default:
		return FALSE;

	}

	return TRUE;

}

BOOL GetLine(HWND hWnd){

	int i,nLen,iLine;
	HWND hCtrl;
	DWORD hSrcFile,dw,dwFileSize;
	char* szData = NULL;
	char* szSrc = NULL;
	char* d;

	hCtrl = GetDlgItem(hWnd,IDC_EDIT1);
	nLen = GetWindowTextLength(hCtrl) + 1;

	szSrc = (char*)malloc(nLen);

	if (GetWindowText(hCtrl,szSrc,nLen) == 0){ 
		free(szSrc); 
		MessageBox(hWnd,"Veuillez choisir un fichier","Fichier",MB_OK);
		return FALSE;}
	
	hSrcFile = CreateFile(szSrc,GENERIC_READ,FILE_SHARE_READ,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);

	if(hSrcFile == INVALID_HANDLE_VALUE)
	{
		free(szSrc);
		return FALSE;
	}
	free(szSrc);

	dwFileSize = GetFileSize(hSrcFile,NULL)+1;
	szData = (char*)malloc(dwFileSize);

	if(!ReadFile(hSrcFile,szData,dwFileSize,&dw,NULL))
	{
		free(szData);
		CloseHandle(hSrcFile);
		return FALSE;
	}
	CloseHandle(hSrcFile);

  • (szData + dwFileSize) = '\0';
d = szData; i = (*d ? 1 : 0); while(*d){ switch(*d){ case '\n':
  • d='\0';
d++; i++; break; default: d++; break; } } d=NULL; hCtrl = GetDlgItem(hWnd,IDC_EDIT2); nLen = GetWindowTextLength(hCtrl) + 1; szSrc = (char*)malloc(nLen); GetWindowText(hCtrl,szSrc,nLen); iLine = atoi(szSrc)-1; free(szSrc); if (iLine<0){ MessageBox(NULL, "Veuillez choisir un nombre positif!", "Erreur", MB_ICONERROR); return FALSE;} if (iLine >= i) { MessageBox(NULL, "Ce fichier n'a pas autant de lignes!", "Erreur", MB_ICONERROR); return FALSE;} while(iLine--) szData += strlen(szData) + 1; MessageBox(hWnd, szData,"la ligne demandée est...",NULL); return TRUE; }

Codes Sources

A voir également

Vous n'êtes pas encore membre ?

inscrivez-vous, c'est gratuit et ça prend moins d'une minute !

Les membres obtiennent plus de réponses que les utilisateurs anonymes.

Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.

Le fait d'être membre vous permet d'avoir des options supplémentaires.