Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. CreateFile related

CreateFile related

Scheduled Pinned Locked Moved C / C++ / MFC
helptutorial
9 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    ashish8patil
    wrote on last edited by
    #1

    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.

    C _ L S 4 Replies Last reply
    0
    • A ashish8patil

      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.

      C Offline
      C Offline
      Cedric Moonen
      wrote on last edited by
      #2

      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++

      A 1 Reply Last reply
      0
      • A ashish8patil

        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.

        _ Offline
        _ Offline
        _Superman_
        wrote on last edited by
        #3

        SetFilePointer is the correct way to do it. Before that you must open the file as GENERIC_READ|GENERIC_WRITE since you want to do both. Each record in your case will take up sizeof(Student) bytes. So it is just a matter of multiplication to find the offset of the record to modify. So do a SetFilePointer to that offset with move method as FILE_BEGIN. Then do a WriteFile with the new record.

        «_Superman_» I love work. It gives me something to do between weekends.

        A 1 Reply Last reply
        0
        • A ashish8patil

          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.

          L Offline
          L Offline
          Luc Pattyn
          wrote on last edited by
          #4

          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.


          1 Reply Last reply
          0
          • C Cedric Moonen

            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++

            A Offline
            A Offline
            ashish8patil
            wrote on last edited by
            #5

            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..

            1 Reply Last reply
            0
            • _ _Superman_

              SetFilePointer is the correct way to do it. Before that you must open the file as GENERIC_READ|GENERIC_WRITE since you want to do both. Each record in your case will take up sizeof(Student) bytes. So it is just a matter of multiplication to find the offset of the record to modify. So do a SetFilePointer to that offset with move method as FILE_BEGIN. Then do a WriteFile with the new record.

              «_Superman_» I love work. It gives me something to do between weekends.

              A Offline
              A Offline
              ashish8patil
              wrote on last edited by
              #6

              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

              _ 1 Reply Last reply
              0
              • A ashish8patil

                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

                _ Offline
                _ Offline
                _Superman_
                wrote on last edited by
                #7

                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.

                A 1 Reply Last reply
                0
                • A ashish8patil

                  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.

                  S Offline
                  S Offline
                  Stuart Dootson
                  wrote on last edited by
                  #8

                  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

                  1 Reply Last reply
                  0
                  • _ _Superman_

                    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.

                    A Offline
                    A Offline
                    ashish8patil
                    wrote on last edited by
                    #9

                    but logically are this things correct? LARGE_INTEGER FilePointer ; FilePointer.QuadPart =-sizeof(UpdateInfoOnServer); SetFilePointerEx ( ServerHandle , FilePointer, NULL , FILE_CURRENT ) ;

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups