tags:

views:

121

answers:

3
string GetLine()
{
char parameter[26] = {NULL};

inFile.getline (parameter,26,' ');

return parameter;
}

Now an example of my input file looks like this:

~in.txt~

BAC BCA(space after the last A)

~End File~

I have to have that space after the A or else my function to get line won't work. Is there a way to not have a space after the A and still get it to work?

I have 26, because the input line will only have up to 26 letters in it.

I need to have them separated like I have it because this is how I use it:

string in, post;
in = GetLine();
post = GetLine();

Thanks for any suggestions on this, this is very small chunk of code for the program i'm still working on. I just wanna cover my bases because my Professor is testing this program with his own input file and I don't know if his input file will end with a space.

A: 

If you know that in and post will have the same length, then here is a solution:

Give GetLine() a char parameter, say delim, that determines the delimiter character.

string GetLine(char delim=' ')

And have it used in the getline call:

inFile.getline (parameter,26,delim);

Then read the lines like this:

string in, post;
in = GetLine(' ');
post = GetLine('\n');

EDIT:

If you don't know whether their will be a space at the end or not, use this:

string GetLine()
{
char parameter[26] = {NULL};

inFile.getline (parameter,26,' ');

string str = parameter;

if (str[str.length()-1]==' ') str.resize(str.length()-1);

return str;
}
Alexander Rafferty
It works...but only if there isn't a space afterwards...Sorry but i should have been more specific i guess. I want it to work with or without the space at the end, just to be safe
MOneAtt
+2  A: 

Perhaps you should just get the line allowing \n to be the delimiter and then just iterate through and tokenize the input by spaces.

Something like this is a much smarter way to do this:

ifstream file(filename);
string   line;
if (file)
{
    string token;
    stringstream iss;
    while ( getline(file, line) )
    {
        iss << line;
        while ( getline(iss, token, ' ') )
        {
            cout << token << endl;
        }
        iss.clear();
    }
}

The EOF and getline don't get along terribly well, so I found this online a few semesters ago when working on a simple parsing problem.

iand675
You should have at least provided the reference - http://www.daniweb.com/forums/thread53349.htmlThe code is exactly the same.
Vlad Lazarenko
I did say that I found it a few semesters ago. I didn't remember where, and it was easier to find on my computer than online.
iand675
+3  A: 

This is kind of a silly redundant function, and I don't know why you would call it "GetLine", but here ya go:

string GetLine()
{
    string s;
    infile >> s;
    return s;
}
PigBen
This doesn't get a line, it gets a string up until some white-space. That is, given "ABC DEF\n", this returns "ABC".
GMan
@GMan Yeah, I know. If you read his question, and his comments, you'll see that that's exactly what he asked for.
PigBen
Alright, I took the -1 off since I honestly skimmed the question. :)
GMan
Thanks... wow that is very simple and it works like i need it to!
MOneAtt
MOneAtt
It's because the >> operator is optimized to work with text files. It works very intuitively for most purposes. It treats whitespace sort of the same way a c++ compiler does. That is to say, you need at least one space(or tab or newline) to separate words. But after that, it's all up to your own taste. You could have a million newlines and they would all be ignored.
PigBen