Click to See Complete Forum and Search --> : C++ file input


MikeYork18
06-15-2001, 02:07 AM
Say, for example, I have a file with the line

100199_12:30 25 12.50% C

I want to read this line, assign the first value, that is 100199 as a date, 12:30 as a time, and 12.50 as a double, with 25 and C as ints and chars respectively.

I've opened the file successfully, and, if I use a char array, can store both the first item and the third item, as well as assign the 25 and C to their own data types. My question is, how do I "remove" the underscore and % from those other two? Should I read them in differently? Edit the char array somehow?

Any help is greatly appreciated.

kmj
06-15-2001, 07:37 AM
I'm pretty sure fscanf could do this, instead of reading in a char array, but I've never used fscanf, and I hear it's kind of crappy. You say you're doing this in C++; are you using an input stream or are you just using fread (or fgets) to read the data into a char *? In c++, the class 'string' has functions for finding chars w/ in a string, and extracting substrings, which would make your job pretty easy. If you choose not to use that, it still shouldn't be too difficult, espcially if you know that the date is always 6 chars and the time is always 5.

First w/ the double -> '12.50%' let's say it's in a var called percent. You could just do double d_percent = atof(percent); atof is ANSI standard, and will convert a string until the first char it sees that it doesn't recognize as part of a number.

This function will help you copy data from one string to another:

char *strncpy( char *strDest, const char *strSource, size_t count );

For the last bit, you could just do a loop. Something like:


for (int i = 6; i < (strlen(date_time); i++) {
time[i] = date_time[i];
}