473,396 Members | 2,011 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

File Help

4
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.
Jan 17 '07 #1
6 1775
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.
Expand|Select|Wrap|Line Numbers
  1. #include<stdio.h>
  2. FILE *pt;
  3. pt=fopen("file name","condion of opneing the file")
  4. while(!feof(pt))
  5.     {
  6.      fwrite ( void *buffer, int num_byte,int count ,FILE *pt );
  7.      fseek(FILE *pt ,long num_byte ,int origin);
  8.     }
Jan 17 '07 #2
Malloc
4
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.
Expand|Select|Wrap|Line Numbers
  1. #include<stdio.h>
  2. FILE *pt;
  3. pt=fopen("file name","condion of opneing the file")
  4. while(!feof(pt))
  5.     {
  6.      fwrite ( void *buffer, int num_byte,int count ,FILE *pt );
  7.      fseek(FILE *pt ,long num_byte ,int origin);
  8.     }

Thanks a lot.I'll try it.
Jan 18 '07 #3
horace1
1,510 Expert 1GB
if you are coding in C++ you will probably use seekg() and seekp()
http://www.cplusplus.com/istream::seekg
http://www.cplusplus.com/reference/i...eam/seekp.html
Jan 18 '07 #4
Malloc
4
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("File.dat",ios::out | ios::app);
if (products.is_open())
{
products.seekp(meg*kod,ios::beg);
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.
Jan 18 '07 #5
horace1
1,510 Expert 1GB
here is a simple example using seekp() and seekg()
Expand|Select|Wrap|Line Numbers
  1. #include <fstream>
  2. #include <iostream>
  3. using namespace std;
  4.  
  5. struct  Test{          // test structure
  6.        int i;
  7.        float x;
  8.        };
  9.  
  10. int main () {
  11.   long pos;
  12.   Test test;
  13.   fstream outfile;
  14.   // create file with some data
  15.   outfile.open ("test.txt",  ios::binary | ios::out);
  16.     if (! outfile)                                                 // open OK ?
  17.         {
  18.         cout << "\nUnable to open file "  << strerror(errno);
  19.         return 1;                                                 // open failed
  20.         }
  21.   // write 10 records
  22.   for(int i=0; i< 10; i++)
  23.       {
  24.        test.i=i;
  25.        test.x=i*1000.;
  26.        outfile.write ((char *) &test,sizeof(Test));
  27.        }
  28.   outfile.close();  
  29.  
  30.   // now, open file for read/write
  31.   outfile.open ("test.txt",  ios::binary | ios::out | ios::in);
  32.   if (! outfile)                                                 // open OK ?
  33.         {
  34.         cout << "\nUnable to open file "  << strerror(errno);
  35.         return 1;                                                 // open failed
  36.         }
  37.   // print the contents 
  38.   cout << "\nfile contents\n";
  39.   while(outfile.good())
  40.       {
  41.        outfile.read((char *) &test,sizeof(Test));
  42.        if(outfile.good()) cout << "i " << test.i << " x " << test.x << endl;
  43.        }
  44.   outfile.clear();    // cklear error condition from last read
  45.  
  46.   // rewrite record 5 and add record on the end 
  47.   outfile.seekp (sizeof(Test)*5);
  48.   test.i=10000;
  49.   test.x=20000.0;
  50.   outfile.write ((char *) &test,sizeof(Test));
  51.   outfile.seekp (0, ios::end);
  52.   test.i=100000;
  53.   test.x=200000.0;
  54.   outfile.write ((char *) &test,sizeof(Test));
  55.  
  56.    // print the updated contents 
  57.   cout << "\nupdated file contents\n";
  58.   outfile.seekg (0);
  59.   while(outfile.good())
  60.       {
  61.        outfile.read((char *) &test,sizeof(Test));
  62.        if(outfile.good()) cout << "i " << test.i << " x " << test.x << endl;
  63.        }
  64.   outfile.clear();    // cklear error condition from last read
  65.   outfile.close();
  66.   cin.get();
  67.   return 0;
  68. }
  69.  
  70.  
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
Jan 18 '07 #6
Malloc
4
here is a simple example using seekp() and seekg()
Expand|Select|Wrap|Line Numbers
  1. #include <fstream>
  2. #include <iostream>
  3. using namespace std;
  4.  
  5. struct  Test{          // test structure
  6.        int i;
  7.        float x;
  8.        };
  9.  
  10. int main () {
  11.   long pos;
  12.   Test test;
  13.   fstream outfile;
  14.   // create file with some data
  15.   outfile.open ("test.txt",  ios::binary | ios::out);
  16.     if (! outfile)                                                 // open OK ?
  17.         {
  18.         cout << "\nUnable to open file "  << strerror(errno);
  19.         return 1;                                                 // open failed
  20.         }
  21.   // write 10 records
  22.   for(int i=0; i< 10; i++)
  23.       {
  24.        test.i=i;
  25.        test.x=i*1000.;
  26.        outfile.write ((char *) &test,sizeof(Test));
  27.        }
  28.   outfile.close();  
  29.  
  30.   // now, open file for read/write
  31.   outfile.open ("test.txt",  ios::binary | ios::out | ios::in);
  32.   if (! outfile)                                                 // open OK ?
  33.         {
  34.         cout << "\nUnable to open file "  << strerror(errno);
  35.         return 1;                                                 // open failed
  36.         }
  37.   // print the contents 
  38.   cout << "\nfile contents\n";
  39.   while(outfile.good())
  40.       {
  41.        outfile.read((char *) &test,sizeof(Test));
  42.        if(outfile.good()) cout << "i " << test.i << " x " << test.x << endl;
  43.        }
  44.   outfile.clear();    // cklear error condition from last read
  45.  
  46.   // rewrite record 5 and add record on the end 
  47.   outfile.seekp (sizeof(Test)*5);
  48.   test.i=10000;
  49.   test.x=20000.0;
  50.   outfile.write ((char *) &test,sizeof(Test));
  51.   outfile.seekp (0, ios::end);
  52.   test.i=100000;
  53.   test.x=200000.0;
  54.   outfile.write ((char *) &test,sizeof(Test));
  55.  
  56.    // print the updated contents 
  57.   cout << "\nupdated file contents\n";
  58.   outfile.seekg (0);
  59.   while(outfile.good())
  60.       {
  61.        outfile.read((char *) &test,sizeof(Test));
  62.        if(outfile.good()) cout << "i " << test.i << " x " << test.x << endl;
  63.        }
  64.   outfile.clear();    // cklear error condition from last read
  65.   outfile.close();
  66.   cin.get();
  67.   return 0;
  68. }
  69.  
  70.  
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...
Jan 18 '07 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

21
by: Dave | last post by:
After following Microsofts admonition to reformat my system before doing a final compilation of my app I got many warnings/errors upon compiling an rtf file created in word. I used the Help...
9
by: Hans-Joachim Widmaier | last post by:
Hi all. Handling files is an extremely frequent task in programming, so most programming languages have an abstraction of the basic files offered by the underlying operating system. This is...
4
by: dixie | last post by:
Help, I'm really out of my depth here (not unusual I hear you say :-). I have just installed HTML Help in an application. I told it in the Project Properties the path to the help file. I then...
1
by: Tim Marshall | last post by:
I'm putting together my first help file (using Easy Help, http://www.easyhelp.com/). So far, so good. I'm able to use the Help File and Help Context ID to have things from my help file pop up...
6
by: Sushil Prasad | last post by:
I am having problem in displaying the compiled help file. In asp.net app I have link button labeled help. On the users click of this button i do the respose.redirect to the url of the .chm file....
4
by: News | last post by:
Hi Everyone, The attached code creates client connections to websphere queue managers and then processes an inquiry against them. The program functions when it gets options from the command...
8
by: Andrew Robert | last post by:
Hi Everyone. I tried the following to get input into optionparser from either a file or command line. The code below detects the passed file argument and prints the file contents but the...
2
by: quicklearner | last post by:
hi I have made a CHM file and I have linked my .CHM file with the controls on the Form in my application which is made in C# 2005. Its working fine . But I want that if user presses 'F1' then when...
2
by: avanti | last post by:
Hi, I use ClickOnce to publish and install an application in the company. I have a help file for the same in .chm format. It is part of the C# project as a 'content' file. I also tried as...
2
by: hotflash | last post by:
Hi All, I found the best pure ASP code to upload a file to either server and/or MS Access Database. It works fine for me however, there is one thing that I don't like and have tried to fix but...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.