Click here to Skip to main content
15,886,518 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Currently Im trying to make my own program to test my vocabulary. It is succesfull. But how do i use myself of createfile() and readfile() to store values in the harddrive?
And to further improve my program how can i create variables in a loop which i can use later in the program?
Posted

Take a look at this example of using CreateFile / ReadFile: Creating and Using a Temporary File[^]:

C++
//  Creates the new file to write to for the upper-case version.
hTempFile = CreateFile((LPTSTR) szTempFileName, // file name
                       GENERIC_WRITE,        // open for write
                       0,                    // do not share
                       NULL,                 // default security
                       CREATE_ALWAYS,        // overwrite existing
                       FILE_ATTRIBUTE_NORMAL,// normal file
                       NULL);                // no template
if (hTempFile == INVALID_HANDLE_VALUE)
{
    PrintError(TEXT("Second CreateFile failed"));
    if (!CloseHandle(hFile))
    {
        PrintError(TEXT("CloseHandle(hFile) failed"));
        return (7);
    }
    return (4);
}

//  Reads BUFSIZE blocks to the buffer and converts all characters in
//  the buffer to upper case, then writes the buffer to the temporary
//  file.
do
{
    if (ReadFile(hFile, chBuffer, BUFSIZE, &dwBytesRead, NULL))
    {
        //  Replaces lower case letters with upper case
        //  in place (using the same buffer). The return
        //  value is the number of replacements performed,
        //  which we aren't interested in for this demo.
        CharUpperBuffA(chBuffer, dwBytesRead);

        fSuccess = WriteFile(hTempFile,
                             chBuffer,
                             dwBytesRead,
                             &dwBytesWritten,
                             NULL);
        if (!fSuccess)
        {
            PrintError(TEXT("WriteFile failed"));
            return (5);
        }
    }
    else
    {
        PrintError(TEXT("ReadFile failed"));
        return (6);
    }
//  Continues until the whole file is processed.
} while (dwBytesRead == BUFSIZE);

//  The handles to the files are no longer needed, so
//  they are closed prior to moving the new file.
if (!CloseHandle(hFile))
{
   PrintError(TEXT("CloseHandle(hFile) failed"));
   return (7);
}


Quote:
And to further improve my program how can i create variables in a loop which i can use later in the program?


You need to define the variables out of loop in order to use them later in the program.
 
Share this answer
 
v2
Comments
JONLIN 28-Sep-15 5:57am    
You say it yourself this is how I can create a temporary file but a permanent one? By that i mean a file which is stored on the harddrive and will still exist even if I shut both the program and computer. But thanks with this i may be able to understand a bit more of the MSDN too :D
and yes i know that i wont be able to use variebles created inside a loop outside of the loop. I was more wondering if I could create as many variables as i want as i enter more words for the program to use.
In order to use CreateFile, (WriteFile,) ReadFile, you have to read the documentation, of course (see, for instance CreateFile at MSDN[^]).
You might also use the (somehow more user-friendly) standard C++ fstream instances for file I/O.
 
Share this answer
 
Comments
JONLIN 28-Sep-15 5:51am    
Yes i did read a bit of the MSDN but it could have been wroten with hieroglyfs becouse there wasnt much i understod :-) thats the reasons i made this question
Leo Chapiro 28-Sep-15 7:02am    
Some people call this "hieroglyfs" a C++ programming language.
CPallini 28-Sep-15 15:39pm    
Are you Egyptian? :-D
JONLIN 28-Sep-15 15:41pm    
Nah I'm from sweden :-D
CPallini 28-Sep-15 15:41pm    
You could start with a tutorial, see, for instance
http://www.cplusplus.com/doc/tutorial/files/

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900