| | |
read line of text from file into array
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Oct 2008
Posts: 44
Reputation:
Solved Threads: 11
•
•
•
•
So line is a string and str is a C style string so you can use strtok on it.
Why not just use strcpy(str, line.c_str()) ?
while loop.. c++ Syntax (Toggle Plain Text)
//fragment code while (getline(filename, line)) //Loop through lines { char *str; str=new char[line.length()+1]; strcpy(str,line.c_str()); char * pch=NULL; count_word=0; pch = strtok (str," "); while (pch!=NULL) { get_content[count_line][count_word]=pch; pch = strtok (NULL, " "); array_words[count_line]=++count_word; } ++count_line; delete []str; } filename.close(); } //display each word for(int p=0;p<count_line;++p) for(int u=0;u<array_words[p];++u) cout<<get_content[p][u]<<endl;;
Last edited by zalezog; Jan 16th, 2009 at 12:59 pm.
Perhaps we could welcome him to the world of C++, rather than staying in the dark ages of C. Vectors, as have already been suggested would be a better approach....as would stringstreams. Chris
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <fstream> #include <vector> #include <sstream> #include <string> using namespace std; int main(void){ ifstream data("test.dat", ios::in); string line, temp; istringstream oss; int count = 0; vector< vector<string> > DataFromFile; while(getline(data, line)){ DataFromFile.push_back( vector<string>() ); oss.clear(); oss.str(line); while(oss >> temp) DataFromFile[count].push_back(temp); count++; } for(int i = 0; i < DataFromFile.size(); i++){ for(int x = 0; x < DataFromFile[i].size(); x++) cout << DataFromFile[i][x] << '\t'; cout << endl; } return 0; }
Last edited by Freaky_Chris; Jan 16th, 2009 at 1:49 pm.
Knowledge is power -- But experience is everything
•
•
Join Date: Dec 2008
Posts: 57
Reputation:
Solved Threads: 0
I've come back to this cos it was doing my head in
I have to do this
I've attached the type of file i want to read
I have been thinking the last couple of days the best way to do this.I was thinking about creating a function to read the file once, count the number of lines between the <rows> tags and create a vector or array. Then another function to count the number of lines between the <col> tags and create another vector or array and add whatever is between those tags to the vector/array.
I just need a point in the right direction and to find out what is the best way of doing this so i can crack on.
I am going to have another crack at it now and see how far i get
I have to do this
•
•
•
•
In order to make the matrix implementation as flexible as possible, we will not place an a priori limit on the size of the matrix that can be represented. This will require the use of dynamic memory allocation. Consequently, the private data members for matrix is consist of the variable rows and cols, which will be used to store the row and column dimensions of a matrix, and element, which will be used to store a variable of type pointer-to-pointer-to-double. For a given matrix, the variable element will point to an array of pointers, and each element in this array of pointers will point to another array that contains the elements belonging to a single row of the matrix.
Each "row" and "col" has a name (string data type) and is identified by an identifier (int type) in the matrix. To elaborate further, each row in the matrix represents a "row" and each column index represents a unique name of a "col".
I have been thinking the last couple of days the best way to do this.I was thinking about creating a function to read the file once, count the number of lines between the <rows> tags and create a vector or array. Then another function to count the number of lines between the <col> tags and create another vector or array and add whatever is between those tags to the vector/array.
I just need a point in the right direction and to find out what is the best way of doing this so i can crack on.
I am going to have another crack at it now and see how far i get
•
•
Join Date: Dec 2008
Posts: 57
Reputation:
Solved Threads: 0
I have been working on this for a little while and have come up with this which isn't perfect by far
what the plan is, is to have seperate functions that adds anything between the <row></row> and <col></col> tags.
I just want to declare a filename once and pass that as a parameter but i get these errors
am i on the right track?
C++ Syntax (Toggle Plain Text)
#include <fstream> #include <iostream> #include <string> #include <vector> using namespace std; void number_rows(string file) { ifstream myfile(file); if (!myfile) { cout << "I could not open the file.\n"; //return EXIT_FAILURE; } else { string line; vector<string> rows; while (getline( myfile, line )) { string str1 = "<row>"; string str2 = "</row>"; int pos = line.find(str1, 0); if(pos !=string:: npos) continue; else pos = line.find(str2, 0); if(pos != string::npos) break; else rows.push_back(line); } } myfile.close(); } void number_cols(string file) { ifstream myfile(file); if (!myfile) { cout << "I could not open the file.\n"; //return EXIT_FAILURE; } else { string line; vector<string> cols; while (getline( myfile, line )) { string str1 = "<col>"; string str2 = "</col>"; int pos = line.find(str1, 0); if(pos !=string:: npos) continue; else pos = line.find(str2, 0); if(pos != string::npos) break; else cols.push_back(line); } } myfile.close(); } void get_matrix(string file) { /*ifstream data(file, ios::in); string line, temp; istringstream oss; int count = 0; vector< vector<string> > DataFromFile; while(getline(data, line)){ DataFromFile.push_back( vector<string>() ); oss.clear(); oss.str(line); while(oss >> temp) DataFromFile[count].push_back(temp); count++; }*/ } int main() { string file = "words.txt"; ifstream myfile(file); if (!myfile) { cout << "I could not open the file. Fooey.\n"; return EXIT_FAILURE; } else { number_rows(file); number_cols(file); //matrix(myfile); } return EXIT_SUCCESS; }
I just want to declare a filename once and pass that as a parameter but i get these errors
•
•
•
•
crap.cpp(10) : error C2664: 'std::basic_ifstream<_Elem,_Traits>::basic_ifstream(const char *,std::ios_base::openmode,int)' : cannot convert parameter 1 from 'std::string' to 'const char *'
with
[
_Elem=char,
_Traits=std::char_traits<char>
]
No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called crap.cpp(42) : error C2664: 'std::basic_ifstream<_Elem,_Traits>::basic_ifstream(const char *,std::ios_base::openmode,int)' : cannot convert parameter 1 from 'std::string' to 'const char *'
with
[
_Elem=char,
_Traits=std::char_traits<char>
]
No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called crap.cpp(95) : error C2664: 'std::basic_ifstream<_Elem,_Traits>::basic_ifstream(const char *,std::ios_base::openmode,int)' : cannot convert parameter 1 from 'std::string' to 'const char *'
with
[
_Elem=char,
_Traits=std::char_traits<char>
]
No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
•
•
Join Date: Oct 2008
Posts: 44
Reputation:
Solved Threads: 11
•
•
•
•
I have been working on this for a little while and have come up with this which isn't perfect by far
what the plan is, is to have seperate functions that adds anything between the <row></row> and <col></col> tags.C++ Syntax (Toggle Plain Text)
#include <fstream> #include <iostream> #include <string> #include <vector> using namespace std; void number_rows(string file) { ifstream myfile(file); if (!myfile) { cout << "I could not open the file.\n"; //return EXIT_FAILURE; } else { string line; vector<string> rows; while (getline( myfile, line )) { string str1 = "<row>"; string str2 = "</row>"; int pos = line.find(str1, 0); if(pos !=string:: npos) continue; else pos = line.find(str2, 0); if(pos != string::npos) break; else rows.push_back(line); } } myfile.close(); } void number_cols(string file) { ifstream myfile(file); if (!myfile) { cout << "I could not open the file.\n"; //return EXIT_FAILURE; } else { string line; vector<string> cols; while (getline( myfile, line )) { string str1 = "<col>"; string str2 = "</col>"; int pos = line.find(str1, 0); if(pos !=string:: npos) continue; else pos = line.find(str2, 0); if(pos != string::npos) break; else cols.push_back(line); } } myfile.close(); } void get_matrix(string file) { /*ifstream data(file, ios::in); string line, temp; istringstream oss; int count = 0; vector< vector<string> > DataFromFile; while(getline(data, line)){ DataFromFile.push_back( vector<string>() ); oss.clear(); oss.str(line); while(oss >> temp) DataFromFile[count].push_back(temp); count++; }*/ } int main() { string file = "words.txt"; ifstream myfile(file); if (!myfile) { cout << "I could not open the file. Fooey.\n"; return EXIT_FAILURE; } else { number_rows(file); number_cols(file); //matrix(myfile); } return EXIT_SUCCESS; }
I just want to declare a filename once and pass that as a parameter but i get these errors
am i on the right track?
should be replaced with:
c++ Syntax (Toggle Plain Text)
ifstream myfile; myfile.open(file.c_str());
Last edited by zalezog; Jan 21st, 2009 at 11:16 am.
•
•
Join Date: Oct 2008
Posts: 44
Reputation:
Solved Threads: 11
or
C++ Syntax (Toggle Plain Text)
ifstream myfile(file.c_str());
•
•
Join Date: May 2008
Posts: 538
Reputation:
Solved Threads: 86
@Zalezog
Don't quote the whole message if you only want to comment on a few lines.
@AdRock
When posting c++ code, please use c++ code tags
[code=c++]
// Your code here
[/code]
(Don't make me tell you again)
With vectors, you don't have to make a 'first pass' to count the number of records and a 'second pass' to read the data. The vectors don't have a size limit.
If you want to confirm that your matrix is rectangular, you could verify the counts after you're done reading.
Don't quote the whole message if you only want to comment on a few lines.
@AdRock
When posting c++ code, please use c++ code tags
[code=c++]
// Your code here
[/code]
(Don't make me tell you again)
With vectors, you don't have to make a 'first pass' to count the number of records and a 'second pass' to read the data. The vectors don't have a size limit.
If you want to confirm that your matrix is rectangular, you could verify the counts after you're done reading.
![]() |
Similar Threads
- Reading from text file into array (C++)
- Inputting text file into array and finding top 3 values (C++)
- cast text file to an array (C#)
- how to read data from text file into data grid??? (VB.NET)
- inputing a text file into an array (C++)
- Read a specific line from a text file - how to ? (Java)
- Putting Auto Numbers in a File (C#)
- Help Reading Info in Text File Into an Array (C++)
Other Threads in the C++ Forum
- Previous Thread: command time output in Linux
- Next Thread: How to Read text file into array and skip the delimeters
| Thread Tools | Search this Thread |
api array based beginner binary bitmap c++ c/c++ calculator char char* class code coding compile compiler console conversion count database delete deploy desktop developer dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker list loop looping loops map math memory multiple news node number numbertoword output parameter pointer problem program programming project python random read recursion recursive reference rpg sorting string strings struct temperature template test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets





