CreateFile related
-
hello all , I am writing student info on file by following way.. Struct Student { int Marks; int RollNo; }; Student Sobj; int main() { //then i create file using ,CreateFile( ....); //I store data for 10 student for(int i=0;i<10;i++) { Sobj.Marks=10*i; Sobj.RollNo=i; WriteFile ( HandleFile , &sobj , sizeof ( struct Student ) ,...,...); } CloseHandle( ..) ; } -------------------------- This code work proper . Then I read that data using ReadFile(Handle,&sobj,....) it also work ----------------------------------- Now My problem is I am reading that file on some condition i want to change data of students eg. for roll no 3 marks 80; how should i do this . means i am reading and on some condition i have to Use WrteFile() i try using SetFilePointer But i was not able to do that ,,how to go back in file. Thanks All Ashish P.
-
hello all , I am writing student info on file by following way.. Struct Student { int Marks; int RollNo; }; Student Sobj; int main() { //then i create file using ,CreateFile( ....); //I store data for 10 student for(int i=0;i<10;i++) { Sobj.Marks=10*i; Sobj.RollNo=i; WriteFile ( HandleFile , &sobj , sizeof ( struct Student ) ,...,...); } CloseHandle( ..) ; } -------------------------- This code work proper . Then I read that data using ReadFile(Handle,&sobj,....) it also work ----------------------------------- Now My problem is I am reading that file on some condition i want to change data of students eg. for roll no 3 marks 80; how should i do this . means i am reading and on some condition i have to Use WrteFile() i try using SetFilePointer But i was not able to do that ,,how to go back in file. Thanks All Ashish P.
ashish8patil wrote:
means i am reading and on some condition i have to Use WrteFile() i try using SetFilePointer But i was not able to do that ,,how to go back in file.
Don't do this. A much better (and probably easier) way is to write the full file once again. The problem is that when writing you are not able to insert new characters. Which means that if your entry was 9 (1 character) before and has to be overwritten by 10 (2 characters), this is not possible. So, when open the file for reading, specifying that the contents should be cleared and then write your full structure again.
Cédric Moonen Software developer
Charting control [v2.0] OpenGL game tutorial in C++ -
hello all , I am writing student info on file by following way.. Struct Student { int Marks; int RollNo; }; Student Sobj; int main() { //then i create file using ,CreateFile( ....); //I store data for 10 student for(int i=0;i<10;i++) { Sobj.Marks=10*i; Sobj.RollNo=i; WriteFile ( HandleFile , &sobj , sizeof ( struct Student ) ,...,...); } CloseHandle( ..) ; } -------------------------- This code work proper . Then I read that data using ReadFile(Handle,&sobj,....) it also work ----------------------------------- Now My problem is I am reading that file on some condition i want to change data of students eg. for roll no 3 marks 80; how should i do this . means i am reading and on some condition i have to Use WrteFile() i try using SetFilePointer But i was not able to do that ,,how to go back in file. Thanks All Ashish P.
SetFilePointer
is the correct way to do it. Before that you must open the file asGENERIC_READ|GENERIC_WRITE
since you want to do both. Each record in your case will take upsizeof(Student)
bytes. So it is just a matter of multiplication to find the offset of the record to modify. So do aSetFilePointer
to that offset with move method asFILE_BEGIN
. Then do aWriteFile
with the new record.«_Superman_» I love work. It gives me something to do between weekends.
-
hello all , I am writing student info on file by following way.. Struct Student { int Marks; int RollNo; }; Student Sobj; int main() { //then i create file using ,CreateFile( ....); //I store data for 10 student for(int i=0;i<10;i++) { Sobj.Marks=10*i; Sobj.RollNo=i; WriteFile ( HandleFile , &sobj , sizeof ( struct Student ) ,...,...); } CloseHandle( ..) ; } -------------------------- This code work proper . Then I read that data using ReadFile(Handle,&sobj,....) it also work ----------------------------------- Now My problem is I am reading that file on some condition i want to change data of students eg. for roll no 3 marks 80; how should i do this . means i am reading and on some condition i have to Use WrteFile() i try using SetFilePointer But i was not able to do that ,,how to go back in file. Thanks All Ashish P.
as Cedrice said, unless you are going to have many people modifying the data at once; in which case you should store all the data in a database, and not worry about files at all. :)
Luc Pattyn [Forum Guidelines] [My Articles]
DISCLAIMER: this message may have been modified by others; it may no longer reflect what I intended, and may contain bad advice; use at your own risk and with extreme care.
-
ashish8patil wrote:
means i am reading and on some condition i have to Use WrteFile() i try using SetFilePointer But i was not able to do that ,,how to go back in file.
Don't do this. A much better (and probably easier) way is to write the full file once again. The problem is that when writing you are not able to insert new characters. Which means that if your entry was 9 (1 character) before and has to be overwritten by 10 (2 characters), this is not possible. So, when open the file for reading, specifying that the contents should be cleared and then write your full structure again.
Cédric Moonen Software developer
Charting control [v2.0] OpenGL game tutorial in C++Thanks for your reply I just give you student as example ,but my data is very large and it may be very long way ,store that data in buffer n all..
-
SetFilePointer
is the correct way to do it. Before that you must open the file asGENERIC_READ|GENERIC_WRITE
since you want to do both. Each record in your case will take upsizeof(Student)
bytes. So it is just a matter of multiplication to find the offset of the record to modify. So do aSetFilePointer
to that offset with move method asFILE_BEGIN
. Then do aWriteFile
with the new record.«_Superman_» I love work. It gives me something to do between weekends.
i also try to do same but it was not write there plz see code ServerHandle = CreateFile ( L"server.dat" , GENERIC_READ | GENERIC_WRITE , FILE_SHARE_WRITE , NULL , OPEN_EXISTING , 0 , NULL ) ; if ( ServerHandle == INVALID_HANDLE_VALUE ) { return ( 1 ) ; } ReadFile ( ServerHandle , & finfo , sizeof ( struct ServerFileInfo ) , &BytesRead , NULL ) ; FilePointer.QuadPart =-sizeof(UpdateInfoOnServer); for (i=0;i<finfo.NoUpdates ;i++) { ReadFile ( ServerHandle , & UpdateInfo , sizeof ( struct UpdateInfoOnServer ) , &BytesRead , NULL ) ; if((UpdateInfo.IsApproved==1)&&(UpdateInfo.IsDownloaded==0) ) { for(j=0;j<UpdateInfo.NumberOfPatches ;j++) { PatchFileID=UpdateInfo.PatchFileID[j] ; iTmp=DownloadByPatchFileID(); if(iTmp==1) { return 1; } } UpdateInfo.IsDownloaded=1; SetFilePointerEx ( ServerHandle , FilePointer, NULL , FILE_CURRENT ) ; WriteFile ( ServerHandle , & UpdateInfo , sizeof ( struct UpdateInfoOnServer ) , &BytesWritten , NULL ) ; } } ///////////////////////////////////////////// where i make mistake..plz tell
-
i also try to do same but it was not write there plz see code ServerHandle = CreateFile ( L"server.dat" , GENERIC_READ | GENERIC_WRITE , FILE_SHARE_WRITE , NULL , OPEN_EXISTING , 0 , NULL ) ; if ( ServerHandle == INVALID_HANDLE_VALUE ) { return ( 1 ) ; } ReadFile ( ServerHandle , & finfo , sizeof ( struct ServerFileInfo ) , &BytesRead , NULL ) ; FilePointer.QuadPart =-sizeof(UpdateInfoOnServer); for (i=0;i<finfo.NoUpdates ;i++) { ReadFile ( ServerHandle , & UpdateInfo , sizeof ( struct UpdateInfoOnServer ) , &BytesRead , NULL ) ; if((UpdateInfo.IsApproved==1)&&(UpdateInfo.IsDownloaded==0) ) { for(j=0;j<UpdateInfo.NumberOfPatches ;j++) { PatchFileID=UpdateInfo.PatchFileID[j] ; iTmp=DownloadByPatchFileID(); if(iTmp==1) { return 1; } } UpdateInfo.IsDownloaded=1; SetFilePointerEx ( ServerHandle , FilePointer, NULL , FILE_CURRENT ) ; WriteFile ( ServerHandle , & UpdateInfo , sizeof ( struct UpdateInfoOnServer ) , &BytesWritten , NULL ) ; } } ///////////////////////////////////////////// where i make mistake..plz tell
You should be using your debugger here. Set a breakpoint and single step through the code. You'll surely catch the error. And also you need to check return values of those APIs that you're calling.
«_Superman_» I love work. It gives me something to do between weekends.
-
hello all , I am writing student info on file by following way.. Struct Student { int Marks; int RollNo; }; Student Sobj; int main() { //then i create file using ,CreateFile( ....); //I store data for 10 student for(int i=0;i<10;i++) { Sobj.Marks=10*i; Sobj.RollNo=i; WriteFile ( HandleFile , &sobj , sizeof ( struct Student ) ,...,...); } CloseHandle( ..) ; } -------------------------- This code work proper . Then I read that data using ReadFile(Handle,&sobj,....) it also work ----------------------------------- Now My problem is I am reading that file on some condition i want to change data of students eg. for roll no 3 marks 80; how should i do this . means i am reading and on some condition i have to Use WrteFile() i try using SetFilePointer But i was not able to do that ,,how to go back in file. Thanks All Ashish P.
ashish8patil wrote:
SetFilePointer
That sounds about right. Maybe you can tell us why you weren't able to use that? Because the following program, based on your (syntactically incorrect) code fragments, compiles and runs successfully. Here's a hint - check function returns values and GetLastError[^].
#include <Windows.h>
struct Student
{
int Marks;
int RollNo;
};
struct Student sobj;
int main()
{
HANDLE hFile = CreateFile("a.a", GENERIC_READ|GENERIC_WRITE, 0, 0, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
for(int i=0;i<10;i++)
{
sobj.Marks=10*i;
sobj.RollNo=i;
DWORD written;
WriteFile ( hFile , &sobj , sizeof ( sobj ) , &written, 0);
}int i = 3;
sobj.Marks=5*i;
sobj.RollNo=i;
SetFilePointer(hFile, sizeof(sobj)*i, 0, FILE_BEGIN);
DWORD written;
WriteFile ( hFile , &sobj , sizeof ( sobj ) , &written, 0);CloseHandle(hFile) ;
}Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
-
You should be using your debugger here. Set a breakpoint and single step through the code. You'll surely catch the error. And also you need to check return values of those APIs that you're calling.
«_Superman_» I love work. It gives me something to do between weekends.
but logically are this things correct? LARGE_INTEGER FilePointer ; FilePointer.QuadPart =-sizeof(UpdateInfoOnServer); SetFilePointerEx ( ServerHandle , FilePointer, NULL , FILE_CURRENT ) ;