i've wrote a code that read and write to some files,
and i have a segmentation fault when i'm running it in the g++ (in the visual studio i don't have any problem like that..)

anyway,
i think the fault may come from this line,
can anyone tell me if it is legal?

tempIn.seekg (tempOut.tellp ());

the declaration:

// pointer to input file
      ifstream in;
      // pointer to output file
      ofstream out;
      // pointer to temp file
      ifstream tempIn;
      ofstream tempOut;

the opening of the files:

out.open("output.txt", ios :: out | ios :: trunc);
      in.open ("input.txt");
      tempIn.open ("temp.txt");
      tempOut.open ("temp.txt", ios :: trunc | ios :: out);

(what i'm trying to do in the first code - to put the in pointer of the file in the place of the out pointer)

tempIn.seekg (tempOut.tellp ());

Problem with this line is due to seekg function. seekg and seekp calls take the position from which they need to start seeking as flags as: file_ptr.seekg(<offset>,flag); Offset is given by tempOut.tellp() but the flag should be one among :
ios::beg to start counting from beginning.
ios::cur to start counting from current file pointer position.
ios::end to start counting from the end of the file.

According to me here you need ios::beg flag so replace that statement with:

tempIn.seekg (tempOut.tellp (),ios::beg);

Problem with this line is due to seekg function. seekg and seekp calls take the position from which they need to start seeking as flags as: file_ptr.seekg(<offset>,flag); Offset is given by tempOut.tellp() but the flag should be one among :
ios::beg to start counting from beginning.
ios::cur to start counting from current file pointer position.
ios::end to start counting from the end of the file.

According to me here you need ios::beg flag so replace that statement with:

tempIn.seekg (tempOut.tellp (),ios::beg);

thx, i'll try that,
also i'm not sure..
when i looked in the net i saw that there are 2 calls to this function, one of them doesn't need the second flag.

and about the flag - i didn't understand why beg or end,
i want it would be in the place of the output pointer, somewhere in the text.

That is right only one argument suffices when you are providing the position in the stream you want to access directly.

And the directions is for the seek call to count.
ios::beg with this the file pointer starts to calculate offset from the beginning of the file(i,e to say it starts counting the number mentioned as offset from the file start.)
and so on with the other flags.

I highlighted that as the mistake because with respect to g++ I think the flag field is needed.

thx for the explanation :)
i've changed it.

i suppose the seg. fault is somewhere else... :(

have a good day!

This question has already been answered. Start a new discussion instead.