943,545 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 8794
  • C++ RSS
You are currently viewing page 2 of this multi-page discussion thread; Jump to the first page
Jan 16th, 2009
0

Re: read line of text from file into array

Quote ...

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()) ?
That's done Murtan suggested a better way to solve the problem,I got your point and may be we utilize the memory a bit more efficiently this time in our while loop..
c++ Syntax (Toggle Plain Text)
  1. //fragment code
  2. while (getline(filename, line)) //Loop through lines
  3. {
  4.  
  5. char *str;
  6. str=new char[line.length()+1];
  7. strcpy(str,line.c_str());
  8.  
  9. char * pch=NULL;
  10. count_word=0;
  11. pch = strtok (str," ");
  12. while (pch!=NULL)
  13. {
  14.  
  15. get_content[count_line][count_word]=pch;
  16. pch = strtok (NULL, " ");
  17.  
  18. array_words[count_line]=++count_word;
  19. }
  20. ++count_line;
  21. delete []str;
  22. }
  23. filename.close();
  24. }
  25. //display each word
  26. for(int p=0;p<count_line;++p)
  27. for(int u=0;u<array_words[p];++u)
  28. cout<<get_content[p][u]<<endl;;
Last edited by zalezog; Jan 16th, 2009 at 12:59 pm.
Reputation Points: 53
Solved Threads: 13
Light Poster
zalezog is offline Offline
47 posts
since Oct 2008
Jan 16th, 2009
0

Re: read line of text from file into array

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.
C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <fstream>
  3. #include <vector>
  4. #include <sstream>
  5. #include <string>
  6.  
  7. using namespace std;
  8.  
  9. int main(void){
  10. ifstream data("test.dat", ios::in);
  11. string line, temp;
  12. istringstream oss;
  13. int count = 0;
  14. vector< vector<string> > DataFromFile;
  15.  
  16. while(getline(data, line)){
  17. DataFromFile.push_back( vector<string>() );
  18. oss.clear();
  19. oss.str(line);
  20. while(oss >> temp)
  21. DataFromFile[count].push_back(temp);
  22. count++;
  23. }
  24.  
  25. for(int i = 0; i < DataFromFile.size(); i++){
  26. for(int x = 0; x < DataFromFile[i].size(); x++)
  27. cout << DataFromFile[i][x] << '\t';
  28. cout << endl;
  29. }
  30. return 0;
  31. }
Chris
Last edited by Freaky_Chris; Jan 16th, 2009 at 1:49 pm.
Reputation Points: 325
Solved Threads: 118
Master Poster
Freaky_Chris is offline Offline
702 posts
since Apr 2008
Jan 21st, 2009
0

Re: read line of text from file into array

I've come back to this cos it was doing my head in

I have to do this
Quote ...
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'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
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
AdRock is offline Offline
64 posts
since Dec 2008
Jan 21st, 2009
0

Re: read line of text from file into array

I have been working on this for a little while and have come up with this which isn't perfect by far
C++ Syntax (Toggle Plain Text)
  1. #include <fstream>
  2. #include <iostream>
  3. #include <string>
  4. #include <vector>
  5.  
  6. using namespace std;
  7.  
  8. void number_rows(string file)
  9. {
  10. ifstream myfile(file);
  11. if (!myfile)
  12. {
  13. cout << "I could not open the file.\n";
  14. //return EXIT_FAILURE;
  15. }
  16. else
  17. {
  18. string line;
  19.  
  20. vector<string> rows;
  21.  
  22. while (getline( myfile, line ))
  23. {
  24. string str1 = "<row>";
  25. string str2 = "</row>";
  26. int pos = line.find(str1, 0);
  27. if(pos !=string:: npos)
  28. continue;
  29. else
  30. pos = line.find(str2, 0);
  31. if(pos != string::npos)
  32. break;
  33. else
  34. rows.push_back(line);
  35. }
  36. }
  37. myfile.close();
  38. }
  39.  
  40. void number_cols(string file)
  41. {
  42. ifstream myfile(file);
  43. if (!myfile)
  44. {
  45. cout << "I could not open the file.\n";
  46. //return EXIT_FAILURE;
  47. }
  48. else
  49. {
  50. string line;
  51.  
  52. vector<string> cols;
  53.  
  54. while (getline( myfile, line ))
  55. {
  56. string str1 = "<col>";
  57. string str2 = "</col>";
  58. int pos = line.find(str1, 0);
  59. if(pos !=string:: npos)
  60. continue;
  61. else
  62. pos = line.find(str2, 0);
  63. if(pos != string::npos)
  64. break;
  65. else
  66. cols.push_back(line);
  67. }
  68. }
  69. myfile.close();
  70. }
  71.  
  72. void get_matrix(string file)
  73. {
  74. /*ifstream data(file, ios::in);
  75.   string line, temp;
  76.   istringstream oss;
  77.   int count = 0;
  78.   vector< vector<string> > DataFromFile;
  79.  
  80.   while(getline(data, line)){
  81.   DataFromFile.push_back( vector<string>() );
  82.   oss.clear();
  83.   oss.str(line);
  84.   while(oss >> temp)
  85.   DataFromFile[count].push_back(temp);
  86.   count++;
  87.   }*/
  88. }
  89.  
  90. int main()
  91. {
  92.  
  93. string file = "words.txt";
  94.  
  95. ifstream myfile(file);
  96. if (!myfile)
  97. {
  98. cout << "I could not open the file. Fooey.\n";
  99. return EXIT_FAILURE;
  100. }
  101. else
  102. {
  103. number_rows(file);
  104. number_cols(file);
  105. //matrix(myfile);
  106. }
  107. return EXIT_SUCCESS;
  108. }
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
Quote ...
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
am i on the right track?
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
AdRock is offline Offline
64 posts
since Dec 2008
Jan 21st, 2009
0

