-
March 22nd, 2009, 03:46 PM
#1
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])
-
March 22nd, 2009, 05:29 PM
#2
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.
Last edited by Tronfi; March 22nd, 2009 at 05:32 PM.
-
March 22nd, 2009, 05:35 PM
#3
Re: Inserting numbers from a .txt file into an array?
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?
Intel Core Duo Macbook w/ Mac OS 10.5.6
gcc 4.2.1 (i386-apple-darwin9.1.0) and Xcode 3.1.1
-
March 22nd, 2009, 05:39 PM
#4
Re: Inserting numbers from a .txt file into an array?
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 . You're totally right.
-
March 22nd, 2009, 05:42 PM
#5
Re: Inserting numbers from a .txt file into an array?
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).
-
March 22nd, 2009, 05:47 PM
#6
Re: Inserting numbers from a .txt file into an array?
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?
Last edited by Etherous; March 22nd, 2009 at 05:52 PM.
Intel Core Duo Macbook w/ Mac OS 10.5.6
gcc 4.2.1 (i386-apple-darwin9.1.0) and Xcode 3.1.1
-
March 22nd, 2009, 05:49 PM
#7
Re: Inserting numbers from a .txt file into an array?
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!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|