I am creating a program in Dev-C++ v. 4.9.9.2 using Win32 API that loads from a file and saves to a string. The only issue is that I cannot split the string to the requested variables...

Here is what is in the file...
08:00:00 PM, 08:00:00 PM, 08:00:00 PM, 08:00:00 PM

#include <string>
   
char strStartTime[20], strFileString[120];   

HANDLE fh;
DWORD dwRead;
DWORD dwFileSize;
fh = CreateFile("Times.txt", GENERIC_READ, FILE_SHARE_READ, NULL,    OPEN_EXISTING, 0, NULL);
dwFileSize = GetFileSize(fh, NULL);
ReadFile(fh, strFileString, dwFileSize, &dwRead, NULL);
CloseHandle(fh);
      
strStartTime = strFileString.substr(0, 11);

Recommended Answers

All 4 Replies

You're mixing C-strings and the C++ string class. Here you just want plaing old C-strings, which do not have member functions associated with them, so you can't do the substr thing. Just do it in a normal old C way.

I am new to C and C++, origionally I tried declaring origionally as:

string strStartTime, strFileString;

but get errors. This is where I am confused.. What is the syntax of the old C way?

Try something like this:

#define MAXFILESIZE 120
...
char strFileString[MAXFILESIZE];
...
ReadFile(fh, strFileString, MAXFILESIZE, &dwRead, NULL);
CloseHandle(fh);

if (dwRead >= MAXFILESIZE) --dwRead; // so we don't write past the end
strFileString[dwRead] = 0; // ensure null termination of C-string

strncpy( strStartTime, strFileString, 11 );
strncpy( strWhateverTime, strFileString + 13, 11 );
// The + 13 skips the first 13 chars, the 11 reads 11 chars.
...

Thanks.. I used strncpy and it works..

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.