I'm trying to get a players name from a file on their computer and then display that name onto a Label on my chat program. This Label is also used when sending messages to the server in order to display the persons name with their message.

The problem I'm having is that when I read the file and display the contents its adding random symbols to the contents and taking away parts of the word...

Here is the code I'm using...

// Player's Name
HANDLE hFile; 
DWORD wmWritten; 
char strName[20]; 
hFile = CreateFile("C:\\ZacarasEmpire-Offline\\Data\\File01.ze",GENERIC_READ,
FILE_SHARE_READ,NULL,OPEN_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL); 
int NameSize = sizeof(hFile);
ReadFile(hFile,strName,NameSize,&wmWritten,NULL);
CloseHandle(hFile); 
         
       hEditName = CreateWindow(TEXT("STATIC"), strName, 
        		    WS_CHILD | WS_VISIBLE | SS_LEFT,
        		    40, 190, 100, 23,
        		    hWnd, (HMENU) IDC_EDIT_NAME, GetModuleHandle(NULL), NULL);

So this turns strName into whatever is written in File01.ze (which is the players name) and then displays strName into the Name label on the screen.

File01.ze currently has just the word Ertzel written in it and nothing else. This is what I need displayed. If you look at the attached picture, thats what is being displayed instead of the word Ertzel.

I can't figure out why its not displaying the correct text onto the label....

Recommended Answers

All 6 Replies

ReadFile() does not null-terminate strings -- it treats the file as if it were a binary file. Instead of CreteFile() and ReadFile() why don't you just use standard C++ fstreams. Just because you are writing a win32 api GUI program doesn't mean you can't use c++ fstream or other STL containers.

>>line 7: int NameSize = sizeof(hFile);
Huh? That will not give you the size of the strName(), or even the size of the file.

I also tried this but it also adds a random character to the end of the name. This code only adds one thing to the end and does display the full Players Name unlike the last one but I still need to get rid of the random thing being added to the end of the name.

// Player's Name
  ifstream CharName;
  CharName.open ("C:\\ZacarasEmpire-Offline\\Data\\File01.ze");
  
  CharName.seekg (0, ios::end);
  Namelength = CharName.tellg();
  CharName.seekg (0, ios::beg);
  
  Namebuffer = new char [Namelength];

  CharName.read (Namebuffer,Namelength);
  CharName.close();

  cout.write (Namebuffer,Namelength);
         
       hEditName = CreateWindow(TEXT("STATIC"), Namebuffer, 
        		    WS_CHILD | WS_VISIBLE | SS_LEFT,
        		    40, 190, 100, 23,
        		    hWnd, (HMENU) IDC_EDIT_NAME, GetModuleHandle(NULL), NULL); 
        		    
delete[] Namebuffer;

Again, the character buffer is not null-terminated. Why not just do CharName >> NameBuffer; or CharName.getline(NameBuffer,NameLength); Better will, declare NameBuffer as std::string instead of char*

Thank you, replacing the old code with this one below solved the problem and now it displays the correct text from the file.

// Player's Name
  ifstream CharName;
  CharName.open ("C:\\ZacarasEmpire-Offline\\Data\\File01.ze");
  
  CharName.seekg (0, ios::end);
  Namelength = CharName.tellg();
  CharName.seekg (0, ios::beg);
  
  Namebuffer = new char [Namelength];

  CharName >> Namebuffer;
  CharName.close();
         
       hEditName = CreateWindow(TEXT("STATIC"), Namebuffer, 
        		    WS_CHILD | WS_VISIBLE | SS_LEFT,
        		    40, 190, 100, 23,
        		    hWnd, (HMENU) IDC_EDIT_NAME, GetModuleHandle(NULL), NULL); 
        		    
delete[] Namebuffer;

line 11. There's the easy way to code it. Notice it does not do any seeks() or memory allocations because std::string will do all memory allocations for you.

// Player's Name
  std::string name;
  ifstream CharName("C:\\ZacarasEmpire-Offline\\Data\\File01.ze");
  if( CharName.is_open())
  {  
      CharName >> name;
      CharName.close();

      cout << name;         
      hEditName = CreateWindow(TEXT("STATIC"), name.c_str(), 
        		    WS_CHILD | WS_VISIBLE | SS_LEFT,
        		    40, 190, 100, 23,
        		    hWnd, (HMENU) IDC_EDIT_NAME, GetModuleHandle(NULL), NULL); 
  }

Thanks, that also works and only uses 14 lines instead of 19. Everything is working now with the Names and I'll be able to use this code to get other Player data from files to the server now.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.