Inserting numbers from a .txt file into an array?
Hey all, I'm pretty much a newbie at this kind of thing so go easy on me :)
I'm writing a program that takes values from a .txt file and inserts them as input into array elements.
For example, within the .txt file is "244900422900"
Using an ifstream object, is there a way to read the first "900" only (ignoring the 244) and input that into the first element of my duration array below?
BEEP (400, duration[durationCount])
Re: Inserting numbers from a .txt file into an array?
To move to a specific place, use seekg. With that and a counter, you will be able to read just the first "900". Probably, you will have to cast it to an int.
Other way: ignore.
Re: Inserting numbers from a .txt file into an array?
Quote:
Originally Posted by
Tronfi
Code:
ifstream f("myfiletext.txt");
char a;
int i=0;
while(f>>a){
myarray[i]=a;
i++;
}
I actually prefer to use the 'read' method of istream to get a fixed length block of data and then do whatever processing on it afterward, for performance reasons, but this here is the general idea.
Clarification: Did you want to skip the first 3 characters, whatever they may be, or did you want to find the first occurrence of 244, then take the next 3 characters?
Re: Inserting numbers from a .txt file into an array?
Quote:
Originally Posted by
Etherous
I actually prefer to use the 'read' method of istream to get a fixed length block of data and then do whatever processing on it afterward, for performance reasons, but this here is the general idea.
I realised what i've written after post it. I edit it quickly, but you were faster :cool: . You're totally right.
Re: Inserting numbers from a .txt file into an array?
Quote:
Originally Posted by
Etherous
I actually prefer to use the 'read' method of istream to get a fixed length block of data and then do whatever processing on it afterward, for performance reasons, but this here is the general idea.
Clarification: Did you want to skip the first 3 characters, whatever they may be, or did you want to find the first occurrence of 244, then take the next 3 characters?
The former, that is, I'm looking for something that will skip the first three characters (whatever they may be) and input the next three after that (whatever they may be).
Re: Inserting numbers from a .txt file into an array?
Quote:
Originally Posted by
Ulnarian
The former, that is, I'm looking for something that will skip the first three characters (whatever they may be) and input the next three after that (whatever they may be).
In that case, this would probably be fastest:
Code:
// Variables
ifstream file(filename,ios::in);
char instr[7];
// Get first 6 characters "244900"
file.read(instr,6);
// We're done with the file now?
file.close();
// Make string valid, by putting end point
instr[6] = '\0';
// Convert the string starting from '9' to an integer and set the first element of duration to that number
duration[0] = atoi(instr[3]);
Is this what you wanted?
Re: Inserting numbers from a .txt file into an array?
Quote:
Originally Posted by
Etherous
In that case, this would probably be fastest:
Code:
ifstream file(filename,ios::in);
char* instr[7];
file.read(instr,6);
file.close();
instr[6] = '\0';
duration[0] = atoi(instr[3]);
Is this what you wanted?
Possibly, but I'll need some time to digest that :) (I'm pretty newbish at this programming stuff)
Thanks!