| | |
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:
Solved Threads: 0
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:
so i've been trying to get some information in these threads and learned this
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
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
example:
C++ Syntax (Toggle Plain Text)
4 ; 0,0 42 ; 0,0 15 ; 3,2 73 ; 5,4 61 ; 5,6 7 ; 4,6
C++ Syntax (Toggle Plain Text)
#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; }
C++ Syntax (Toggle Plain Text)
#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; }
thx for help
Last edited by Narue; Jan 22nd, 2009 at 9:20 am. Reason: added code tags
you can use the istringstream class and convert string to int
I think you can try this..
C++ Syntax (Toggle Plain Text)
string a = "byebye"; istringstream s(a); int i = 0; s>>i ...
I think you can try this..
thanks
-chandra
-chandra
Also a few other point:
First (and most important) learn how to use code-tags when posting code
Next:
Never use the horrible eof() command... Change it to:
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:
[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...
First (and most important) learn how to use code-tags when posting code
Next:
C++ Syntax (Toggle Plain Text)
while (! myfile.eof()) { getline (myfile,line, ';'); array[temp]=line; temp++; }
Never use the horrible eof() command... Change it to:
C++ Syntax (Toggle Plain Text)
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:
C++ Syntax (Toggle Plain Text)
#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...
Last edited by niek_e; Jan 21st, 2009 at 11:23 am.
•
•
Join Date: Sep 2008
Posts: 17
Reputation:
Solved Threads: 0
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
•
•
Join Date: Mar 2008
Posts: 1,490
Reputation:
Solved Threads: 123
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.
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.
•
•
Join Date: Sep 2008
Posts: 17
Reputation:
Solved Threads: 0
Still dont understand the warnings i get
C++ Syntax (Toggle Plain Text)
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,
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
C++ Syntax (Toggle Plain Text)
#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; }
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
![]() |
Other Threads in the C++ Forum
- Previous Thread: read line of text from file into array
- Next Thread: Intstack question
| Thread Tools | Search this Thread |
Tag cloud for C++
api array arrays based beginner binary bmp c++ c/c++ calculator char class classes code compile compiler console conversion convert count data delete deploy dll download dynamic dynamiccharacterarray encryption error file format forms fstream function functions game givemetehcodez graph gui homeworkhelp iamthwee ifstream input int java lib library lines linker list loop looping loops map math matrix memory microsoft newbie news number output pointer problem program programming project python random read recursion recursive reference return rpg search simple spoonfeeding string strings struct temperature template templates test text text-file tree url variable vector video visual visualstudio void win32 windows winsock wordfrequency wxwidgets





well this will make it work in an easy way thank you very much 
