From: B.S on
the sizeof(programa) works ideal, it is returning 256, and i always
used TCHAR and it always worcked in writing file but i always used
CREATE_ALWAYS not OPEN_EXISTING for writing in file.

here is my project :

http://www.daniweb.com/forums/attachment.php?attachmentid=12299&stc=1&d=1256490726
From: Friedel Jantzen on
Am Sun, 25 Oct 2009 10:15:41 -0700 (PDT) schrieb B.S:

> the sizeof(programa) works ideal, it is returning 256, and i always
> used TCHAR and it always worcked in writing file but i always used
> CREATE_ALWAYS not OPEN_EXISTING for writing in file.
>
> here is my project :
>
> http://www.daniweb.com/forums/attachment.php?attachmentid=12299&stc=1&d=1256490726

Following this link I get:
"Invalid Attachment specified. If you followed a valid link, please notify
the administrator"

Quoting the code from your posting on 2009-10-23:
<cite>
HANDLE hfile;
DWORD din;

TCHAR *programa[256]="some text";
TCHAR KEY[2];
hfile=CreateFile("file.txt",GENERIC_WRITE,
0,0,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,0);
int size=GetFileSize(hfile,0);

SetFilePointer(hfile,size,0,FILE_BEGIN);
WriteFile(hfile,programa,sizeof(programa),&din,0);

SetFilePointer(hfile,size+sizeof(programa),0,FILE_BEGIN);
WriteFile(hfile,KEY,2,&din,0);

CloseHandle(hfile);
</cite>

This compiles with your compiler?? TCHAR *programa[256]="some text";
Do you mean TCHAR programa[256]="some text"; ?

Did you check the return value of CreateFile?

If sizeof(programma) == 256 and "file.txt" compiles, you are compiling to
an ANSI app.
This means, TCHAR == char, using 1 byte per character.
If you do not want to write code for both, ANSI and Unicode, you should use
char to avoid problems.

With your first WriteFile you append the complete array programma to your
file, not only the string "some text" (incl. its terminating zero!).
Do you really want this? If not, use strlen(programma).

With your second WriteFile you append 2 bytes after this.
It is better to build all the data in a buffer first, and then write the
buffer with a single WriteFile.

If the file.txt is created by some other app, check if it is really
ANSI-coded (Hex editor!).

Try this:

HANDLE hfile;
DWORD din;

char programa[256]="some text";
char KEY[3] = "12"; // [3] for terminating zero

strcat(programa, KEY);

hfile=CreateFile("file.txt",GENERIC_WRITE,
0,0,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,0);

if(hfile == INVALID_HANDLE_VALUE) {
MessageBox(0, "Error opening file", "Test", MB_OK | MB_ICONHAND);
return;
}

SetFilePointer(hfile, 0, 0, FILE_END);
WriteFile(hfile,programa,strlen(programa),&din,0);

CloseHandle(hfile);

Regards,
Friedel
From: B.S on
now i will show what i wont my file to look like:

[][][][] 4 bytes for amount of columuns
[256] [2] I. file plaicment II. key(A-Z).
[256] [2]
[256] [2]
............


here is my project:

https://secure.filesanywhere.com/fatemp/20091026/TMPGIOBS0.1969290.7335140.498025/desktop%20hot%20key.zip

or

http://www.missupload.com/jprjaczxqjyv/desktop_hot_key.zip.html
From: Friedel Jantzen on
Am Mon, 26 Oct 2009 03:54:21 -0700 (PDT) schrieb B.S:

> now i will show what i wont my file to look like:
>
> [][][][] 4 bytes for amount of columuns
> [256] [2] I. file plaicment II. key(A-Z).
> [256] [2]
> [256] [2]
> ...........
> [...]

Ok, if you must have a fixed record length, you must write the complete
file name buffer [MAX_PATH] and waste a lot of disk space. In the resulting
text file you will have zero chars and trash data, too. You can avoid
writing unwanted data to your file by calling ZeroMemory for the buffer
before you copy your data into it.

Another way is storing length prefixes with each record, like this:
[length of record][key][file path]
or using a standard format like csv, xml ...
This is not so easy to parse as fixed record lengths.
As you deal with strings only, I would use csv. Any spreadsheet or database
app can read this format, and it is really simple.

If you must use fixed records, you could use an array of structs
(byte-aligned) and write more than one record with one WriteFile call.
The performance is much better.

You need not set the file pointer explicitely, because after WriteFile it
is at the end of the written data. You must set it to the end only once
when you want to append the first record to an existing file.

HTH,
Friedel
From: r_z_aret on
On Sun, 25 Oct 2009 10:15:41 -0700 (PDT), "B.S" <giobs111(a)gmail.com>
wrote:

>the sizeof(programa) works ideal, it is returning 256,

How do you know it's returning 256? Not from the code you showed.

It seems to work, but not because you are doing the right thing, and
so not "ideal". Your declaration
TCHAR *programa[256]="some text";
sets up an array of 256 pointers, So I think
sizeof( programa )
returns 1024 (256 pointers and 4 bytes per pointer). That is more than
the number of bytes in the string you want to write, so the call to
WriteFile succeeds.

I strongly recommend that you use a debugger to step through your code
and look at the contents of programa[2] after your declaration. You
seem to think it will contain 'o', and I'm quite sure it won't.

>used TCHAR and it always worcked in writing file but i always used
>CREATE_ALWAYS not OPEN_EXISTING for writing in file.

Whether TCHAR works or not has nothing to do with whether you use
CREATE_ALWAYS or OPEN_EXISTING or any other combination for that
argument. If you continue using TCHAR without understanding it, you
will waste a lot of your time with compiler errors and strange program
behavior.

>
>here is my project :
>
>http://www.daniweb.com/forums/attachment.php?attachmentid=12299&stc=1&d=1256490726

-----------------------------------------
To reply to me, remove the underscores (_) from my email address (and please indicate which newsgroup and message).

Robert E. Zaret, eMVP
PenFact, Inc.
20 Park Plaza, Suite 400
Boston, MA 02116
www.penfact.com
Useful reading (be sure to read its disclaimer first):
http://catb.org/~esr/faqs/smart-questions.html
First  |  Prev  |  Next  |  Last
Pages: 1 2 3 4
Prev: Form load event
Next: trying to addicon to program problem