#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    7
    Rep Power
    0

    Put std:string to array


    Hello I'm new in C++ and in this forum. I need your help on placing std strings into array.

    I used the following in declaring array of string but none of them seem to work. I have 10 std strings and I want to put them in an array.

    Code:
    string mystringcollection[10]; //I don't know which of the following
    char *mystringcollection[10]; //is the correct declaration
    Also how do I go back, or read in a part in file after the while statement below. Because when I try to read in a line after the while statement, it doesn't read in anything ( I think it has already reached the end of file) but I need to get access to that part of the file.

    Code:
    	while (getline(infile,line)) { //get next line of input file
    	//search for something in file and do something
    }
    //go back to next part of file
    getline(infile, line); outfile<<line; //outputs nothing
    Thanks in advance for your suggestion/help.
  2. #2
  3. /(bb|[^b]{2})/

    Join Date
    Nov 2001
    Location
    Somewhere in the great unknown
    Posts
    5,163
    Rep Power
    793
    Here is a sample program to help explain how to use pointers for multiple strings in C++. There are multiple ways to do this, this is just one simple way.

    PHP Code:
    #include <iomanip>
    #include <fstream>
    #include <cstdlib>
    #include <cstring>
    #include <cstddef>

    using namespace std;

    #define MAX_LINES     10
    #define MAX_BUF        128
    #define FNAME        "./test.txt"

    int main(void) {
        
    char *cLines[MAX_LINES 1];
        
    char *cTemp;
        
    int iCntr=0;
        
    ifstream fp;

        
    /* allocate memory for the strings */
        
    for(iCntr=0iCntr <= MAX_LINESiCntr++) {
            
    cLines[iCntr] = new char[MAX_BUF 1];
            if (
    NULL == cLines[iCntr]) {
                
    cerr << "Error allocating memory for cLines # " << iCntr << endl;
                return (
    2);
            }
        }
        
    cTemp = new char[MAX_BUF 1];
        if (
    NULL == cTemp) {
            
    cerr << "Error allocating memory for temp string." << endl;
            return (
    2);
        }
        
        
    fp.open(FNAME);
        if (
    fp.fail()) {
            
    cerr << "Error opening file: " << FNAME << endl;
            for(
    iCntr=0iCntr MAX_LINESiCntr++) {
                if (
    NULL != cLines[iCntr]) {
                    
    delete cLines[iCntr];
                }
                if (
    NULL != cTemp) {
                    
    delete cTemp;
                }
            }
            return(
    1);
        }
        
    iCntr 0;
        while (!
    fp.eof()) {
            if (
    iCntr MAX_LINES) {
                
    fp.getline(cLines[iCntr++], MAX_BUF);
                
    cout << "Line # " << iCntr << " = " << cLines[iCntr 1] << endl;
            } else {
                
    fp.getline(cTempMAX_BUF);
                
    cout << "Extra line: " << cTemp << endl;
            }
        }
        
        
    fp.close();

        
    /* free our memory */
        
    if (NULL != cTemp) {
            
    delete cTemp;
        }
        for (
    iCntr 0iCntr <= MAX_LINESiCntr++) {
            if (
    NULL != cLines[iCntr]) {
                
    delete cLines[iCntr];
            }
        }

        return(
    0);
        
    /* end of program */

    Last edited by Onslaught; July 8th, 2003 at 03:22 PM.
  4. #3
  5. No Profile Picture
    status unknown
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Posts
    262
    Rep Power
    12
    Originally posted by Onslaught
    #define MAX_LINES 10
    char *cLines[MAX_LINES];
    /* allocate memory for the strings */
    for(iCntr=0; iCntr <= MAX_LINES; iCntr++) {
    Woops! Should be:
    for(iCntr=0; iCntr < MAX_LINES; iCntr++) {

    ;-)
  6. #4
  7. /(bb|[^b]{2})/

    Join Date
    Nov 2001
    Location
    Somewhere in the great unknown
    Posts
    5,163
    Rep Power
    793
    No, it should be <=
    This is an array of arrays, not a single array. Although I actually allocate memory for this element (11) I don't actually use it, but I could if I so desired because I allocate memory for each one.
  8. #5
  9. No Profile Picture
    status unknown
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Posts
    262
    Rep Power
    12
    > This is an array of arrays, not a single array.
    No it isn't. It's a single array with 10 elements, indexed from 0 to 9. It's an array of pointers to char. In other words, our compiler will allocate enough storage for 10 pointers to characters.

    Code:
    #define MAX_LINES     10
    #define MAX_BUF        128
    #define FNAME        "./test.txt"
    
    int main(void) {
        char *cLines[MAX_LINES];
    Here you declare the array of MAX_LINES pointers to char.

    Code:
        int iCntr=0;
        ifstream fp;
    
        /* allocate memory for the strings */
        for(iCntr=0; iCntr <= MAX_LINES; iCntr++) {
            cLines[iCntr] = new char[MAX_BUF + 1];
            if (NULL == cLines[iCntr]) {
                cerr << "Error allocating memory for cLines # " << iCntr << endl;
                return (2);
            }
        }
    The loop above iterates 11 times, for values of iCntr from zero to 10 (<= MAX_LINES). For each iteration the following statement is executed:

    > cLines[iCntr] = new char[MAX_BUF + 1];

    During the final iteration this stores a pointer to the newly created character array in cLines[10], which is outside of the bounds of the array declared earlier.
  10. #6
  11. /(bb|[^b]{2})/

    Join Date
    Nov 2001
    Location
    Somewhere in the great unknown
    Posts
    5,163
    Rep Power
    793
    Ok chief, whatever you say. I'm not going to argue with you. Your right, your tha man. I will change my code above to reflect your request.
  12. #7
  13. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    7
    Rep Power
    0

    thanks


    Thanks a lot for your help. Anyway, I decided to put my strings into a vector of strings because it will be easier. But I very much appreciate your suggestions here. I could use it in another part of my program.

    Anyway I got stuck in another problem in my code. I need to construct a loop that does something until program finds "loc" character. Here's the code.

    Code:
    getline(infile,line);
    int i=0;
    string::size_type locpos = line.find(loc[i]); //search for "loc" string in line
    while () { //do this until find character "loc"		
    	int i=0;
    	string::size_type calpos = line.find(cal[i]); /look for "cal" string in line	
            if(calpos != std::string::npos) {
    		string calext(".CAL"); 
    		string calfile = line.substr(0, calpos)+calext; //add .cal to string filename
    		calvec.push_back(calfile); //the part where I store strings inside vector
    	}
    }
    Last edited by C++ Padua; July 9th, 2003 at 12:21 AM.
  14. #8
  15. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    7
    Rep Power
    0
    I figured it out. To those who may encounter the same problem
    Code:
    string::size_type locpos;
    	do {
    //do something
    	}while (locpos == std::string::npos);

IMN logo majestic logo threadwatch logo seochat tools logo