1

Currently working on a snippet to input variables in which a user changes a text file to be used later. Storing these in an array, and later referencing them for some openGL.

The input text file looks something like this.

something = 18.0;

something else = 23.4;

... 6 lines total

//the variable of type ifstream:
ifstream patientInput(".../Patient1.txt");
double n[6]= {0.0,0.0,0.0,0.0,0.0,0.0};
register int i=0;
string line;
//check to see if the file is opened:
 if (patientInput) printf("Patient File Successfully Opened.\n");

else printf("Unable to open patient file\n");

 while(!patientInput.eof())
 {
    getline(patientInput,line);
    char *ptr, *buf;
    buf = new char[line.size() + 1];
    strcpy(buf, line.c_str());
    n[i]=strtod(strtok(buf, ";"), NULL);
    printf("%f\n",n[i]);
    i++;
 }
//close the stream:
patientInput.close();

Right now it is saving all the values in the array as initialized but not overwriting them later, as it should when I am breaking the lines into the tokens. Any help is appreciated.

3
  • Why the random mixing of iostreams and C-style IO? Commented Apr 29, 2011 at 18:32
  • 3
    This doesn't fix your problem, but why are you mixing C++ and C-style strings? For a start, you've engineered yourself a memory leak (where does the memory pointed to buf go?). Commented Apr 29, 2011 at 18:33
  • I see that however to use strtok I have to have create buf to convert to a char from a string. Not sure of any other way to fix this.. Also any other ideas for fixing the problem? Commented Apr 29, 2011 at 18:50

2 Answers 2

1

It looks to me like the bug is here:

n[i]=strtod(strtok(buf, ";"), NULL);

On the first run through the while loop, strtok() will return a C string like "something = 18.0".

And then strtod() will try to convert that to a double, but the string "something = 18.0" is not so easily converted to a double. You'll want to tokenize the initial "something =" first, and throw that data out if necessary (or do something with it, if you want to).

You may want to refer to this thread to get ideas for some more C++-style ways to tokenize your string, instead of C-style like you're currently using:

How do I tokenize a string in C++?

Good luck!

Sign up to request clarification or add additional context in comments.

Comments

0

To apply what NattyBumppo had to say just change:

n[i]=strtod(strtok(buf, ";"), NULL);

to:

strtok(buf," =");
n[i] = strtod(strtok(NULL, " ;"), NULL);
delete buf;

Of course, there are a lot of other ways to do it without strtok.

Here's one example:

ifstream input;
input.open("temp.txt", ios::in);
if( !input.good() )
    cout << "input not opened successfully";

while( !input.eof() )
{
    double n = -1;
    while( input.get() != '=' && !input.eof() );

    input >> n;
    if( input.good() )
        cout << n << endl;
    else
        input.clear();

    while( input.get() != '\n' && !input.eof() );
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.