-1

Given below is a part of my whole program:

         void filewrite(fstream &f2)
         {
            f2.seekp(0,ios::beg);
            f2.write(customerno,strlen(customerno));
            f2.seekp(24,ios::beg);
            f2.write(customername,strlen(customername));
            f2.seekp(56,ios::beg);
            f2.write(product,strlen(product));
            f2<<endl;
         }

here f2 is the file pointer customerno,customername and product are strings 0,24 and 56 are respective positions for these strings from the beginning

the problem i m facing is, everytime i m trying to write a new record it is over writing the previous one and not going to the next line.

i want each new record to be aligned with 0th, 24th and 56th position. how should i do this ? Thanks in advance.

  • Is there a reason you need each string to be at a specific location? You could just use a whitespace delimited approach. – Andrue May 11 '18 at 19:13
  • yes it is important ! – Vinita Kumari May 11 '18 at 19:14
  • Can you post part of the code where you're calling this function? Also, why does the spacing matter? – Andrue May 11 '18 at 19:17
  • seekp give the position in the output stream. So, you are overwriting the same positions every time you call this function. – Andrue May 11 '18 at 19:34
  • You are not writing the terminating NUL so you will have problems determining the lengths of the strings when you read them back. – stark May 11 '18 at 19:34

2 Answers2

1

You are giving yourself a challenging problem if you use that method. You want to use std::setw() to do this for you.

Here is a far simpler method to do this:

#include <fstream>
#include <iomanip>
using namespace std; 
void filewrite(ofstream &f, string customerno, string customername, string product)
{
   f << left << setw(24) << customerno << setw(32) << customername << product << endl;
}
Andrue
  • 688
  • 3
  • 11
  • 27
  • hey thank you so much Andrue. but i have another problem. the pointer is still not going to the next line. the cursor goes back to the top left corner i.e, the beginning of the line. :( help me plz – Vinita Kumari May 12 '18 at 07:29
  • I tested the code before I posted and it worked. Can you update your question with how you are calling it? – Andrue May 13 '18 at 03:16
0

I don't know, if i am getting your question right, but if you want to make sure to seek inside the current line you can do it like that:

void filewrite(fstream &f2)
{
  long pos = f2.tellp();

  f2.seekp(pos + 0);
  f2.write(customerno,strlen(customerno));
  f2.seekp(pos + 24);
  f2.write(customername,strlen(customername));
  f2.seekp(pos + 56);
  f2.write(product,strlen(product));
  f2<<endl;
}

This only works as intended though, if you can make sure, that the filestream is at the beginning of the next line when the function filewrite is called.

Otherwise i will be kind of complex to find the correct placement of the cursor in the stream.

n00ne
  • 239
  • 1
  • 7
  • Make sure pos is 0 the first time it is called for this method. Also, note that this code will not compile because the variables are out of scope. – Andrue May 11 '18 at 20:06
  • Andrue, thanks for your feedback. You are right, i added three global variables before the function for this code snippet to compile. – n00ne May 11 '18 at 20:17