9 Replies - 1794 Views - Last Post: 22 July 2011 - 02:52 AM Rate Topic: -----

#1 anoopvadi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 19-November 10

Reading numbers from a text file and storing it in an array in C++

Posted 21 July 2011 - 11:37 PM

hi,
am a novice in programming. i am facing some difficulty in C++ programming, actually i want to read a column of numbers from a text file or an excel sheet and store that in an array so that i can use it for further processing.Plz help me to solve this issue.i am using turbo C++.
thanking in advance.
Anoop

Is This A Good Question/Topic? 0
  • +

Replies To: Reading numbers from a text file and storing it in an array in C++

#2 Hiram  Icon User is offline

  • D.I.C Head

Reputation: 69
  • View blog
  • Posts: 203
  • Joined: 02-June 09

Re: Reading numbers from a text file and storing it in an array in C++

Posted 21 July 2011 - 11:52 PM

Well, what have you tried? Post what code you've written so far.
Was This Post Helpful? 0
  • +
  • -

#3 anoopvadi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 19-November 10

Re: Reading numbers from a text file and storing it in an array in C++

Posted 22 July 2011 - 12:18 AM

i dont know how to start this thing...am a beginner in programming....plz help
Was This Post Helpful? 0
  • +
  • -

#4 anoopvadi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 19-November 10

Re: Reading numbers from a text file and storing it in an array in C++

Posted 22 July 2011 - 12:49 AM

i just modified a code that i got from net... but it is always showing error..it fails to open the file..


#include<iostream.h>
#include<fstream.h>
#include<iomanip.h>
#include<conio.h>
float temp[100];// array that can hold 100 numbers for 1st column
int main() // int main NOT void main
{
ifstream infile;

int num = 0; // num must start at 0
infile.open("D:\temperature.txt");// file containing numbers in 3 columns
if(infile.fail()) // checks to see if file opended
{
cout << "error" << endl;
return 1; // no point continuing if the file didn't open...
}
while(!infile.eof()) // reads file to end of *file*, not line
{
infile >> temp[num]; // read first column number

++num; // go to the next number

for(int i=0;i<60;i++)
cout<<temp[i]<<endl;
infile.close();

return 0; // everything went right.
getch();
}
Was This Post Helpful? 0
  • +
  • -

#5 Hiram  Icon User is offline

  • D.I.C Head

Reputation: 69
  • View blog
  • Posts: 203
  • Joined: 02-June 09

Re: Reading numbers from a text file and storing it in an array in C++

Posted 22 July 2011 - 01:01 AM

I wouldn't rely on getting help if you're not at least going to try, and then post something where you explicitly state you got code from the net.
Was This Post Helpful? 0
  • +
  • -

#6 anoopvadi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 19-November 10

Re: Reading numbers from a text file and storing it in an array in C++

Posted 22 July 2011 - 01:13 AM

i got only the idea...rest of the code is written by myself...it is not just a copy paste thing
Was This Post Helpful? 0
  • +
  • -

#7 Hiram  Icon User is offline

  • D.I.C Head

Reputation: 69
  • View blog
  • Posts: 203
  • Joined: 02-June 09

Re: Reading numbers from a text file and storing it in an array in C++

Posted 22 July 2011 - 01:24 AM

Alright then. So what exactly is the error being reported?
Was This Post Helpful? 0
  • +
  • -

#8 anoopvadi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 19-November 10

Re: Reading numbers from a text file and storing it in an array in C++

Posted 22 July 2011 - 01:32 AM

i am getting output screen with
error


coz of following commands

if(infile.fail()) // checks to see if file opended
{
cout << "error" << endl;
return 1; // no point continuing if the file didn't open...
}

indicating that the program fails to open the file..but i gave the right path...also i tried with putting the file in the same folder where this program is saved...but still it is not working..
Was This Post Helpful? 0
  • +
  • -

#9 Hiram  Icon User is offline

  • D.I.C Head

Reputation: 69
  • View blog
  • Posts: 203
  • Joined: 02-June 09

Re: Reading numbers from a text file and storing it in an array in C++

Posted 22 July 2011 - 02:21 AM

Include cerrno and cstring, and after you do cout << "error" << endl, add cout << strerror(errno) << endl;
Was This Post Helpful? 0
  • +
  • -

#10 anoopvadi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 19-November 10

Re: Reading numbers from a text file and storing it in an array in C++

Posted 22 July 2011 - 02:52 AM

it is solved.. using the following code


#include<iostream.h>
#include<fstream.h>
#include <stdio.h>
#include <stdlib.h>
#include<math.h>
using namespace std;
float temp2[100];// array that can hold 100 numbers for 1st column
FILE *fp;
int main() // int main NOT void main
{
ifstream infile("temperature.txt");
int num = 0,i; // num must start at 0
//fp=infile.open("temperature.txt");//
if(infile.fail()) // checks to see if file opended
{
cout << "error" << endl;
return 1; // no point continuing if the file didn't open...
}
while(!infile.eof()) // reads file to end of *file*, not line
{
infile >> temp2[num]; // read first column number

++num; // go to the next number
}
for(i=0;i<60;i++)
{
cout<<temp2[i]<<endl;
}
infile.close();

return 0;
}


but in Dev C++.. not in turbo C++...
in dev C++ i faced a problem of pausing the output screen...
even if i put getch,cin.get() or system("pause") with respective header files ,it is not working....the output screen is just blinking away... i saw the output by writing them in a file ...is there any other way to pause output screen??
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1