Program wyszukujący i kopiujący plik-tym razem troche mniej lamerska wersja :-)

0

Wiem wiem niby powinienem użyć funkcji FindFirstFile i tego http://www.winapi.org/index.php?option=com_content&task=view&id=149&Itemid=32 algorytmu, ale po pierwsze jest on strasznie zawiły a po drugie nie działa (runtime error-stack overflow).

#include "stdafx.h"
#include <Windows.h>
#include <fstream>
#include <iostream>
#include <atlsecurity.h>
#include <string>
 
 
LPSTR GetMyFilePath();
 
int WINAPI wWinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,PWSTR commandLine, int nCmdShow )
{
 
        CopyFile(GetMyFilePath(),"nowyplik.txt",0);
        MessageBox(0,GetMyFilePath(),"Komunikat",0); //Sprawdzam, jaka ścieżka została zwrócona przez funkcję GetMyFilePath()
 
        return 0;
}
 
 
LPSTR GetMyFilePath()
{
        std::ifstream iFile;
        std::string sBuffer;
 
        system("dir /s /b C:\\myFile.txt 1> temp.txt");
 
        iFile.open("temp.txt");
        getline(iFile, sBuffer);
 
       MessageBox(0,const_cast<char *>(sBuffer.c_str()),0,0); // sprawdzam, czy ścieżka została poprawnie pobrana

	return const_cast<char *>(sBuffer.c_str());
}

W pierwszym MessageBox'ie jest wszystko OK, w drugim natomiast wyskakują krzaki. DLACZEGO?!

0
return const_cast<char *>(sBuffer.c_str());

Tutaj zwracasz wskaźnik do c-stringa, który po wyjściu z funkcji już nie istnieje i dostajesz jakieś "śmieci".

Ale to:

system("dir /s /b C:\\myFile.txt 1> temp.txt");
iFile.open("temp.txt");

jest straszne.

0
lukasz1235 napisał(a)
return const_cast<char *>(sBuffer.c_str());

Tutaj zwracasz wskaźnik do c-stringa, który po wyjściu z funkcji już nie istnieje i dostajesz jakieś "śmieci".

W takim razie wytłumaczcie mi jak taki program może działać :

#include <iostream>
#include <Windows.h>
#include <string>
#include <fstream>



LPSTR getHello()
{
	LPSTR greeting="Hello World!";

	MessageBox(0,greeting,"Info Inside",MB_ICONINFORMATION);

	return greeting;
}


int main(void)
{
	MessageBox(0,getHello(),"Info outside",MB_ICONINFORMATION);



	system("PAUSE");
} 

Treść obydwu MessageBox jest identyczna.

0
#include "stdafx.h"
#include <Windows.h>
#include <fstream>
#include <iostream>
#include <atlsecurity.h>
#include <string>
 
 
LPSTR GetMyFilePath();
 
int WINAPI wWinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,PWSTR commandLine, int nCmdShow )
{
 
        CopyFile(GetMyFilePath(),"nowyplik.txt",0);
        MessageBox(0,GetMyFilePath(),"Zewnątrz funkcji",0); //Sprawdzam, jaka ścieżka została zwrócona przez funkcję GetMyFilePath()
 
        return 0;
}
 
 
LPSTR GetMyFilePath()
{
        std::ifstream iFile;
        std::string sBuffer;
        
        static LPSTR finalString;

        system("dir /s /b C:\\myFile.txt 1> temp.txt");
 
        iFile.open("temp.txt");
        getline(iFile, sBuffer);
        
       finalString = const_cast<char *>(sBuffer.c_str())       

       MessageBox(0,finalString,"Wewnątrz funkcji",0); 
        
       return finalString;
}
 

MessageBox zewnątrz funkcji dalej pokazuje śmietnik, nawet jeśli zadeklaruję finalString jako zmienną globalną.

1

Jeżeli już coś ma być zmienną globalną to sBuffer.

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