-
April 29th, 2011, 05:39 AM
#1
txt file info to array
hi, wondering if someone could provide a snippet that takes info from a txt file & puts it into an array. I need to use cin.get or stream ins.get (over similar simple means); each line has two pieces of data (numbers) separated by a space. So I'm thinking that I need to be able to use a space delimiter. I don't have problems w/ opening file or extracting info. It's just the space delimiter & trying to use cin.get. Because I have to get one char at a time until I hit a whitespace, maybe I should count the char until a whitespace is encountered, then transfer 0-num characters to an array.
I know this can be done w/ printf, but I don't think I'm permitted to use this.
Thnx for any help. I have to work today. So will not be able to provide feedback until later.
-
April 29th, 2011, 05:46 AM
#2
Re: txt file info to array
You should not use get. Just use operator >>
Code:
int v1 , v2;
cin >> v1 >> v2; // or your ifstream object
-
April 29th, 2011, 06:18 AM
#3
Re: txt file info to array
ok, I think "or your ifstream object" is this meant to say "of"? If so, I think I got it. The cin operator will stop at the whitespace as default? & assign my two pieces of data to v1 & v2, then I can put those into my array.
-
April 29th, 2011, 06:21 AM
#4
Re: txt file info to array
If you are reading from a file, you would normally use ifstream, not cin. Unless
you are re-directing input.
Yes, operator >> stops at whitespaces (or illegal character or EOF)
-
April 29th, 2011, 04:50 PM
#5
Re: txt file info to array
Originally Posted by Muffalufagus
hi, wondering if someone could provide a snippet that takes info from a txt file & puts it into an array. I need to use cin.get or stream ins.get (over similar simple means); each line has two pieces of data (numbers) separated by a space. So I'm thinking that I need to be able to use a space delimiter. I don't have problems w/ opening file or extracting info. It's just the space delimiter & trying to use cin.get. Because I have to get one char at a time until I hit a whitespace, maybe I should count the char until a whitespace is encountered, then transfer 0-num characters to an array.
I know this can be done w/ printf, but I don't think I'm permitted to use this.
Thnx for any help. I have to work today. So will not be able to provide feedback until later.
cin will stop at white space. and generally, you can parse each line like
Use ifstream to read file. like
ifstream in("filename");
string line,sub;
while(getline(in,line))
{
/* use line.find_first_of(DELIMINATORS_YOU_USE) to find the locations of the deliminators
use the locations and line.substr to get the substring
like sub=line.substr(STARTPOINT,NUMBER_OF_CHARS)
use atoi or atof to change to number, like atoi(sub.c_str())
*/
}
-
April 29th, 2011, 08:12 PM
#6
Re: txt file info to array
Originally Posted by talentspsp
cin will stop at white space. and generally, you can parse each line like
Use ifstream to read file. like
ifstream in("filename");
string line,sub;
while(getline(in,line))
{
/* use line.find_first_of(DELIMINATORS_YOU_USE) to find the locations of the deliminators
use the locations and line.substr to get the substring
like sub=line.substr(STARTPOINT,NUMBER_OF_CHARS)
use atoi or atof to change to number, like atoi(sub.c_str())
*/
}
Personally I would stuff the line into an istringstream and parse it from there or use boosts lexical_cast. If you must recommend C functions then at least choose the right option. atoi and atof are better replaced by strtol and strtod.
Get Microsoft Visual C++ Express here or CodeBlocks here.
Get STLFilt here to radically improve error messages when using the STL.
Get these two can't live without C++ libraries, BOOST here and Loki here.
Check your code with the Comeau Compiler and FlexeLint for standards compliance and some subtle errors.
Always use [code] code tags [/code] to make code legible and preserve indentation.
Do not ask for help writing destructive software such as viruses, gamehacks, keyloggers and the suchlike.
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
|