|
Open the file with OPEN_ALWAYS and check the error code to know if it has been created or an existing file has been opened:
SetLastError(0);
HANDLE hFile = CreateFile(fileName, GENERIC_READ|GENERIC_WRITE, 0, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
if (hFile != INVALID_HANDLE_VALUE)
{
if (GetLastError() == ERROR_ALREADY_EXISTS)
{
}
else
{
}
CloseHandle(hFile);
}
Alternatively check if the file exists and open it for reading or writing afterwards. This can be optimised by trying to open the file for reading and if that fails to open it for writing:
HANDLE hFile = CreateFile(fileName, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (hFile != INVALID_HANDLE_VALUE)
{
}
else
{
hFile = CreateFile(fileName, GENERIC_WRITE, 0, NULL, CREATE_NEW, FILE_ATTRIBUTE_NORMAL, NULL);
if (hFile != INVALID_HANDLE_VALUE)
{
}
}
if (hFile != INVALID_HANDLE_VALUE)
CloseHandle(hFile);
|
|
|
|
|
Hello,
Can somebody please give me an example of _tcslwr function used with BYTE array as input?
Thanks in advance.
|
|
|
|
|
This depends on the content stored in the byte array. Because the function expects a NULL terminated string, the byte array must contain such a string. You must also know the text encoding for the stored string.
Assuming your byte array contains an ASCII or ANSI string, you can call _strlwr :
_strlwr((char *)byteArray);
The casting to char * avoids compiler warnings or errors because BYTE is unsigned (unsigned char ).
If the resulting string must be printed out later with Unicode builds, convert it using the MultiByteToWideChar function (Windows)[^]. If it is ASCII or uses the current code page of the user running the application, it can be also converting by assigning the lowered string to a CString :
CString strLwr = _strlwr((char *)byteArray);
The above will work for Unicode and multi byte builds (with Unicode builds, the string is converted to Unicode).
|
|
|
|
|
Hi All , I have the below declaration in say abc.h and implementation of the member function in abc.cpp in Project A. How can I call/access some of this member functions in project B and Project C without including the A.lib in Project B and Project C?
As is I am getting linker error from project B and C
class __declspec(dllimport) B
{
public:
B();
static B* Ptr1();
static int fn1(D* ptr2);
D * fn2();
private:
static int Result;
static map <string, d*=""> map1;
};
class __declspec(dllimport) C
{
public:
C();
static C* Ptr1();
static int Fn4(const string& str1);
static int Fn6(const string &str3, B* ptr4);
static B* Fn7(string &str4);
static bool isBoolEnabled() { return Boolean;}
static void SetBoolean(bool bArg) {Boolean = bArg; }
private:
static map <string, b*=""> map3 ;
static map <int, string=""> map4;
static bool Boolean;
};
class __declspec(dllimport) D
{
vector <string> Token1;
vector <string> Token2;
vector <string> Token3;
}
|
|
|
|
|
Assuming that you are using Windows and that the code to be called is in a DLL, look at the LoadLibrary () and the GetProcAddress () APIs.
If you have an important point to make, don't try to be subtle or clever. Use a pile driver. Hit the point once. Then come back and hit it again. Then hit it a third time - a tremendous whack.
--Winston Churchill
|
|
|
|
|
Thank you Daniel for the thoughts.. I am exploring other option due to the order I build the .lib gives linker error...if other option does not pan out I will go with your suggestion.. This was helpful..!
|
|
|
|
|
You have declared the classes as dllimport so you must add the .lib file to your linker section in your build.
|
|
|
|
|
Thank you Richard !.. The order I am compiling the different projects(.lib) cannot be changed. First project referencing member functions from other project that was not even compiled..so other than getprocaddress() , no better option ?
|
|
|
|
|
That makes no sense at all. Build the lib project first or waste your time.
|
|
|
|
|
I think he is trying to tell you he has got a circular dependancy problem.
In vino veritas
|
|
|
|
|
How to solve circular dependency in linker is shown here
Library order in static linking - Eli Bendersky's website[^]
The trick is to organize the library files as shown and link them twice which is perfectly valid.
I assume that is the reason you are trying to avoid including the library file.
In vino veritas
|
|
|
|
|
I need to open a file for both reading/writing using fstream and read each character then write that character back to the file. for example i have this code.
fstream in("test.txt",ios::in | ios::out);
if(!in)
cout<<"error...";
else
{
char ch;
in.seekg(0,ios::end);
int end=in.tellg();
in.seekg(0); for(int i=0;i<end;i++)
{
if(!in.get(ch).fail()) {
in.seekp(static_cast<int>(in.tellg())-1); if(in.put(ch).fail()) break; }
}
}
I have a file named "test.txt" which contains "ABCD". As i understand it both put() and get() methods of the stream object move the file pointer forward(i see that by getting the return value of tellg() or tellp() functions after each get() or put() method call). My question is when i comment out the code that will seek the stream pointer to "where it is now"(in.seekg(in.tellg()), the code will result incorrect results. I don't understand why this is since tellg() is showing the correct position of the character to be read next what is the purpose of explicitly seeking to it? The incorrect result is it write to the file "ABBB" instead of "ABCD". I am using visual studio 2005.
modified 3-Apr-16 16:26pm.
|
|
|
|
|
Read the documentation
The seekp method changes the location of a stream object's file pointer for output (put or write.) In most cases, seekp also changes the location of a stream object's file pointer for input (get or read.)
See that last bit of the statement. Your code assumes you are independently moving the get and put pointers. The implementation in linux depends also on stream type and I am not sure in VS2005 so read the documentation carefully.
In later versions of Visual Studio they use different terms
seekg => The absolute position in which to move the read pointer.
seekp => The position in the stream (meaning both read and write)
You also need a stream clear() after any seekp otherwise a seek to last character of a file will cause an error .. ATE flag gets set
In vino veritas
modified 3-Apr-16 13:36pm.
|
|
|
|
|
Thanks for your replay. But the code doesn't assume the input and output pointers are independently moved. In fact,in this case, the put() method will move the input pointer and the get() method move the output pointer as it is evident by looking at the values returned from tellp() and tellg(). This is why i am confused. if the put() statement in the code moves the input file pointer to the next character( which will be read by the next iteration) why do i need to seek to it explicitly is the question.
|
|
|
|
|
First you have the input and output pointers mixed in the above
Second the put function doesn't work the way you think in the above
MSDN => The member function put (which writes a single character) is called, the character is not written directly to the physical file with which the stream is associated. Instead of that, the character is inserted in that stream's intermediate buffer.
Put in.sync() after the put
MSDN => If the streams are synchronized, a program can mix iostream operations with stdio operations, and their observable effects are guaranteed to follow the same order as used in the program.
I think you need to do some more reading on streams with the sync it will do what you expect.
In vino veritas
|
|
|
|
|
|
Happy New Month to you all.
However, l am playing with Createfile, WriteFile and ReadFile functions, Createfile and Writefile works well but my problem is ReadFile.
l want to read the data written to a buffer "szBuffer", by using "ReadFile" to read the data in the szBuffer into another buffer szBuf. But it doesn't work.
When l check the return value like error=ReadFile(hfile,szBuffer,10,&in,NULL), it returns 0 but when l use GetLastError() to check for the return value, it gives me 998. l learnt that the second parameter to ReadFile() is the address to store the data read.
Code snippet below:
<pre
lang="c++">hfile=CreateFile("C:\file.txt",GENERIC_READ|GENERIC_WRITE,FILE_SHARE_READ|FILE_SHARE_WRITE,NULL,CREATE_NEW,FILE_ATTRIBUTE_NORMAL,NULL);// this works well
<pre>
WriteFile(hfile,szBuffer,256,&in,NULL);//this works well
ReadFile(hfile,szBuf,256,&in,NULL);//this does not read data
CloseHandle(hfile);
hdc=GetDC(hwnd);
TextOut(hdc,50,300,szerror,wsprintf(szerror,"%i",GetLastError()));//check return value
ReleaseDC(hwnd,hdc);</pre>
Can someone help me to overcome this?
Thanks.
modified 4-Apr-16 9:59am.
|
|
|
|
|
After writing to the file, the file pointer is at the actual end of the file when the file has been created. If you then try to read from the file, there is nothing to read at that position (the end of the file).
If you want to read the data that has just been written, you must set the file pointer using SetFilePointer function (Windows)[^]:
SetFilePointer(hFile, 0, 0, FILE_BEGIN);
A return value of zero (FALSE ) indicates that the function failed (see ReadFile function (Windows)[^]). Error code 998 is ERROR_NOACCESS / " Invalid access to memory location."
|
|
|
|
|
Thanks. But l have made necessary corrections but the ReadFile is still not working and GetLasterror still returns 998. Also, direct check of ReadFile also returns 0. Help please.
<blockquote class="quote"><div class="op">Quote:</div>hfile=CreateFile("C:\\file.txt",GENERIC_READ|GENERIC_WRITE,FILE_SHARE_READ|FILE_SHARE_WRITE,NULL,CREATE_NEW,FILE_ATTRIBUTE_NORMAL,NULL);
<pre>
WriteFile(hfile,szBuffer,256,&in,NULL); SetFilePointer(hfile,0,0,FILE_BEGIN);
ReadFile(hfile,szBuf,256,&in,NULL); CloseHandle(hfile);
hdc=GetDC(hwnd);
TextOut(hdc,50,300,szerror,wsprintf(szerror,"%i",GetLastError())); ReleaseDC(hwnd,hdc);</blockquote></pre>
|
|
|
|
|
You should check the return value from all system calls to see whether the function succeeded or failed. Without that information you are just guessing.
|
|
|
|
|
A probable error source is szBuf being NULL .
|
|
|
|
|
Use double slashes in your file name.
ReadFile() will read from the file pointer's current position. Perhaps you need to call SetFilePointer(FILE_BEGIN) in between write and read.
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles
|
|
|
|
|
I made following program to use opencl library supplied with Nvidia cuda 7.5.
As result I obtained:
Num of devices=1
Device name= quadro K600
Device vendor=NVIDIA Corporation
Device Version=OPENCL 1.2 CUDA
Driver Version= 353.90
DEVICE_MAX_COMPUTE_UNITS = 1 !!!!!!!!!!!!!!!!!!!!!!!!!!
DEVICE_MAX_CLOCK_FREQUENCY = 875 MHZ
DEVICE_GLOBAL_MEM_SIZE= 1073741824
Only 1 compute unit at 875 Mhz????
I read that K600 has 192
#include <stdlib.h>
#define __CL_ENABLE_EXCEPTIONS
#pragma warning(disable:4996)
#include <CL/cl.hpp>
#include <fstream>
#include <iostream>
#include <time.h>
#include <cmath>
#define LOCAL_SIZE 512
#define WIDTH_A (4096*2)
#define HEIGHT_A (4096*2)
float *matrix_A;
float *vector_B;
float *result_vector;
float *result_vector_host;
#define MAX_SOURCE_SIZE (0x100000)
int main()
{
cl_platform_id platform_id = NULL;
cl_device_id device_id = NULL;
cl_context context = NULL;
cl_command_queue command_queue = NULL;
cl_mem Amobj = NULL;
cl_mem Bmobj = NULL;
cl_mem Cmobj = NULL;
cl_program program = NULL;
cl_kernel kernel = NULL;
cl_uint ret_num_devices;
cl_uint ret_num_platforms;
cl_int ret;
int i, j;
ret = clGetPlatformIDs(1, &platform_id, &ret_num_platforms);
ret = clGetDeviceIDs(platform_id, CL_DEVICE_TYPE_DEFAULT, 1, &device_id, &ret_num_devices);
context = clCreateContext(NULL, 1, &device_id, NULL, NULL, &ret);
char buffer[10240];
cl_uint buf_uint;
cl_ulong buf_ulong;
printf("Num of devices=%i", ret_num_devices);
for (i = 0; i < ret_num_devices; i++)
{
printf(" -- %d --\n", i);
clGetDeviceInfo(device_id, CL_DEVICE_NAME, sizeof(buffer), buffer, NULL);
printf(" DEVICE_NAME = %s\n", buffer);
clGetDeviceInfo(device_id, CL_DEVICE_VENDOR, sizeof(buffer), buffer, NULL);
printf(" DEVICE_VENDOR = %s\n", buffer);
clGetDeviceInfo(device_id, CL_DEVICE_VERSION, sizeof(buffer), buffer, NULL);
printf(" DEVICE_VERSION = %s\n", buffer);
clGetDeviceInfo(device_id, CL_DRIVER_VERSION, sizeof(buffer), buffer, NULL);
printf(" DRIVER_VERSION = %s\n", buffer);
clGetDeviceInfo(device_id, CL_DEVICE_MAX_COMPUTE_UNITS, sizeof(buf_uint), &buf_uint, NULL);
printf(" DEVICE_MAX_COMPUTE_UNITS = %u\n", (unsigned int)buf_uint);
clGetDeviceInfo(device_id, CL_DEVICE_MAX_CLOCK_FREQUENCY, sizeof(buf_uint), &buf_uint, NULL);
printf(" DEVICE_MAX_CLOCK_FREQUENCY = %u\n", (unsigned int)buf_uint);
clGetDeviceInfo(device_id, CL_DEVICE_GLOBAL_MEM_SIZE, sizeof(buf_ulong), &buf_ulong, NULL);
printf(" DEVICE_GLOBAL_MEM_SIZE = %llu\n", (unsigned long long)buf_ulong);
}
}
modified 31-Mar-16 11:32am.
|
|
|
|
|
How is this:
printf(" DEVICE_MAX_CLOCK_FREQUENCY = %u\n", (unsigned int)buf_uint);
printing this:
DEVICE_MAX_CLOCK_FREQUENCY = 875 MHZ
Perhaps the code you are using and the code you pasted are not the same?
Have you tried asking your question here?
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles
|
|
|
|
|
I edited it after reading to make it more easy to understand.
I have seen that the old video card is not very good improving the performance of the CPU.
|
|
|
|
|