6 Replies - 5530 Views - Last Post: 16 November 2009 - 11:17 PM Rate Topic: -----

#1 QSOSMOOTH  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 20
  • Joined: 07-November 09

reading data from a file into an array

Post icon  Posted 16 November 2009 - 05:48 PM

i am trying to read data froma file into an array but when it runs nothing shows here is what i have
int number;
  ifstream infile;
  infile.open("c:MP3data.txt");
  infile >> number;
  int i;
  int downloads [number];
  for(i=0;i<number;i++)
  {

	  infile >> downloads [i];

  }
   
  infile.close( );

for(i=0;i<number;i++)
{
	 cout<<downloads [i]<< " ";
}



	return 0;

This post has been edited by QSOSMOOTH: 16 November 2009 - 05:52 PM


Is This A Good Question/Topic? 1
  • +

Replies To: reading data from a file into an array

#2 Guest_c.user*


Reputation:

Re: reading data from a file into an array

Posted 16 November 2009 - 05:52 PM

"c:\\MP3data.txt"
Was This Post Helpful? 0

#3 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1076
  • View blog
  • Posts: 4,536
  • Joined: 09-June 09

Re: reading data from a file into an array

Posted 16 November 2009 - 05:52 PM

What does the file look like?
ifstream file("data.txt");

int i=0;
while(getline(file,line))
{
downloads[i] = atoi(line.c_str());
i++;

}

EDIT:: CUser good catch, Make sure to include full path with backslashes

This post has been edited by ImaSexy: 16 November 2009 - 05:54 PM

Was This Post Helpful? 0
  • +
  • -

#4 QSOSMOOTH  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 20
  • Joined: 07-November 09

Re: reading data from a file into an array

Posted 16 November 2009 - 06:09 PM

View PostImaSexy, on 16 Nov, 2009 - 04:52 PM, said:

What does the file look like?
ifstream file("data.txt");

int i=0;
while(getline(file,line))
{
downloads[i] = atoi(line.c_str());
i++;

}

EDIT:: CUser good catch, Make sure to include full path with backslashes


it is suppose to have ids and those people have downloads assigned to them
Was This Post Helpful? 0
  • +
  • -

#5 QSOSMOOTH  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 20
  • Joined: 07-November 09

Re: reading data from a file into an array

Posted 16 November 2009 - 06:41 PM

View PostQSOSMOOTH, on 16 Nov, 2009 - 05:09 PM, said:

View PostImaSexy, on 16 Nov, 2009 - 04:52 PM, said:

What does the file look like?
ifstream file("data.txt");

int i=0;
while(getline(file,line))
{
downloads[i] = atoi(line.c_str());
i++;

}

EDIT:: CUser good catch, Make sure to include full path with backslashes


it is suppose to have ids and those people have downloads assigned to them

it still doesnt open the file
Was This Post Helpful? 0
  • +
  • -

#6 QSOSMOOTH  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 20
  • Joined: 07-November 09

Re: reading data from a file into an array

Posted 16 November 2009 - 07:01 PM

View PostQSOSMOOTH, on 16 Nov, 2009 - 05:41 PM, said:

View PostQSOSMOOTH, on 16 Nov, 2009 - 05:09 PM, said:

View PostImaSexy, on 16 Nov, 2009 - 04:52 PM, said:

What does the file look like?
ifstream file("data.txt");

int i=0;
while(getline(file,line))
{
downloads[i] = atoi(line.c_str());
i++;

}

EDIT:: CUser good catch, Make sure to include full path with backslashes


it is suppose to have ids and those people have downloads assigned to them

it still doesnt open the file

int number;
  ifstream infile; // input file stream object
  infile.open("MP3data.txt"); // open the file
  infile >> number;
  int i; // loop counter variable
  int downloads [number]; // array

 //read the downloads from the file into the array
 for(i=0;i<number;i++)
  {
	infile >> downloads [i];
  }

  infile.close( );// closes the file

  // display the downloads read
  for(i=0;i<number;i++)
  {
	 cout<<downloads [i]<< " ";
  }



still doesnt open the file
Was This Post Helpful? 1
  • +
  • -

#7 Guest_c.user*


Reputation:

Re: reading data from a file into an array

Posted 16 November 2009 - 11:17 PM

	infile.open("MP3data.txt"); // open the file
	if (!infile.is_open()) {
		cerr << "error: file" << endl;
		return 1;
	}
	infile >> number;


Was This Post Helpful? 0

Page 1 of 1