LPSTR problem

This is a discussion on LPSTR problem within the C++ Programming forums, part of the General Programming Boards category; I created a method to get a file in the current directory: Code: LPSTR CApplication::GetFile(LPSTR File) { char Path[1000]; GetModuleFileName(m_hInst,Path,1000); ...

  1. #1
    Registered User
    Join Date
    Oct 2003
    Posts
    18

    LPSTR problem

    I created a method to get a file in the current directory:

    Code:
    LPSTR CApplication::GetFile(LPSTR File)
    {
    	char Path[1000];
    	GetModuleFileName(m_hInst,Path,1000);
    	for(UINT x=(UINT)strlen(Path);x>0;x--)
    		if(Path[x]=='\\')
    			break;
    
    	Path[x+1]='\0';
    
    	strcat(Path,File);
    
    	LPSTR ReturnPath=Path;
    
    	return ReturnPath;
    }
    When I request a file the method returns a string, which can be shown by a messagebox, but when I try to write it to a file I get:

    Code:
    08 11 F4 77 1E (hex)
    what am I doing wrong?

    thanks in advance,
    borre mosch

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >char Path[1000];
    Okay so far.

    >LPSTR ReturnPath=Path;
    Still okay, but very foreboding.

    >return ReturnPath;
    Aaaahh! My eyes! You're returning a pointer to a local array. When the function returns, the memory for Path is released so you can no longer reference it. Maybe something like this would fix that particular problem, but your function is still suspect:
    Code:
    LPSTR CApplication::GetFile(LPSTR File)
    {
    	char *Path = new char[1000];
    	GetModuleFileName(m_hInst,Path,1000);
    	for(UINT x=(UINT)strlen(Path);x>0;x--)
    		if(Path[x]=='\\')
    			break;
    
    	Path[x+1]='\0';
    
    	strcat(Path,File);
    
    	return Path;
    }
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    Post code for the writing to file. You may be writing the address of the pointer, not the content stored at the address of the pointer, to the file.

  4. #4
    Registered User
    Join Date
    Oct 2003
    Posts
    18
    well yes I guess I AM writing the pointer to the file, but is there any way to make the function return a string and not a pointer, other than using a global variable?
    By the way I still wonder why the messagebox function is capable of showing the message which' memory was actually delocated when the fwrite function doesn't

  5. #5
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    one way:
    pass the pointer to the array into the function. So that it is allocated in the caller

    second way: allocate dynamically, pass it out

    third way: use a string class. Make the return value of type string. It'll be copied into the return value.
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  6. #6
    Registered User
    Join Date
    Oct 2003
    Posts
    18
    I tried to pass a pointer to the variable to the function, but it didn't really work out...can anyone give an example of such a function?

  7. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >can anyone give an example of such a function?
    Code:
    void callee(char buffer[], size_t size);
    
    void
    caller()
    {
      char buffer[BUFSIZ];
    
      fill0(buffer, sizeof buffer);
      ...
    }
    
    void
    fill0(
      char   buffer[],
      size_t size
      )
    {
      for (size_t i = 0; i < size; i++) {
        buffer[i] = 0;
      }
    }
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding a problem
    By dnguyen1022 in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2009, 05:21 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 09:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 03:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 07:54 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21