Array of Strings

This is a discussion on Array of Strings within the C++ Programming forums, part of the General Programming Boards category; All, I have a problem where my program just hangs on a certain line, and I can't figure out why. ...

  1. #1
    Registered User
    Join Date
    Aug 2009
    Posts
    3

    Question Array of Strings

    All, I have a problem where my program just hangs on a certain line, and I can't figure out why.

    It's a command line app where it reads in a file and based on the user's arguments to the program, it will split the file into X seperate files.

    To do this I read each line of the file using getline and stuff it into a string (not char) using string.append(). As I'm reading the file, I'm filtering out
    certain values based on user args and duplicate rows.

    After I've read the file in, I want to split it into X seperate files. First, I define an Array of Strings using malloc:
    Code:
    	string *aOutputFiles;
    	if ((aOutputFiles = (string *) malloc ( intTempNumberFiles * sizeof *aOutputFiles )) == NULL) {
    		cerr << "\nError! Not enough memory to allocate buffer\n";
    		exit(EXIT_FAILURE);
    	}
    Then I proceed to loop thru the string finding each NewLine value and stuffing it into the String Array:
    Code:
    	intIterator = 0;
    	//now split the input file into multiple pieces
    	if (strInputFileContents.length() > 0) {
    		szFind=strInputFileContents.find_first_of("\n");
    		while (szFind!=string::npos) {
    			aOutputFiles[intIterator].append(strInputFileContents,0,(szFind+1)); //<--this is the line that breaks
    //but not all the time...and if it does it's usually on the 2nd iteration of the loop
    			strInputFileContents.erase(0,(szFind+1));
    			szFind=strInputFileContents.find_first_of("\n");
    			if ( intIterator >= (intTempNumberFiles - 1) ) {
    				intIterator = 0;
    			} else {
    				intIterator = intIterator + 1;
    			}
    		}
    	}
    Any ideas?

    Also, if there's a better/faster way to do this please let me know also...

    Thanks,

    Brad

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    I'm afraid that you can't allocate an array of strings with malloc. malloc just gives you enough memory for x strings, but it doesn't mean you'll have any actual usable string objects - that memory just contains garbage. The C++ equivalent of malloc that also constructs the object(s) is new/new[].

    However, in C++ one would really just use a std::vector<std::string>.

    Next, wouldn't it be simpler to read a line with getline and append it to a suitable string?

    Technically, you could use string::find instead of find_first_of (which means find any of the given characters), and there's lots of expensive erasing going on (use a version of find that takes position to start search from + substr to extract lines?)
    Last edited by anon; 09-15-2009 at 05:01 PM.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    Registered User
    Join Date
    Aug 2009
    Posts
    3

    Many Thanks!

    Anon,

    Thank you for your advise & input!! It worked!!

    I'm so used to using arrays in other languages, but vectors sure make things easier in C++.

    Brad

  4. #4
    Registered User
    Join Date
    Aug 2009
    Posts
    3

    Thumbs up Lists Too!!

    Guys, after seeing what vectors could do for me, I looked at some of the other
    containers that C++ had to offer and I found some neat features with lists.
    They can sort easily and output unique values. This is great for my app!

    Anon, you Rock!!

    Many Thanks again!!

    Brad

  5. #5
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Vectors sort easily and can output unique values too (look at <algorithm> header, list only provides those as methods, because they can be implemented (more) efficiently if you have access to implementation details of a linked list).
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. matching of names in 2d array of strings...
    By roaan in forum C Programming
    Replies: 6
    Last Post: 07-25-2009, 10:59 AM
  2. intializing an array of strings
    By doubty in forum C Programming
    Replies: 4
    Last Post: 06-19-2009, 01:59 PM
  3. Replies: 1
    Last Post: 03-19-2009, 11:56 AM
  4. Build an array of strings dynamically
    By Nazgulled in forum C Programming
    Replies: 29
    Last Post: 04-07-2007, 10:35 PM
  5. Array of strings in C
    By szill in forum C Programming
    Replies: 10
    Last Post: 02-22-2005, 05:03 PM

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