vector

This is a discussion on vector within the C++ Programming forums, part of the General Programming Boards category; You've done other things wrong too but I don't have time to cover them right now, sorry. I hope I ...

  1. #16
    Registered User
    Join Date
    Sep 2010
    Posts
    90
    You've done other things wrong too but I don't have time to cover them right now, sorry. I hope I helped, anyway.
    I hope common sense dictates to all of you that this is not a school project. I'll be into this for months, right here. I will be following Thread #15 until I understand it through and through and I will fix all problems. C++ is 25 years old and it was not written over night. I'm sure it will takes me a week or two longer to catch-on. Thanks for that whiteflags. Who want to live in rotten code forever and never know it and worse yet who else would have notice on to-date??? IOU a quad-triple THANKS

    You're not going to like the answer
    Is this a shout-out or are you saying that even the experts don't know how to re-arrange my tiny code and make it out-put only a few vector-strings to a simple textfile. Please tell me this is some kind of joke.

    But again, we're trying to be cool for no real reason.
    I'm sorry here but that's no excuse. Even if never used, when coders notice something strange, you don't run away to hide the fact, you run into it, head-on. Talking the "Why Not" .. "What For" and "Why Would You" don't cut the mustard.

    Anyway, I'll fix what is here if it can be done. I'm sure C++ can't be that big of a c-blocker to ALL work-arounds. There is more information in this thread that you ever find anywhere else about this so I Thank you all but don't stop now... I don't plan to.

  2. #17
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,740
    Is this a shout-out or are you saying that even the experts don't know how to re-arrange my tiny code and make it out-put only a few vector-strings to a simple textfile. Please tell me this is some kind of joke.
    Not a joke. As far as I know, opening binary files for reading or writing is considered unformatted input or output, because no interpretation is made on the characters (bytes) of input or output. This is important for binary files because you just want what is there: You don't need to format bytes into some other data type to make them usable.

    Unfortunately this means that you are restricted to input functions and output functions that work on unformatted data, like write() and read(). Stream iterators are not written in terms of anything that would be OK, so you can not use them. By extension, you cannot write to or read from the stream with std::copy.

    You gotta try something else. Using vector<char> as a processing buffer could work well. Or, if your binary files are record based, (which means that your file is basically the bytes of several objects stored one after another) you could read() directly into an object or write() objects directly to the file. It all depends on what can be serialized to your file.

    Code:
    // provided for your benefit
    
    #include <fstream>
    #include <iostream>
    #include <vector>
    #include <string>
    
    class Data
    {
    private:
        int x;
        std::string y;
    
    public:
        Data()
        {
            //nothin
        }
        Data( int my_x, std::string my_y ):x(my_x), y(my_y)
        {
            //nothin
        }
        void display (std::ostream &out) const
        {
            out << x << " : " << y << '\n';
        }
    };
    
    int main()
    {
        Data one(1, "one");
        Data two(2, "two");
        Data three(3, "three");
    
        std::vector<Data> elems;
        elems.push_back(one);
        elems.push_back(two);
        elems.push_back(three);
        elems.push_back(one);
        elems.push_back(two);
        elems.push_back(three);
    
        std::fstream example;
        example.open("stuff.bin", std::ios_base::out | std::ios_base::binary);
        if( example.is_open() ) {
            for( std::vector<Data>::iterator it = elems.begin(); it != elems.end(); it++ ) {
                example.write((char*)&*it, sizeof *it);
            }
            example.close();
        }
        example.open("stuff.bin", std::ios_base::in | std::ios_base::binary);
        if( example.is_open() ) {
            Data d;
            while( example.read((char*)&d, sizeof d) ) {
                d.display(std::cout);
            }
            example.close();
        }
        return 0;
    }
    This will produce a stuff.bin file which you can throw away. Sorry.

  3. #18
    Registered User
    Join Date
    Sep 2010
    Posts
    90
    Just when I was about to give-in. You work fast Elysia... I just notice your post. The first example still dies at the for-loop
    Line 42: Error: C++ ISO forbids declaration of "it" with no type ... with a few others reports for the same line.
    I think I understand but I wonder why it compile for you and not me.

    What I don't understand ... what is "textfile?" doing or holding? I getting ready to google about it.
    Code:
    v_S.push_back("textfile?");
    Also, your code is based on my original way of thinking until I realize I may need to read-in an external file sometimes. You keep avoiding that. Why?

    But still, code sample-2 works perfectly and I have a lot of code already that been trying to use it. That section of my personal-project is basically, completed.

    I prefer reading a external file into vector dimensions (i bet that is the key) but I'll be happy with what I can get. Reading a file into vector string was a great accomplishment and it was not easy so I don't want to throw it away. I be working on all of this tomorrow, I am totally worn out again. It's all in here somewhere including every word said. I just notice whiteflag post. I'll take it from the top and read myself to sleep, 10x. Thanks so much. I believe nothing about vector is totally impossible! He got friends that need to be used somehow.

  4. #19
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,740
    As usual I made a mistake.

    I perhaps should mention that most objects will not be able to be serialized since they likely use pointers to some degree. Such is the case with std::string, and you have to write the data and not their buffer pointers to a binary file.

    With the smallest changes possible, this certainly works:
    Code:
    #include <fstream>
    #include <iostream>
    #include <vector>
    
    class Data
    {
        int x;
        char y;
    
    public:
        Data()
        {
            //nothin
        }
        Data( int my_x, char my_y ):x(my_x), y(my_y)
        {
            //nothin
        }
        void display (std::ostream &out) const
        {
            out << x << " : " << y << '\n';
        }
    };
    
    int main()
    {
        Data one(1, '1');
        Data two(2, '2');
        Data three(3, '3');
    
        std::vector<Data> elems;
        elems.push_back(one);
        elems.push_back(two);
        elems.push_back(three);
        elems.push_back(one);
        elems.push_back(two);
        elems.push_back(three);
    
        std::fstream example;
        example.open("stuff.bin", std::ios_base::out | std::ios_base::binary);
        if( example.is_open() ) {
            for( std::vector<Data>::iterator it = elems.begin(); it != elems.end(); it++ ) {
                example.write((char*)&*it, sizeof *it);
            }
            example.close();
        }
        example.open("stuff.bin", std::ios_base::in | std::ios_base::binary);
        if( example.is_open() ) {
            Data d;
            while( example.read((char*)&d, sizeof d) ) {
                d.display(std::cout);
            }
            example.close();
        }
        return 0;
    }

  5. #20
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,789
    Quote Originally Posted by sharris View Post
    Just when I was about to give-in. You work fast Elysia... I just notice your post. The first example still dies at the for-loop

    I think I understand but I wonder why it compile for you and not me.
    You need to enable C++0x mode. Depends on compiler, but Visual C++ and GCC both support it.

    What I don't understand ... what is "textfile?" doing or holding? I getting ready to google about it.
    Code:
    v_S.push_back("textfile?");
    Nothing special. It's just a string pushed back into a vector.

    Also, your code is based on my original way of thinking until I realize I may need to read-in an external file sometimes. You keep avoiding that. Why?
    Because I'm lazy.

    A final example:
    Code:
    #include <string>
    #include <fstream>
    #include <vector>
    #include <iterator>
    #include <iostream>
    #include <algorithm>
    
    int main() 
    {
    	std::vector<std::string> v_S;
    
    	v_S.push_back("Success ");
    	v_S.push_back("words ");
    	v_S.push_back("are ");
    	v_S.push_back("sorted ");
    	v_S.push_back("from ");
    	v_S.push_back("a ");
    	v_S.push_back("textfile:\n\n");
    
    	v_S.push_back("\n\n ... ");
    	v_S.push_back("but ");
    	v_S.push_back("how ");
    	v_S.push_back("do ");
    	v_S.push_back("I ");
    	v_S.push_back("save ");
    	v_S.push_back("it ");
    	v_S.push_back("to ");
    	v_S.push_back("a ");
    	v_S.push_back("textfile?");
    
    	std::ofstream outfile("sorted.txt");
    	std::ostream_iterator<std::string> I_O(outfile, " ");
    	std::copy(v_S.begin(), v_S.end(), I_O);
    	outfile.close();
    	v_S.clear();
    
    	std::ifstream infile("sorted.txt");
    	std::istream_iterator<std::string> in_it(infile);
    	std::istream_iterator<std::string> in_end;
    	std::copy(in_it, in_end, std::back_inserter(v_S));
    	
    	std::ostream_iterator<std::string> out_it(std::cout, " ");
    	std::copy(v_S.begin(), v_S.end(), out_it);
    	return 0;
    }
    Again, note two things here:
    - Whitespace is skipped (you don't see newlines in the out, for example).
    - It only works with text-mode files. That is, no binary.
    So it has big drawbacks. It has its uses, but it's not universal.

    If you want to dump objects into files (pretty much binary), I'd recommend Boost's serialization library for that. I can make examples of that, if you want.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. can some one please tell me the cause of the error ?
    By broli86 in forum C Programming
    Replies: 8
    Last Post: 06-26-2008, 08:36 PM
  2. syntax help?
    By scoobygoo in forum C++ Programming
    Replies: 1
    Last Post: 08-07-2007, 10:38 AM
  3. Vector class
    By Desolation in forum C++ Programming
    Replies: 2
    Last Post: 05-12-2007, 05:44 PM
  4. Need some help/advise for Public/Private classes
    By nirali35 in forum C++ Programming
    Replies: 8
    Last Post: 09-23-2006, 12:34 PM
  5. Certain functions
    By Lurker in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2003, 12:26 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21