Hi i got an problem i need to read from an text file that i got from an excel file at start. so i want each block in the excel file to have one space at the array i saved it so it got seperated by ;
example:

4   ; 0,0
42 ; 0,0
15 ; 3,2
73 ; 5,4
61 ; 5,6
7   ; 4,6

so i've been trying to get some information in these threads and learned this

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main () 
{
   string line;
   string array[50];
   int temp=0;
   ifstream myfile ("example.txt");
   if (myfile.is_open())
   {
      while (! myfile.eof())
      {
         getline (myfile,line, ';');
         array[temp]=line;
         temp++;
      }
      myfile.close();
   }
   else cout << "Unable to open file"; 
   for (int a=0;a<=temp;a++)
   {
      cout<<array[a]<<endl;
   }

   return 0;
}

This one works fine because it seperates away the ; and writes it to my array bur since its an string i cant work on with it... i need it in int form so i can do my other calculations later on

#include <iostream>
#include <fstream>
using namespace std;

int main()
{

   int i = 0;
   int array[20];
   int max_read = 20;
   int amountRead = 0;

   std::ifstream in("example.txt",std::ios::in |std::ios::binary);

   if(!in)
   {
      std::cout<<"Could not open file"<<std::endl;
      return 1; 
   }
   //this is where we are reading in the information into our array
   while(in>>array[amountRead] && amountRead < max_read)
   {
      amountRead++; 
   }

   for(i = 0; i < 20; i++)
   {
      cout<<array[i]<<endl; 
   }
   return 0;  
}

And this program are with int form but i cant get it to seperate and with this program i have to know how big the array is soposed to be. That wont work so well for me because this program is going to be used later on with some calculations and it can be different large everytime

thx for help :)

you can use the istringstream class and convert string to int

string a = "byebye";
istringstream s(a);
int i = 0;
s>>i 
...

I think you can try this..

mhm didnt know that could be done :P well this will make it work in an easy way thank you very much :)

Also a few other point:

First (and most important) learn how to use code-tags when posting code

Next:

while (! myfile.eof())
{
getline (myfile,line, ';');
array[temp]=line;
temp++;
}

Never use the horrible eof() command... Change it to:

while (getline (myfile,line, ';'))
{
     array[temp]=line;
     temp++;
}

But what happens if there are more then 50 words (numbers) in your file? Your array will go out of bounds -> Crash!
I would recommend you use vectors to solve this issue. Here's an example:

#include <iostream>
#include <fstream>
#include <vector>
#include <string>

using namespace std;

int main ()
{
    ifstream myfile ("example.txt");
    if (!myfile.is_open())
    {
        cout << "Failed to open";
        return 0;
    }
    vector<string> numbers;
    string line;
    while (getline(myfile, line, ';'))
        numbers.push_back(line);  
    cout << "found: " << numbers.size() << " numbers in file: \n";
    for (unsigned i = 0; i < numbers.size(); i++)
        cout << numbers[i] << "";
    return 0;
}

[edit]

On second thought: This may actually not work (your code neither). If you want to delimit on a semicolon ( ; ) you'd need one after every number...

So I guess you'll have to read in one line at a time and then break it up manually...

sorry for the not code taging but this worked quiete good but got some warnings i dont understand but the real problem is that i need to do some calculations with it later so i would feel most familiar if it got converted to int form. the things i need to do later on is to compare some values and stuff

I would probably try and solve this by reading all the data into an array of chars, and then manually splitting up the text. You should have more control over whats happening if you do this. See my code snippet. As for converting to an int, it's no big task, you can either use std::stringstream, or use the C function atoi (which I would probably use as it's easier).
You can do as niek said, split up all the text into lines, and then split up the lines to retrieve the data you need.

Hope this helps.

Still dont understand the warnings i get :(

C:\Documents and Settings\Dendeii\Skrivbord\Programmering\dedi.cpp(25) : warning C4786: 'std::reverse_iterator<std::basic_string<char,std::char_traits<char>,std::allocator<char> > const *,std::basic_string<char,std::char_traits<char>,std::allocator<
char> >,std::basic_string<char,std::char_traits<char>,std::allocator<char> > const &,std::basic_string<char,std::char_traits<char>,std::allocator<char> > const *,int>' : identifier was truncated to '255' characters in the debug information
C:\Documents and Settings\Dendeii\Skrivbord\Programmering\dedi.cpp(25) : warning C4786: 'std::reverse_iterator<std::basic_string<char,std::char_traits<char>,std::allocator<char> > *,std::basic_string<char,std::char_traits<char>,std::allocator<char> 
>,std::basic_string<char,std::char_traits<char>,std::allocator<char> > &,std::basic_string<char,std::char_traits<char>,std::allocator<char> > *,int>' : identifier was truncated to '255' characters in the debug information
c:\program\microsoft visual studio\vc98\include\vector(39) : warning C4786: 'std::vector<std::basic_string<char,std::char_traits<char>,std::allocator<char> >,std::allocator<std::basic_string<char,std::char_traits<char>,std::allocator<char> > > >::ve
ctor<std::basic_string<char,std::char_traits<char>,std::allocator<char> >,std::allocator<std::basic_string<char,std::char_traits<char>,std::allocator<char> > > >' : identifier was truncated to '255' characters in the debug information
c:\program\microsoft visual studio\vc98\include\vector(60) : warning C4786: 'std::vector<std::basic_string<char,std::char_traits<char>,std::allocator<char> >,std::allocator<std::basic_string<char,std::char_traits<char>,std::allocator<char> > > >::~v
ector<std::basic_string<char,std::char_traits<char>,std::allocator<char> >,std::allocator<std::basic_string<char,std::char_traits<char>,std::allocator<char> > > >' : identifier was truncated to '255' characters in the debug information

I would do something like this,

#include <iostream>
#include <string>
#include <sstream>
#include <vector>
#include <fstream>

using namespace std;

struct cell{
    int value;
    string pos;
}cellData;

int main(void){
    ifstream ins("example.txt");
    if(!ins.good()) return 0;

    vector<cell> data;
    string line;
    istringstream iss;

    while(getline(ins, line)){
        for(int i = 0; i < line.length(); i++)
            if(line[i] == ';') line.erase(i, 1);
        iss.clear();
        iss.str(line);
        iss >> cellData.value >> cellData.pos;
        data.push_back(cellData);
    }
    for(int i = 0; i < data.size(); i++)
        cout << "Value: " << data[i].value << " (" << data[i].pos << ")" << endl;

    return 0;
}

I would not reccomend atoi() it's error response is poor, if you wish to do it this way I would say use strtol() (yes thats right to long, but at least it can return errors!). But I would just use string streams as you can see from above.

neik_e is correct, do not use eof() it can return true on some escpae characters so it's not advised!

Chris

that worked very well :) are going to work on the converting now thank you :P

This question has already been answered. Start a new discussion instead.