Re: read line of text from file into array

Click to Expand / Collapse  Quote originally posted by AdRock ...
I have been working on this for a little while and have come up with this which isn't perfect by far
C++ Syntax (Toggle Plain Text)
  1. #include <fstream>
  2. #include <iostream>
  3. #include <string>
  4. #include <vector>
  5.  
  6. using namespace std;
  7.  
  8. void number_rows(string file)
  9. {
  10. ifstream myfile(file);
  11. if (!myfile)
  12. {
  13. cout << "I could not open the file.\n";
  14. //return EXIT_FAILURE;
  15. }
  16. else
  17. {
  18. string line;
  19.  
  20. vector<string> rows;
  21.  
  22. while (getline( myfile, line ))
  23. {
  24. string str1 = "<row>";
  25. string str2 = "</row>";
  26. int pos = line.find(str1, 0);
  27. if(pos !=string:: npos)
  28. continue;
  29. else
  30. pos = line.find(str2, 0);
  31. if(pos != string::npos)
  32. break;
  33. else
  34. rows.push_back(line);
  35. }
  36. }
  37. myfile.close();
  38. }
  39.  
  40. void number_cols(string file)
  41. {
  42. ifstream myfile(file);
  43. if (!myfile)
  44. {
  45. cout << "I could not open the file.\n";
  46. //return EXIT_FAILURE;
  47. }
  48. else
  49. {
  50. string line;
  51.  
  52. vector<string> cols;
  53.  
  54. while (getline( myfile, line ))
  55. {
  56. string str1 = "<col>";
  57. string str2 = "</col>";
  58. int pos = line.find(str1, 0);
  59. if(pos !=string:: npos)
  60. continue;
  61. else
  62. pos = line.find(str2, 0);
  63. if(pos != string::npos)
  64. break;
  65. else
  66. cols.push_back(line);
  67. }
  68. }
  69. myfile.close();
  70. }
  71.  
  72. void get_matrix(string file)
  73. {
  74. /*ifstream data(file, ios::in);
  75.   string line, temp;
  76.   istringstream oss;
  77.   int count = 0;
  78.   vector< vector<string> > DataFromFile;
  79.  
  80.   while(getline(data, line)){
  81.   DataFromFile.push_back( vector<string>() );
  82.   oss.clear();
  83.   oss.str(line);
  84.   while(oss >> temp)
  85.   DataFromFile[count].push_back(temp);
  86.   count++;
  87.   }*/
  88. }
  89.  
  90. int main()
  91. {
  92.  
  93. string file = "words.txt";
  94.  
  95. ifstream myfile(file);
  96. if (!myfile)
  97. {
  98. cout << "I could not open the file. Fooey.\n";
  99. return EXIT_FAILURE;
  100. }
  101. else
  102. {
  103. number_rows(file);
  104. number_cols(file);
  105. //matrix(myfile);
  106. }
  107. return EXIT_SUCCESS;
  108. }
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?
As far as the errors you are encountering,
Quote ...
c++ Syntax (Toggle Plain Text)
  1. ifstream myfile(file);
should be replaced with:
c++ Syntax (Toggle Plain Text)
  1. ifstream myfile;
  2. myfile.open(file.c_str());
Last edited by zalezog; Jan 21st, 2009 at 11:16 am.
Reputation Points: 53
Solved Threads: 13
Light Poster
zalezog is offline Offline
47 posts
since Oct 2008
Jan 21st, 2009
0

Re: read line of text from file into array

or
C++ Syntax (Toggle Plain Text)
  1. ifstream myfile(file.c_str());
Reputation Points: 53
Solved Threads: 13
Light Poster
zalezog is offline Offline
47 posts
since Oct 2008
Jan 21st, 2009
0

Re: read line of text from file into array

@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.
Reputation Points: 344
Solved Threads: 116
Practically a Master Poster
Murtan is offline Offline
670 posts
since May 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: command time output in Linux
Next Thread in C++ Forum Timeline: How to Read text file into array and skip the delimeters





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC