-2

The read function has it's first argument as pointer to an array to store the n bytes(mentioned by the second argument) in the file...

The query is,

if i have created 2 objects of class x..

then,

x stack[2]; 

Assume i've filled the 2 object contents, opened a file 'xyz'(using fstream i've created object file & used the command file.open("xyz",ios::in|ios::out) and filled the file with contents of 2 objects using write..

now if i use this command

file.read((char*) &stack[1],sizeof(stack[0]))

will the stack[1] have the contents of stack[0],

i.e. will the read function take it's first argument address as &stack[1] or will it consider &stack[0] ??

Update:The code which is used to reverse the contents of the stack

// //To reverse the contents of the objects

    #include "stdafx.h"
    #include<iostream>
    #include<fstream>
    #include<iomanip>

    using namespace std;

    class Inventory
    {
     char name[10];
     int code;
     int cost;

     public:

     void readdata();
     void writedata();
    };

    void Inventory::readdata(void)
    {
     cin>>name;
     cin>>code;
     cin>>cost;
    }

    void Inventory::writedata(void)
    {
     cout<<setiosflags(ios::left)
     <<setw(10)<<name
     <<resetiosflags(ios::right)
     <<setw(10)<<code
     <<setprecision(2)
     <<setw(10)<<cost
     <<endl;
    }

    int main()
    {
     Inventory item[3];

     fstream file;

     file.open("Stock",ios::in|ios::out);

     cout<<"Enter details for three items\n";

     for(int i=0;i<3;i++)
     {
       item[i].readdata();
       file.write((char*) &item[i],sizeof(item[i]));
     }
     file.seekg(0);

     cout<<"\nOutput\n\n";

     for(int i=0;i<3;i++)
     {
         file.read((char*) &item[2-i],sizeof(item[i]));
     }

     for(int i=0;i<3;i++)item[i].writedata();
     file.close();
     return 0;

    }
Matt
  • 19,570
  • 12
  • 62
  • 104
bkcpro
  • 15
  • 7
  • 1
    I think the answer is "yes". but this is *really* hard to read. Is it really so difficult to use correct punctuation, spelling and grammar? – Beta Sep 03 '12 at 15:50
  • I am still unable to figure out the question. :( – Coding Mash Sep 03 '12 at 15:51
  • 1
    The second argument is there to tell `read` how many bytes it needs to read and that's it. It's evaluated at compile time and has no direct correlation to *what* is read. – jrok Sep 03 '12 at 15:59
  • @CodingMash i've edited the query properly(i guess so) are u able to get it now? – bkcpro Sep 03 '12 at 16:20
  • @Beta so if i use the read command as above ... will it have its first argument consider address of stack[0] or stack[1], to generalize even if i have it's first argument as stack[n].. even then will it consider the starting address(stack[0]) of the array of stack objects...? – bkcpro Sep 03 '12 at 16:26
  • Please post the code in your question, as text. – ecatmur Sep 03 '12 at 17:21
  • 1
    Don't post *links to screenshots* of the code, post the actual code. **I** tried looking at the screenshot **you** provided, and the site began to bombard me with unskippable video, so **I** closed it. **I** have tested a version of this code, and it overwrites `stack[1]` as intended. If **you** are having trouble, post the code **you** are using, and maybe we can help **you**. – Beta Sep 03 '12 at 17:21
  • @Beta check out the code... the code was written to reverse contents of the array of objects of class Inventory.. But when i output it displays their original contents... Is there a wrong with the read statement.. – bkcpro Sep 03 '12 at 17:50
  • @ecatmur Code posted... check it out... is there any issue or a logical err in the read statement...? or have i missed anything.. – bkcpro Sep 03 '12 at 17:53
  • @Beta u've tested a version of the code.. but test mine.. u'll find that it's not overwritten.. so what's the err? – bkcpro Sep 03 '12 at 20:23

3 Answers3

1

Assuming that you've rewound or reopened the file, that you've written the data out correctly, and that x is trivially copyable (which implies that the destructor of x has no side effects), then yes: You have written an elaborate way of copying stack[0] into stack[1].


Update: Without working through all your code, here's something that should work:

#include <type_traits>
#include <fstream>

int main()
{
    char const * filename = "myfile.bin";

    Foo stack[2] = { /* ... initialize ... */ };

    static_assert(std::is_trivially_copyable<Foo>::value,
                  "Error: Foo cannot be serialized naively.");

    {
        std::ofstream ofile(filename, std::ios::binary);
        ofile.write(reinterpret_cast<char const *>(stack), sizeof stack);
    }

    {
        std::ifstream ifile(filename, std::ios::binary);
        ifile.read(reinterpret_cast<char *>(stack + 1), sizeof(stack[0]));
    }

    assert(stack[0] == stack[1]);  // if operator==() is implemented
}
Kerrek SB
  • 428,875
  • 83
  • 813
  • 1,025
  • but then if i output the contents of stack[1].. it's displays the original contents ... but if the read actually consider the first argument as &stack[1] then it would have overwritten with contents of stack[0] :) – bkcpro Sep 03 '12 at 16:28
  • `stack[1]` is unaffected. You now have two copies of the original `stack[1]`. – Kerrek SB Sep 03 '12 at 17:30
  • No, I haven't seen any pictures. But I'll humour you and post a complete piece of code that *should* work. – Kerrek SB Sep 03 '12 at 17:52
  • yes.. i understand ur code.. but then u've used the same read statement that i've used incremented the address to the next object as first argument(stack+1)... So.. Why hasnt my code has it's contents reversed... ? – bkcpro Sep 03 '12 at 18:03
  • @bkpro: Whoops, above I meant "you get two copies of `stack[0]`". Nothing is *reversed*, you just have two copies of the same thing. You write two elements but only read one back. And `stack + 1` is the same as `&stack[1]`. – Kerrek SB Sep 03 '12 at 18:09
  • yep i know that stack+1 and &stack[1] are the same... But check out my code... for(int i=0;i<3;i++) { file.read((char*) &item[2-i],sizeof(item[i])); } when i output the objects they print the same instead of overwriting as you've said "you get two copies of stack[0]"... – bkcpro Sep 03 '12 at 18:14
  • I'l explain my code in simple words so that u can concentrate only on read statement... I've created 3 objects with class Inventory.. Filled the objects with contents.. Wrote those contents on to a file.. Lastly coming to the read statement i've used for loop(3 iterations) file.read((char*) &item[2-i],sizeof(item[i])); assume read has copied item[i] bytes(still skeptical from which location it has started to copy) and put it in item[2] at first.. & so on till the end of loop.. so finally if u output the contents.. item[0] should have itemp[2] contents and vice versa..itemp[1] will same – bkcpro Sep 03 '12 at 18:26
0

Your code doesn't use stack[0] in an evaluated context anywhere; sizeof is a non-evaluated context. So there's no reason that read would use stack[0].

ecatmur
  • 137,771
  • 23
  • 263
  • 343
  • ok,irrespective of the second argument.. let it consider the first (stack[0])bytes from the file to be copied into memory pointed by the first argument... then i've given address of stack[1].. thus,the contents of stack[1] should be overwritten by contents of stack[0].. but instead it doesnt and retains it's original contents.. which means the first argument has considered the address as starting address of the array of objects (i.e. &stack[0]) – bkcpro Sep 03 '12 at 16:37
  • i've tested and output the contents of stack[0] and stack[1] ... and stack[1] contents are not modified.. what i understand is irrespective of the address of object n we've mentioned .. it always take the address of stack[0] – bkcpro Sep 03 '12 at 16:40
  • @bkpro there are any number of reasons why `basic_ifstream::read` would fail to modify its argument; I'd guess you're not resetting it after writing out the original data. – ecatmur Sep 03 '12 at 16:45
0

Try this:

file.open("Stock",ios::in|ios::trunc|ios::out);
Beta
  • 86,746
  • 10
  • 132
  • 141
  • yup.. it has worked :).. thanq.. so did it work because trunc has cleared the contents before opening?? infact i guess ios::out does that?? – bkcpro Sep 04 '12 at 13:12