Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How can you put a file that you input into ur function into an array?

Status
Not open for further replies.

What1234

Programmer
May 16, 2005
1
AU
Please help me im struggling with this.. i have to input a file into my program and i need to put the contents of that file into an array....
the contents of the file are in the format of like y, 5, 11 , PP
How do i do this??
PLEASE HELP MEHH>

thanks.
 
From what it sounds like the data that needs to be placed into the array is of type <string>.

I am assuming the file can change in size so there is no point in using a standard size array. You should use a <vector>, which is a resizable array

Code:
[blue]
#include <vector>  [green]//Place this with the rest of the includes [/green]
vector <string> myData; [green]//A vector of strings[/green]

[green]//Do some file reading stuff here
You are probably using fstream?
So reading the file would be like:
[/green]

while(myFile.eof()!)
{
   myStream.getline(SomeCharBuffer,1000);
   [green]//As you are reading data in you can push things on to the back of the vector[/green]

   myData.push_back(SomeCharBuffer);

}

[green]//When you are done you can iterate through the vector just as you could with a standard array [/green]
for (int i =0;i<(int)myData.size();i++) cout << myData[i] << endl;


[/blue]


I hope that helps!

-Ron






typedef map<GiantX,gold, less<std::shortestpathtogold> > AwesomeMap;
 
I think this should work.
Code:
#include <fstream>
using std::ifstream;

#include <vector>
using std::vector;

#include <string>
using std::string;

int main()
{
   ifstream fileIn( "C:\\myfile.txt" );
   string buf;
   vector<string> myArray;

   while ( fileIn.eof() == false )
   {
      fileIn.get( buf, ',' );
      myArray.push_back( buf );
   }

   fileIn.close();

   // Now do something with the array...
   return 0;
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top