2 Replies - 1796 Views - Last Post: 13 April 2011 - 03:12 AM Rate Topic: -----

#1 crlstome1  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 01-April 11

reading in data from a text file into an array using fstream.

Posted 13 April 2011 - 02:38 AM

//how do you read in data from a text file into an array using c++

#include <iostream>
#include <fstream>

using namespace std;

int main()
{
    int myArray[10];
    ifstream infile;

    infile.open("text.txt");

    //from here, i dont know...

    return 0;
}


MOD EDIT: When posting code...USE CODE TAGS!!!

:code:

This post has been edited by JackOfAllTrades: 13 April 2011 - 03:02 AM


Is This A Good Question/Topic? 0
  • +

Replies To: reading in data from a text file into an array using fstream.

#2 muballitmitte  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 174
  • View blog
  • Posts: 470
  • Joined: 05-November 08

Re: reading in data from a text file into an array using fstream.

Posted 13 April 2011 - 02:49 AM

fstream inherits the >> operator so assuming your file has 3 numbers on separate lines as so
1
2
3


to read you would

int x[3];
int i = 0;

while (i < 3)
{
infile >> x[i];
++i;
}


Was This Post Helpful? 0
  • +
  • -

#3 crlstome1  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 01-April 11

Re: reading in data from a text file into an array using fstream.

Posted 13 April 2011 - 03:12 AM

yes...
thanks alot...
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1