Thread: Reading Strings from a file.

  1. #16
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    If you try to return a pointer to the vector's data instead of returning the vector itself, you will have the same problem as before. The vector will be destroyed and the pointer will be invalid.

    I think you're going to have to use new[]/delete[].

  2. #17
    Registered User
    Join Date
    May 2007
    Posts
    88
    > I think you're going to have to use new[]/delete[].

    That works, or if you don't want to mess with dynamic memory, I think using a static array might be a viable option here as well.

    Code:
    URL_Keyword_Mapping* read(std::ifstream& ifile, int& length) {
    
    	//Add the static identifier
    	static URL_Keyword_Mapping UKM_ARRAY[50];
    
             //...
    
    	return UKM_ARRAY;
    }
    There are some complications with this method, though. Consider the following code:
    Code:
    URL_Keyword_Mapping* foo, *bar;
    
    foo = read(blah, blah);
    bar = read(blah, blah);
    After this, foo and bar will point to the same array (the array that was written the second time read was called). If you want the original value of foo to be conserved, you'd have to declare it as an array and use memcpy.

    The other alternative is to use a vector and to return the object itself, which, if efficiency isn't a concern, is probably the best option.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with reading strings from a file
    By ldcel in forum C Programming
    Replies: 3
    Last Post: 12-01-2007, 01:31 AM
  2. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  3. Simple File encryption
    By caroundw5h in forum C Programming
    Replies: 2
    Last Post: 10-13-2004, 10:51 PM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM