How to Read text file into array and skip the delimeters

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Sep 2008
Posts: 17
Reputation: Dendei is an unknown quantity at this point 
Solved Threads: 0
Dendei Dendei is offline Offline
Newbie Poster

How to Read text file into array and skip the delimeters

 
0
  #1
Jan 21st, 2009
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:
  1. 4 ; 0,0
  2. 42 ; 0,0
  3. 15 ; 3,2
  4. 73 ; 5,4
  5. 61 ; 5,6
  6. 7 ; 4,6
so i've been trying to get some information in these threads and learned this
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. using namespace std;
  5.  
  6. int main ()
  7. {
  8. string line;
  9. string array[50];
  10. int temp=0;
  11. ifstream myfile ("example.txt");
  12. if (myfile.is_open())
  13. {
  14. while (! myfile.eof())
  15. {
  16. getline (myfile,line, ';');
  17. array[temp]=line;
  18. temp++;
  19. }
  20. myfile.close();
  21. }
  22. else cout << "Unable to open file";
  23. for (int a=0;a<=temp;a++)
  24. {
  25. cout<<array[a]<<endl;
  26. }
  27.  
  28. return 0;
  29. }
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
  1. #include <iostream>
  2. #include <fstream>
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7.  
  8. int i = 0;
  9. int array[20];
  10. int max_read = 20;
  11. int amountRead = 0;
  12.  
  13. std::ifstream in("example.txt",std::ios::in |std::ios::binary);
  14.  
  15. if(!in)
  16. {
  17. std::cout<<"Could not open file"<<std::endl;
  18. return 1;
  19. }
  20. //this is where we are reading in the information into our array
  21. while(in>>array[amountRead] && amountRead < max_read)
  22. {
  23. amountRead++;
  24. }
  25.  
  26. for(i = 0; i < 20; i++)
  27. {
  28. cout<<array[i]<<endl;
  29. }
  30. return 0;
  31. }
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
Last edited by Narue; Jan 22nd, 2009 at 9:20 am. Reason: added code tags
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 462
Reputation: Agni is a jewel in the rough Agni is a jewel in the rough Agni is a jewel in the rough Agni is a jewel in the rough 
Solved Threads: 71
Sponsor
Agni's Avatar
Agni Agni is offline Offline
Posting Pro in Training

Re: How to Read text file into array and skip the delimeters

 
0
  #2
Jan 21st, 2009
you can use the istringstream class and convert string to int

  1. string a = "byebye";
  2. istringstream s(a);
  3. int i = 0;
  4. s>>i
  5. ...

I think you can try this..
thanks
-chandra
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 17
Reputation: Dendei is an unknown quantity at this point 
Solved Threads: 0
Dendei Dendei is offline Offline
Newbie Poster

Re: How to Read text file into array and skip the delimeters

 
0
  #3
Jan 21st, 2009
mhm didnt know that could be done well this will make it work in an easy way thank you very much
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,972
Reputation: niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute 
Solved Threads: 308
Moderator
Featured Poster
niek_e's Avatar
niek_e niek_e is offline Offline
Cenosillicaphobiac

Re: How to Read text file into array and skip the delimeters

 
0
  #4
Jan 21st, 2009
Also a few other point:

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

Next:
  1. while (! myfile.eof())
  2. {
  3. getline (myfile,line, ';');
  4. array[temp]=line;
  5. temp++;
  6. }

Never use the horrible eof() command... Change it to:
  1. while (getline (myfile,line, ';'))
  2. {
  3. array[temp]=line;
  4. temp++;
  5. }

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:

  1. #include <iostream>
  2. #include <fstream>
  3. #include <vector>
  4. #include <string>
  5.  
  6. using namespace std;
  7.  
  8. int main ()
  9. {
  10. ifstream myfile ("example.txt");
  11. if (!myfile.is_open())
  12. {
  13. cout << "Failed to open";
  14. return 0;
  15. }
  16. vector<string> numbers;
  17. string line;
  18. while (getline(myfile, line, ';'))
  19. numbers.push_back(line);
  20. cout << "found: " << numbers.size() << " numbers in file: \n";
  21. for (unsigned i = 0; i < numbers.size(); i++)
  22. cout << numbers[i] << "";
  23. return 0;
  24. }

[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...
Last edited by niek_e; Jan 21st, 2009 at 11:23 am.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 17
Reputation: Dendei is an unknown quantity at this point 
Solved Threads: 0
Dendei Dendei is offline Offline
Newbie Poster

Re: How to Read text file into array and skip the delimeters

 
0
  #5
Jan 21st, 2009
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
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 1,490
Reputation: William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of 
Solved Threads: 123
Sponsor
William Hemsworth William Hemsworth is online now Online
Nearly a Posting Virtuoso

Re: How to Read text file into array and skip the delimeters

 
0
  #6
Jan 21st, 2009
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.
Last edited by William Hemsworth; Jan 21st, 2009 at 12:34 pm.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 17
Reputation: Dendei is an unknown quantity at this point 
Solved Threads: 0
Dendei Dendei is offline Offline
Newbie Poster

Re: How to Read text file into array and skip the delimeters

 
0
  #7
Jan 21st, 2009
Still dont understand the warnings i get

  1. 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<
  2. 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
  3. 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>
  4. >,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
  5. 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
  6. 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
  7. 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
  8. 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
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 671
Reputation: Freaky_Chris is a jewel in the rough Freaky_Chris is a jewel in the rough Freaky_Chris is a jewel in the rough 
Solved Threads: 113
Freaky_Chris's Avatar
Freaky_Chris Freaky_Chris is offline Offline
Practically a Master Poster

Re: How to Read text file into array and skip the delimeters

 
0
  #8
Jan 21st, 2009
I would do something like this,
  1. #include <iostream>
  2. #include <string>
  3. #include <sstream>
  4. #include <vector>
  5. #include <fstream>
  6.  
  7. using namespace std;
  8.  
  9. struct cell{
  10. int value;
  11. string pos;
  12. }cellData;
  13.  
  14. int main(void){
  15. ifstream ins("example.txt");
  16. if(!ins.good()) return 0;
  17.  
  18. vector<cell> data;
  19. string line;
  20. istringstream iss;
  21.  
  22. while(getline(ins, line)){
  23. for(int i = 0; i < line.length(); i++)
  24. if(line[i] == ';') line.erase(i, 1);
  25. iss.clear();
  26. iss.str(line);
  27. iss >> cellData.value >> cellData.pos;
  28. data.push_back(cellData);
  29. }
  30. for(int i = 0; i < data.size(); i++)
  31. cout << "Value: " << data[i].value << " (" << data[i].pos << ")" << endl;
  32.  
  33. return 0;
  34. }
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
Knowledge is power -- But experience is everything
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 17
Reputation: Dendei is an unknown quantity at this point 
Solved Threads: 0
Dendei Dendei is offline Offline
Newbie Poster

Re: How to Read text file into array and skip the delimeters

 
0
  #9
Jan 21st, 2009
that worked very well are going to work on the converting now thank you
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Other Threads in the C++ Forum
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC