Sign in to follow this  
raptorstrike

reading in an array from a .txt file

Recommended Posts

ok how would you go about storing an array from a .txt file into an array in your program? Note:(i do know how to read files just not how to seperate the file into data types) could you do somthing like this Array[] = String; if the string is {5,5,45,6,457,4,5,645,734,7,646,346,437,34,637,3,7,37,3,73,73,7,357,3,743,7,5,};? (or whatever)

Share this post


Link to post
Share on other sites
Okay then. Here take a look at this link. It's the documentation for the fstream class. You need to include fstream.h, and it's under the namespace std.

http://www.cplusplus.com/ref/iostream/fstream/index.html

Share this post


Link to post
Share on other sites
that dosnt exactly help me. im trying to get a file thats going to = a int ARRAY[] so i need to do somthing like this fin >> ARRAY[] but when i do this


#include <stdlib.h> // Include this to use system()
#include <iostream> // Include our standard header
#include <string> // Include this to use strings
#include <fstream>
using namespace std;

int main()
{
ifstream hi;
string ok;
int array[900];
hi.open("ok.txt");
while(getline(hi, ok))
hi >> array[900];
std::cout << array[900];
system("pause");
};



i get the output of
4442316
Press any key to continue...

and my txt file contains this

{9,0};

so i dont know were 4442316 is comming from and why its not reading my file right

Share this post


Link to post
Share on other sites
Quote:
Original post by raptorstrike
that dosnt exactly help me. im trying to get a file thats going to = a int ARRAY[] so i need to do somthing like this fin >> ARRAY[] but when i do this

*** Source Snippet Removed ***

i get the output of
4442316
Press any key to continue...

and my txt file contains this

{9,0};

so i dont know were 4442316 is comming from and why its not reading my file right


Okay, first off, you don't need to keep specifying that your array is 900. Just specify the 900 part the first time you declare it.

Also, when you make your array, it's filled with whatever was in memory before that. Give me a few minutes to type up an example. It would help abit if you posted the entire text file so I can look at it. Why would you need a 900 large array for 2 numbers?

Share this post


Link to post
Share on other sites
ok that is the whole text file and im just trying to be accurate here so i made a 900 file because thats how many (maby more) are going to be in the real thing also it just hit me that im going to be using a 2d array this is going to get complicated (you know i could just copy and paste my .txt file into my array decleration (that would be a whole lot easyer)

Share this post


Link to post
Share on other sites
Quote:
Original post by Vampyre_Dark
Okay then. Here take a look at this link. It's the documentation for the fstream class. You need to include fstream.h, and it's under the namespace std.

http://www.cplusplus.com/ref/iostream/fstream/index.html


I must correct, in c++ fstream.h is deprecated, use fstream with no .h.

Share this post


Link to post
Share on other sites
Well since you aren't being too specific about your file type. :) You keep telling me different things, I put together an example for you. I didn't compile it, and probably full of mistakes. I haven't done allot of string parsing in c/c++, and I have to play around with this stuff abit before it works, so this might not compile and work perfectly as is. :D



include <fstream>

int main (void)
{

int nArray[10][10];
char c[256];

std::fstream fHighScore("ok.txt",std::fstream::in);
if (!fHighScore.is_open()) return;

int nRows, int nCols = 0;

//read in the rows, and cols
fHighScore.getline(c,256,','); nRows = atoi(c);
fHighScore.getline(c,256,'\n'); nCols = atoi(c);

int i;
for (i = 0; i < nRows; ++i)
{
fHighScore.getline(c,256,','); nArray[i][0] = atoi(c);
fHighScore.getline(c,256,','); nArray[i][1] = atoi(c);
fHighScore.getline(c,256,','); nArray[i][2] = atoi(c);
fHighScore.getline(c,256,','); nArray[i][3] = atoi(c);
fHighScore.getline(c,256,','); nArray[i][4] = atoi(c);
fHighScore.getline(c,256,','); nArray[i][5] = atoi(c);
fHighScore.getline(c,256,','); nArray[i][6] = atoi(c);
fHighScore.getline(c,256,','); nArray[i][7] = atoi(c);
fHighScore.getline(c,256,','); nArray[i][8] = atoi(c);
fHighScore.getline(c,256,'\n'); nArray[i][9] = atoi(c);
}

return 0;
}

// dummy text file
//first line is rows, cols
//data follows, ',' delimited, '\n' breaks lines

10,10
1,1,1,1,1,1,1,1,1,1
1,1,1,1,1,1,1,1,1,1
1,1,1,1,1,1,1,1,1,1
1,1,1,1,1,1,1,1,1,1
1,1,1,1,1,1,1,1,1,1
1,1,1,1,1,1,1,1,1,1
1,1,1,1,1,1,1,1,1,1
1,1,1,1,1,1,1,1,1,1
1,1,1,1,1,1,1,1,1,1
1,1,1,1,1,1,1,1,1,1


Share this post


Link to post
Share on other sites
Quote:
Original post by raptorstrike
that dosnt exactly help me. im trying to get a file thats going to = a int ARRAY[] so i need to do somthing like this fin >> ARRAY[] but when i do this

*** Source Snippet Removed ***

i get the output of
4442316
Press any key to continue...

and my txt file contains this

{9,0};

so i dont know were 4442316 is comming from and why its not reading my file right

You can't index element 900, you need to change the line to:
std::cout << array[899];, because the array goes from 0 to 899. 900 is not in your array.

Share this post


Link to post
Share on other sites
The istream extraction operator >> will ignore white space (tabs spaces, newlines), so just make your file uses one or more of these as delimiters for your data. If you do not know how many elements are in the file either store the number of elements in the file header, and allocate the array after you read the header, or use a vector instead of an array:

#include <fstream>
#include <vector>
using namespace std;


int main()
{
vector <int> array;
ifstream input;
input.open("data.txt");
int temp;
while (input.good())
{
input >> temp;
array.push_back(temp);
}
input.close();
for (int i=0; i < array.size(); ++i)
cout << array[i] << endl;
cin.ignore();
}

Share this post


Link to post
Share on other sites

#include <algorithm>
#include <vector>
#include <iterator>
#include <fstream>
#include <iostream>

int main() {
std::vector<int> array;
//load into vector
copy(std::istream_iterator<int>(std::ifstream("a.txt")),
std::istream_iterator<int>(),
std::back_inserter(array));
//print out
copy(array.begin(), array.end(), std::ostream_iterator<int>(std::cout, " "));
}

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this