File Help

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Malloc
    New Member
    • Jan 2007
    • 4

    File Help

    Hello everyone...

    I am new in this forum so please forgive me if i'm doing this wrong.

    Lets get to the problem now.I am trying to read a file and put data in a specific record,witch means that i should propably move the pointer of my file to the record that i want and then write the data that i want.The problem is that i don't know how to do that.I've just started learning c++ and so far i've been searching the net to find informations about creating files etc.

    Thanks in advance
    Malloc.....

    P.S : My english aren't very good so just ignore any mistake you notice.
  • khajeddin
    New Member
    • Nov 2006
    • 51

    #2
    File Help

    Originally posted by Malloc
    Hello everyone...

    I am new in this forum so please forgive me if i'm doing this wrong.

    Lets get to the problem now.I am trying to read a file and put data in a specific record,witch means that i should propably move the pointer of my file to the record that i want and then write the data that i want.The problem is that i don't know how to do that.I've just started learning c++ and so far i've been searching the net to find informations about creating files etc.

    Thanks in advance
    Malloc.....

    P.S : My english aren't very good so just ignore any mistake you notice.
    hi:
    you have to use function "fseek" for moving the pointer of file ,and use function "fwrite "to write every record in the file.if you have to save several records you can use a WHILE loop ,with condition (!feof(pt)).
    pt is a FILE pointer the you have been define it before.
    Code:
    #include<stdio.h>
    FILE *pt;
    pt=fopen("file name","condion of opneing the file")
    while(!feof(pt))
        {
         fwrite ( void *buffer, int num_byte,int count ,FILE *pt );
         fseek(FILE *pt ,long num_byte ,int origin);
        }
    Last edited by khajeddin; Jan 17 '07, 07:04 PM. Reason: mistype

    Comment

    • Malloc
      New Member
      • Jan 2007
      • 4

      #3
      Originally posted by khajeddin
      hi:
      you have to use function "fseek" for moving the pointer of file ,and use function "fwrite "to write every record in the file.if you have to save several records you can use a WHILE loop ,with condition (!feof(pt)).
      pt is a FILE pointer the you have been define it before.
      Code:
      #include<stdio.h>
      FILE *pt;
      pt=fopen("file name","condion of opneing the file")
      while(!feof(pt))
          {
           fwrite ( void *buffer, int num_byte,int count ,FILE *pt );
           fseek(FILE *pt ,long num_byte ,int origin);
          }

      Thanks a lot.I'll try it.

      Comment

      • horace1
        Recognized Expert Top Contributor
        • Nov 2006
        • 1510

        #4
        if you are coding in C++ you will probably use seekg() and seekp()

        Comment

        • Malloc
          New Member
          • Jan 2007
          • 4

          #5
          Hi again

          I tryed the seekp but i still have a problem.When i try to put a record to the file it puts it to the begining of it.At first i thought that the seekp didn't work but when i used tellp() to check it out i saw that the pointer was where i told it to be.So,i must be doing something wrong with the way i insert the record to the file.
          Here is my code

          myfile.open("Fi le.dat",ios::ou t | ios::app);
          if (products.is_op en())
          {
          products.seekp( meg*kod,ios::be g);
          products << proion.code ;
          }
          else
          cout << "Couldn't open file......." << endl;

          Please help.
          Thanks......

          PS: meg=the size of every record
          kod = the number of the record that i want to go.

          Comment

          • horace1
            Recognized Expert Top Contributor
            • Nov 2006
            • 1510

            #6
            here is a simple example using seekp() and seekg()
            Code:
            #include <fstream>
            #include <iostream>
            using namespace std;
            
            struct  Test{          // test structure
                   int i;
                   float x;
                   };
                   
            int main () {
              long pos;
              Test test;
              fstream outfile;
              // create file with some data
              outfile.open ("test.txt",  ios::binary | ios::out);
                if (! outfile)                                                 // open OK ?
                    {
                    cout << "\nUnable to open file "  << strerror(errno);
                    return 1;                                                 // open failed
                    }
              // write 10 records
              for(int i=0; i< 10; i++)
                  {
                   test.i=i;
                   test.x=i*1000.;
                   outfile.write ((char *) &test,sizeof(Test));
                   }
              outfile.close();  
              
              // now, open file for read/write
              outfile.open ("test.txt",  ios::binary | ios::out | ios::in);
              if (! outfile)                                                 // open OK ?
                    {
                    cout << "\nUnable to open file "  << strerror(errno);
                    return 1;                                                 // open failed
                    }
              // print the contents 
              cout << "\nfile contents\n";
              while(outfile.good())
                  {
                   outfile.read((char *) &test,sizeof(Test));
                   if(outfile.good()) cout << "i " << test.i << " x " << test.x << endl;
                   }
              outfile.clear();    // cklear error condition from last read
              
              // rewrite record 5 and add record on the end 
              outfile.seekp (sizeof(Test)*5);
              test.i=10000;
              test.x=20000.0;
              outfile.write ((char *) &test,sizeof(Test));
              outfile.seekp (0, ios::end);
              test.i=100000;
              test.x=200000.0;
              outfile.write ((char *) &test,sizeof(Test));
            
               // print the updated contents 
              cout << "\nupdated file contents\n";
              outfile.seekg (0);
              while(outfile.good())
                  {
                   outfile.read((char *) &test,sizeof(Test));
                   if(outfile.good()) cout << "i " << test.i << " x " << test.x << endl;
                   }
              outfile.clear();    // cklear error condition from last read
              outfile.close();
              cin.get();
              return 0;
            }
            when run it gives

            file contents
            i 0 x 0
            i 1 x 1000
            i 2 x 2000
            i 3 x 3000
            i 4 x 4000
            i 5 x 5000
            i 6 x 6000
            i 7 x 7000
            i 8 x 8000
            i 9 x 9000

            updated file contents
            i 0 x 0
            i 1 x 1000
            i 2 x 2000
            i 3 x 3000
            i 4 x 4000
            i 10000 x 20000
            i 6 x 6000
            i 7 x 7000
            i 8 x 8000
            i 9 x 9000
            i 100000 x 200000

            Comment

            • Malloc
              New Member
              • Jan 2007
              • 4

              #7
              Originally posted by horace1
              here is a simple example using seekp() and seekg()
              Code:
              #include <fstream>
              #include <iostream>
              using namespace std;
              
              struct  Test{          // test structure
                     int i;
                     float x;
                     };
                     
              int main () {
                long pos;
                Test test;
                fstream outfile;
                // create file with some data
                outfile.open ("test.txt",  ios::binary | ios::out);
                  if (! outfile)                                                 // open OK ?
                      {
                      cout << "\nUnable to open file "  << strerror(errno);
                      return 1;                                                 // open failed
                      }
                // write 10 records
                for(int i=0; i< 10; i++)
                    {
                     test.i=i;
                     test.x=i*1000.;
                     outfile.write ((char *) &test,sizeof(Test));
                     }
                outfile.close();  
                
                // now, open file for read/write
                outfile.open ("test.txt",  ios::binary | ios::out | ios::in);
                if (! outfile)                                                 // open OK ?
                      {
                      cout << "\nUnable to open file "  << strerror(errno);
                      return 1;                                                 // open failed
                      }
                // print the contents 
                cout << "\nfile contents\n";
                while(outfile.good())
                    {
                     outfile.read((char *) &test,sizeof(Test));
                     if(outfile.good()) cout << "i " << test.i << " x " << test.x << endl;
                     }
                outfile.clear();    // cklear error condition from last read
                
                // rewrite record 5 and add record on the end 
                outfile.seekp (sizeof(Test)*5);
                test.i=10000;
                test.x=20000.0;
                outfile.write ((char *) &test,sizeof(Test));
                outfile.seekp (0, ios::end);
                test.i=100000;
                test.x=200000.0;
                outfile.write ((char *) &test,sizeof(Test));
              
                 // print the updated contents 
                cout << "\nupdated file contents\n";
                outfile.seekg (0);
                while(outfile.good())
                    {
                     outfile.read((char *) &test,sizeof(Test));
                     if(outfile.good()) cout << "i " << test.i << " x " << test.x << endl;
                     }
                outfile.clear();    // cklear error condition from last read
                outfile.close();
                cin.get();
                return 0;
              }
              when run it gives

              file contents
              i 0 x 0
              i 1 x 1000
              i 2 x 2000
              i 3 x 3000
              i 4 x 4000
              i 5 x 5000
              i 6 x 6000
              i 7 x 7000
              i 8 x 8000
              i 9 x 9000

              updated file contents
              i 0 x 0
              i 1 x 1000
              i 2 x 2000
              i 3 x 3000
              i 4 x 4000
              i 10000 x 20000
              i 6 x 6000
              i 7 x 7000
              i 8 x 8000
              i 9 x 9000
              i 100000 x 200000

              Thanks a lot.That really helped me...

              Comment

              Working...