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

yup....and i got some code but many of them was in C...also they were not wotking in turbo C++.. in total not at all useful.:(

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();
  }

Edited 3 Years Ago by Dani: Formatting fixed

I don't mean to be picky but you should use the code tags for posting code, those are forum rules for posting. If you don't follow them your post will more likely be ignored.

Also with code tags and a good identation you can see for yourself the errors more clearly.

Finally when you get an error please specify it,

BTW in that code the closing tag is missing for example

am getting an output screen in which it is written
error
coz of 13th line..means program fails to open the file.

but i correctly gave the path to the file..

so what may be the problem??

Modified your code a bit

// these are c++ includes not c
#include<iostream>
#include<fstream>
#include<iomanip>

using namespace std;

int main() // int main NOT void main
{
  float temp[100];// array that can hold 100 numbers for 1st column

  ifstream infile;
  
  int num = 0; // num must start at 0
  infile.open("infile.cc");// 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.
    } 
}

This program works perfectly so...

why don't you put the file in the same folder as the executable and specify the relative path to it (which is just the name of the file)

Edited 5 Years Ago by tikoti: n/a

This article has been dead for over six months. Start a new discussion instead.