reading in an array from a .txt file

Started by
11 comments, last by raptorstrike 19 years, 6 months ago
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)
____________________________"This just in, 9 out of 10 americans agree that 1 out of 10 americans will disagree with the other 9"- Colin Mochrie
Advertisement
What language?
oops sorry i didnt include that
( should really just make that my sig)
anyway c++ dev 4.9.9.0 SDL(if that makes a diffrence)
____________________________"This just in, 9 out of 10 americans agree that 1 out of 10 americans will disagree with the other 9"- Colin Mochrie
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
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
____________________________"This just in, 9 out of 10 americans agree that 1 out of 10 americans will disagree with the other 9"- Colin Mochrie
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?
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)
____________________________"This just in, 9 out of 10 americans agree that 1 out of 10 americans will disagree with the other 9"- Colin Mochrie
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.
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[0] = atoi(c);        fHighScore.getline(c,256,','); nArray[1] = atoi(c);        fHighScore.getline(c,256,','); nArray[2] = atoi(c);        fHighScore.getline(c,256,','); nArray[3] = atoi(c);        fHighScore.getline(c,256,','); nArray[4] = atoi(c);        fHighScore.getline(c,256,','); nArray[5] = atoi(c);        fHighScore.getline(c,256,','); nArray[6] = atoi(c);        fHighScore.getline(c,256,','); nArray[7] = atoi(c);        fHighScore.getline(c,256,','); nArray[8] = atoi(c);        fHighScore.getline(c,256,'\n'); nArray[9] = atoi(c);    }        return 0;}// dummy text file//first line is rows, cols//data follows, ',' delimited, '\n' breaks lines10,101,1,1,1,1,1,1,1,1,11,1,1,1,1,1,1,1,1,11,1,1,1,1,1,1,1,1,11,1,1,1,1,1,1,1,1,11,1,1,1,1,1,1,1,1,11,1,1,1,1,1,1,1,1,11,1,1,1,1,1,1,1,1,11,1,1,1,1,1,1,1,1,11,1,1,1,1,1,1,1,1,11,1,1,1,1,1,1,1,1,1


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.

This topic is closed to new replies.

Advertisement