View Single Post
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
  #7
Jan 15th, 2009
Many thanks for helping me out....i've only been doing C++ for a month

Have i got the code right? It's complaining about 2 undeclared variables

"str" and "array_words"

Once i can get those words or whatever is in the file into my arrays i should be on a roll

  1. #include <fstream>
  2. #include <iostream>
  3. #include <string>
  4. #include <cstdio>
  5.  
  6. using namespace std;
  7.  
  8. int main()
  9. {
  10. string GetFileName;
  11. string line;
  12.  
  13. cout << "System >> Enter file to read: \n";
  14.  
  15. cout << "User >> ";
  16. cin >> GetFileName;
  17.  
  18. ifstream filename( GetFileName.c_str() );
  19.  
  20. if(! filename )
  21. {
  22. cout << "Unable to open file: " << GetFileName << endl;
  23.  
  24. return EXIT_FAILURE;
  25. }
  26. else
  27. {
  28. //fragment code
  29. string get_content[10][BUFSIZ];
  30. string words_per_line[5];
  31. //if memory isn't your constraint
  32. //if there are 10 lines in your file
  33. //and if there are more than 5 words in your file
  34. int count_line=0;
  35. int count_word=0;
  36.  
  37. while(getline(filename, line))
  38. {
  39. for(int i=0;line[i];++i)
  40. str[i]=line[i];
  41.  
  42. char * pch=NULL;
  43. char str[BUFSIZ]={0} ;
  44.  
  45. pch = strtok (str," ");
  46. count_word=0;//reset after each line
  47. while (pch!=NULL)
  48. {
  49. get_content[count_line][count_word]=pch;
  50. pch = strtok (NULL, " ");
  51.  
  52. array_words[count_line]=++count_word;
  53. }
  54. ++count_line;
  55. }
  56. filename.close();
  57. }
  58. return EXIT_SUCCESS;
  59. }
Reply With Quote