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

read line of text from file into array

 
0
  #1
Jan 14th, 2009
I'm trying to get each line of text of a file split into words and then put into an array

I found an exmaple oc cpluscplus.com and their example works but when i try to edit to make changes to read the line of text from a file i get an error on this line:
  1. char str[] = getline(filename, line);

error message:
error c2440: 'initializing' : cannot convert from 'std::basic_istream<_Elem,_Traits>' to 'char[]'
I need to be able to read in a line and spilt the words into an array so i can output them

  1. #include <fstream>
  2. #include <iostream>
  3. #include <string>
  4. #include <stdio.h>
  5.  
  6. using namespace std;
  7.  
  8. int main()
  9. {
  10. string GetFileName;
  11.  
  12. cout << "System >> Enter file to read: \n";
  13.  
  14. cout << "User >> ";
  15. cin >> GetFileName;
  16.  
  17. ifstream filename( GetFileName.c_str() );
  18.  
  19. if(! filename )
  20. {
  21. cout << "Unable to open file: " << GetFileName << endl;
  22.  
  23. return EXIT_FAILURE;
  24. }
  25. else
  26. {
  27. while (getline(filename, line)) //Loop through lines
  28. {
  29. char str[] = getline(filename, line);
  30. char * pch;
  31.  
  32. pch = strtok (str," ");
  33. while (pch != NULL)
  34. {
  35. printf ("%s\n",pch);
  36. pch = strtok (NULL, " ");
  37. }
  38. }
  39. filename.close();
  40. }
  41. return EXIT_SUCCESS;
  42. }
Reply With Quote