read line of text from file into array

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Oct 2008
Posts: 44
Reputation: zalezog is an unknown quantity at this point 
Solved Threads: 11
zalezog zalezog is offline Offline
Light Poster

Re: read line of text from file into array

 
0
  #11
Jan 16th, 2009

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..
  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.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 670
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: read line of text from file into array

 
0
  #12
Jan 16th, 2009
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.
  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.
Knowledge is power -- But experience is everything
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 57
Reputation: AdRock is an unknown quantity at this point 
Solved Threads: 0
AdRock AdRock is offline Offline
Junior Poster in Training

Re: read line of text from file into array

 
0
  #13
Jan 21st, 2009
I've come back to this cos it was doing my head in

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'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
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 57
Reputation: AdRock is an unknown quantity at this point 
Solved Threads: 0
AdRock AdRock is offline Offline
Junior Poster in Training

Re: read line of text from file into array

 
0
  #14
Jan 21st, 2009
I have been working on this for a little while and have come up with this which isn't perfect by far
  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
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?
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 44
Reputation: zalezog is an unknown quantity at this point 
Solved Threads: 11
zalezog zalezog is offline Offline
Light Poster

Re: read line of text from file into array

 
0
  #15
Jan 21st, 2009
Originally Posted by AdRock View Post
I have been working on this for a little while and have come up with this which isn't perfect by far
  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,
  1. ifstream myfile(file);
should be replaced with:
  1. ifstream myfile;
  2. myfile.open(file.c_str());
Last edited by zalezog; Jan 21st, 2009 at 11:16 am.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 44
Reputation: zalezog is an unknown quantity at this point 
Solved Threads: 11
zalezog zalezog is offline Offline
Light Poster

Re: read line of text from file into array

 
0
  #16
Jan 21st, 2009
or
  1. ifstream myfile(file.c_str());
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 538
Reputation: Murtan is a jewel in the rough Murtan is a jewel in the rough Murtan is a jewel in the rough Murtan is a jewel in the rough 
Solved Threads: 86
Murtan Murtan is offline Offline
Posting Pro

Re: read line of text from file into array

 
0
  #17
Jan 21st, 2009
@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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC