Thread: substring of character arrays?

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    17

    substring of character arrays?

    I'm using the following code to put the contents of a a file into a 2D array.

    Code:
    getline(readFile, line, '\n'); 
    	while (!readFile.eof())
    	{
    	strcpy(getTheLine[s], line.c_str()); 
    	getline(readFile, line, '\n');
    	s++; 
    	}
    This is working fine for my purposes, but I can't figure out how to do any type of "substr" operations on the array. I want to extract the first 8 characters of each getTheLine[s] component and display it. As of now, I'm using something similar to

    Code:
    for...
    cout << getTheLines[i][0] << getTheLines[i][1] << getTheLines[i][2]
    which looks horrible. There has to be a better way to do it? Thanks in advance.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    So, you are reading into a C++ std::string, then copying to a character array. Is this actually part of your assignment to use C style strings (character arrays), rather than C++ style strings?

    C++ style stings do have a substr() function.

    Otherwise, if you insist on using C style strings, just setting the 9th position of the string (index 8) to zero ('\0' or 0) will make the string a maximum of 8 characters long.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Oct 2008
    Posts
    17
    Quote Originally Posted by matsp View Post
    So, you are reading into a C++ std::string, then copying to a character array. Is this actually part of your assignment to use C style strings (character arrays), rather than C++ style strings?

    C++ style stings do have a substr() function.

    Otherwise, if you insist on using C style strings, just setting the 9th position of the string (index 8) to zero ('\0' or 0) will make the string a maximum of 8 characters long.

    --
    Mats
    Well, it's not technically part of the assignment. I just wanted to fool around with the 2d arrays because half of the chapter is about them. My program is working fine now, but it's just the section where I display the studentID, I have to call on 8 different specific elements from the array.

    I know that substr() doesn't work on cstrings. The closest I've found was 'strncpy' which copies a number of chars from a cstring to another cstring array - but it delivers garbled characters that are not in the file for some reason. Do you have any idea why?

    I've tried setting the horizontal index on another 2d array, but that doesn't work.
    Code:
    char b[lines][8];
    strcpy(b[s], line.c_str());
    That actually reads the entire file and displays it.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by misterFry View Post
    The closest I've found was 'strncpy' which copies a number of chars from a cstring to another cstring array - but it delivers garbled characters that are not in the file for some reason. Do you have any idea why?
    Because it doesn't. Or if it does, it would be because you managed to not null-terminate your string, so that the system is unable to determine where to stop.
    I've tried setting the horizontal index on another 2d array, but that doesn't work.
    Code:
    char b[lines][8];
    strcpy(b[s], line.c_str());
    Here, b[s] only has room for seven characters; if you want eight, you're going to have to make that second dimension nine. And to use strncpy, you would just do
    Code:
    strncpy(b[s], line.c_str(), 8);
    b[s][8] = '\0';

  5. #5
    Registered User
    Join Date
    Oct 2008
    Posts
    17
    Ok, thanks for the replies. After nothing else worked, I just used a for loop to get what I needed from the array.

    Code:
    int i;                          
    for (i = 0; i <= 8; i++){
    cout << array[s][i];}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Manipulating Character arrays in functions.
    By kbro3 in forum C++ Programming
    Replies: 11
    Last Post: 08-16-2008, 02:24 AM
  2. Character array substring.
    By megatron09 in forum C++ Programming
    Replies: 12
    Last Post: 09-10-2006, 11:53 AM
  3. multidimentional character arrays
    By ckeener in forum C++ Programming
    Replies: 7
    Last Post: 03-11-2005, 08:49 PM
  4. character substring
    By Shawty in forum C++ Programming
    Replies: 2
    Last Post: 11-14-2002, 09:57 PM
  5. help with character arrays
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 03-28-2002, 06:13 PM