9 Replies - 2765 Views - Last Post: 22 November 2011 - 01:36 PM Rate Topic: -----

#1 dangsos   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 23-May 10

how do I input lines from a file into an array until end of file?

Posted 21 November 2011 - 11:14 PM

so I've been reading all around google and I guess I use getline? but I'm not 100% sure how to use getline...

here is the line of code I'm playing with

for(int i=0;!inFile.eof();i++) {
        getline(inFile, array[i]);
    }


would that not loop until at the end of iFile and get the first line from iFile placing it into the array however many times?

I feel like Im lost lol!
like I'm not even incrementing thru the file so how is that not an infinite loop?

Is This A Good Question/Topic? 0
  • +

Replies To: how do I input lines from a file into an array until end of file?

#2 dangsos   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 23-May 10

Re: how do I input lines from a file into an array until end of file?

Posted 21 November 2011 - 11:43 PM

okay so maybe I am doing it right now? It seems to work

for (int i=0; !inFile.eof();i++){
   inFile >> array[i];
}



is that pulling each line from the inFile stream (linked to data.txt in this code) and putting it into the array by line?
Was This Post Helpful? 0
  • +
  • -

#3 David W   User is offline

  • DIC supporter
  • member icon

Reputation: 298
  • View blog
  • Posts: 1,839
  • Joined: 20-September 08

Re: how do I input lines from a file into an array until end of file?

Posted 22 November 2011 - 12:14 AM

Try this ...
int max_size = 10; // or whatever is the max size
int j = 0;
while( j < max_size && inFile >> array[ j ] ) ++ j


This post has been edited by David W: 22 November 2011 - 12:16 AM

Was This Post Helpful? 0
  • +
  • -

#4 dangsos   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 23-May 10

Re: how do I input lines from a file into an array until end of file?

Posted 22 November 2011 - 12:16 AM

okay so for anyone reading this I think I figured it out

for (int i = 0; !inFile.eof(); i++){
   getline(inFile,array[i]);
}



will fill up the array with each line it reads from the file inFile is linked to.

my question is this....how does that for loop tell the pointer to move thru the file? like when getline is done does the pointer automatically rest at the end of the linee it reads? I would assume it would read the line and return back to the start of the file everytime, but it works so I don't know.
Was This Post Helpful? 0
  • +
  • -

#5 David W   User is offline

  • DIC supporter
  • member icon

Reputation: 298
  • View blog
  • Posts: 1,839
  • Joined: 20-September 08

Re: how do I input lines from a file into an array until end of file?

Posted 22 November 2011 - 12:20 AM

Use C++ strings and ...
ifstream fin( "yourFileOflines.txt" );
while( j < max_size && getline( fin, aryStr[ j ] ) ) ++j;

This post has been edited by David W: 22 November 2011 - 12:29 AM

Was This Post Helpful? 0
  • +
  • -

#6 dangsos   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 23-May 10

Re: how do I input lines from a file into an array until end of file?

Posted 22 November 2011 - 12:23 AM

how do I know what maxsize of the file is?
Was This Post Helpful? 0
  • +
  • -

#7 David W   User is offline

  • DIC supporter
  • member icon

Reputation: 298
  • View blog
  • Posts: 1,839
  • Joined: 20-September 08

Re: how do I input lines from a file into an array until end of file?

Posted 22 November 2011 - 12:32 AM

max_size is your array max size ...
But best to read into a vector of strings using push_back
ifstream fin( "lines.txt" );
vector< string > v;
string line;
while( getline( fin, line ) ) v.push_back( line) ;
// ok ... done reading file ... so ...
cout << "Number of lines in file = v.size() = " << v.size() << endl;

If you need to use an array, you could first count the lines in the file, first pass ...
Then make enough dynamic memory for your array of strings to hold that count
string* aryStr = new string[ count ];

//... when done...
delete [ ] aryStr; // avoid memory leaks

or ... you can write your own dynamic array code ... so best to use a STL vector or list container that is 'ready to go' ...

This post has been edited by David W: 22 November 2011 - 12:59 AM

Was This Post Helpful? 0
  • +
  • -

#8 JackOfAllTrades   User is offline

  • Saucy!
  • member icon

Reputation: 6246
  • View blog
  • Posts: 24,014
  • Joined: 23-August 08

Re: how do I input lines from a file into an array until end of file?

Posted 22 November 2011 - 04:44 AM

Quote

my question is this....how does that for loop tell the pointer to move thru the file? like when getline is done does the pointer automatically rest at the end of the linee it reads? I would assume it would read the line and return back to the start of the file everytime, but it works so I don't know.


When you open a file in C and C++ (and AFAIK, any language), an internal file pointer is created which moves through the file as you call functions. Every time you call getline the internal file pointer moves to the position immediately after its last read. This causes a problem in you original loop:

for (int i = 0; !inFile.eof(); i++){
   getline(inFile,array[i]);
}


The use of .eof() as the condition means you will get one extra entry in your array, because eof is a flag that is set when the file pointer goes to read the file and finds it's at the end; it doesn't occur automatically. This is why it's a bad idea to use eof to control a loop. The FAQ on said subject
Was This Post Helpful? 0
  • +
  • -

#9 obviousninja   User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 68
  • Joined: 17-February 10

Re: how do I input lines from a file into an array until end of file?

Posted 22 November 2011 - 11:10 AM

fgets(charbuffer, howmanyletters, filestream)
Was This Post Helpful? 0
  • +
  • -

#10 David W   User is offline

  • DIC supporter
  • member icon

Reputation: 298
  • View blog
  • Posts: 1,839
  • Joined: 20-September 08

Re: how do I input lines from a file into an array until end of file?

Posted 22 November 2011 - 01:36 PM

View Postobviousninja, on 22 November 2011 - 01:10 PM, said:

fgets(charbuffer, howmanyletters, filestream)

The OP was using getline which suggests a C++ style ... fgets is C
The problem is a very common one ... and the FAQ link above, by Jack... is very relevant ... Good to take a look-see :)

This post has been edited by David W: 22 November 2011 - 01:41 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1