Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Basic string manipulation

Status
Not open for further replies.

nashcom

MIS
Apr 12, 2002
91
GB
I need to open a text file as input, extract some data from it, saving it to another file.

I'm reading through the source file line by line using something along the lines of:

while (!spoolfile.eof()) {
spoolfile.getline(buff, sizeof(buff));

However, I'm not sure how to extract the data I need.

For example, one line in the file line might look like:

A Order : 1234 B

If I need to extract the '1234' what's the best way to go about it? I'd appreciate two ways of doing it:

i) Assuming the text I need to extract always occurs at the same position in the line (eg at character position 25 for 4 characters)

ii) If the position and length of the text to be extracted could vary. I think I'd need to look for the ':' and then move two spaces to the right, then extract any following chracters until a space occurs.

Sorry, I know it's very basic, but I've just realised it's been 10 years since I last did any serious C/C++ programming, and even then I was bodging things as I went along!

Thanks very much.

 
I'm assuming that buff is a character array. Personally, I would use standard C++ strings or AnsiString if you are using Borland/CodeGear C++.

If you want to use strings I can help but if for some reason you need to use character arrays, I can't. It's been decades since I used those. :)



James P. Cottingham
[sup]I'm number 1,229!
I'm number 1,229![/sup]
 
Hi James

I was using a character array, but it'd be good to come into the 21st century! I'm using C++ Builder 5 Pro, so AnsiString examples would be great.

David
 
OK. Here goes. This is a snippet from one of my programs.
Code:
        std::string ProcessLine;
        while (std::getline(ProcessFileStrm, ProcessLine, '\n'))  // Loop through lines in file
        {
            AnsiString SplitLineStr = ProcessLine.c_str();
            if (SplitLineStr.Length() != 0)  // Data to process
            {
                // Collect the data from the line
                AnsiString RecTypeStr = (SplitLineStr.SubString(2, 1)).Trim();
                AnsiString ProdNumStr = (SplitLineStr.SubString(12, 15)).Trim();
                AnsiString BndlNumStr = (SplitLineStr.SubString(27, 4)).Trim();
                AnsiString SheetCntStr = (SplitLineStr.SubString(31, 2)).Trim();
                AnsiString BndlCMLenStr = (SplitLineStr.SubString(33, 3)).Trim();
                AnsiString BndlCMWidStr = (SplitLineStr.SubString(36, 3)).Trim();
                AnsiString BndlSMStr = (SplitLineStr.SubString(39, 9)).Trim();
                AnsiString ProdSMStr = (SplitLineStr.SubString(48, 9)).Trim();
                AnsiString SpecieStr = (SplitLineStr.SubString(79, 6)).Trim();
                AnsiString PalletStr = (SplitLineStr.SubString(111, 8)).Trim();
                AnsiString BndlInLenStr = (SplitLineStr.SubString(134, 6)).Trim();
                AnsiString BndlInWidStr = (SplitLineStr.SubString(140, 6)).Trim();
                AnsiString BndlSFStr = (SplitLineStr.SubString(146, 9)).Trim();
                AnsiString ProdSFStr = (SplitLineStr.SubString(155, 8)).Trim();


// More processing here

            }                           // Data to process
        }                               // Loop through lines in file

I start with creating a standard string to use in the the getline call.
Code:
std::string ProcessLine;
while (std::getline(ProcessFileStrm, ProcessLine, '\n'))

Since I prefer to use AnsiString's substring calls, I then put the standard string into an AnsString. However, you can use standard string throughout if you prefer.
Code:
AnsiString SplitLineStr = ProcessLine.c_str();

i) If you know the position of the substring, you can call it via AnsiString's substring call as shown. The Trim() only removes any spaces before and after the pulled string.
Code:
AnsiString ProdNumStr = (SplitLineStr.SubString(12, 15)); // starts at 12th character and gets the next 15 characters.
Note that AnsiStrings start at 1 and not 0.

ii) If you need to find a character in the AnsiString, use the Pos method. It will return the position of the character in question.
Code:
//I'm doing this from memory so look at the help files for more help
int XMarksSpot = SplitLineStr.Pos(":");
if (XMarksSpot > 0) // The character exists
{
    AnsiString NewStr = SplitLineStr.SubString(XMarksSpot - 2, 3); // Maybe some error checking?
}

Look at the help file for AnsiString to see its methods.



James P. Cottingham
[sup]I'm number 1,229!
I'm number 1,229![/sup]
 
Thanks very much, James. I'll give that a bash. I'll read your examples and look at the help files, and hopefully get around to coding at the weekend early next week.

David
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top