943,922 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 8812
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Jan 14th, 2009
0

read line of text from file into array

Expand Post »
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:
C++ Syntax (Toggle Plain Text)
  1. char str[] = getline(filename, line);

error message:
Quote ...
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

C++ Syntax (Toggle Plain Text)
  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. }
Similar Threads
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
AdRock is offline Offline
64 posts
since Dec 2008
Jan 14th, 2009
0

Re: read line of text from file into array

Why can't you use a vector? It would be a lot less painful.
Team Colleague
Reputation Points: 361
Solved Threads: 214
Taboo Programmer
Comatose is offline Offline
2,413 posts
since Dec 2004
Jan 14th, 2009
0

Re: read line of text from file into array

Though ,not a good approach but the following approach does work.(if your input contains texts which are separated by more than a couple of newlines,expect some unexpected characters in the output)

Inside the while loop
C++ Syntax (Toggle Plain Text)
  1. while (getline(filename, line)) //Loop through lines
  2. {
  3. char str[BUFSIZ] ;//BUFSIZ
  4. // already defined in stdio.h
  5.  
  6. for(int i=0;line[i];++i)
  7. str[i]=line[i];
  8.  
  9.  
  10. char * pch=NULL;
  11.  
  12. pch = strtok (str," ");
  13. while (pch!=NULL)
  14. {
  15. printf ("%s\n",pch);
  16. pch = strtok (NULL, " ");
  17. }
  18. }
Last edited by zalezog; Jan 14th, 2009 at 4:13 pm.
Reputation Points: 53
Solved Threads: 13
Light Poster
zalezog is offline Offline
47 posts
since Oct 2008
Jan 14th, 2009
0

Re: read line of text from file into array

Thanks zalezog

I'm actually getting somewhere.

It does loop through each of the lines but it only outputs the first "token" on each line

This is an example of what i need to get from my file

1.4 0 0.5
2.3 1.7 0.1
0.8 0 0
0.7 1.0 0.2
1.2 1.3 0.5

Is there a way of tweaking the code cos the output is going to be changed later anyway but I need to get the first line into the array pointers [0][0], [0][1],[0][2] etc and the same for the other lines so i can select 2 array pointers and an display the contents
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
AdRock is offline Offline
64 posts
since Dec 2008
Jan 14th, 2009
0

Re: read line of text from file into array

Click to Expand / Collapse  Quote originally posted by AdRock ...
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:
C++ Syntax (Toggle Plain Text)
  1. char str[] = getline(filename, line);

error message:


I need to be able to read in a line and spilt the words into an array so i can output them

C++ Syntax (Toggle Plain Text)
  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. }

Is this the whole program? I don't see where line is declared. You have two getline statements when you only need one.

C++ Syntax (Toggle Plain Text)
  1. while (getline(filename, line)) //Loop through lines
  2. {
  3. char str[] = getline(filename, line);

The second one, even if it was syntactically correct, which it is not, overwrites line , which I imagine is supposed to be a string.

You can use char* or string with getline:

http://www.cplusplus.com/reference/i...m/getline.html
http://www.cplusplus.com/reference/string/getline.html

The links above have examples of each.
Featured Poster
Reputation Points: 2614
Solved Threads: 687
Posting Expert
VernonDozier is offline Offline
5,375 posts
since Jan 2008
Jan 15th, 2009
0

Re: read line of text from file into array

Click to Expand / Collapse  Quote originally posted by AdRock ...
Thanks zalezog

I'm actually getting somewhere.

It does loop through each of the lines but it only outputs the first "token" on each line

This is an example of what i need to get from my file

1.4 0 0.5
2.3 1.7 0.1
0.8 0 0
0.7 1.0 0.2
1.2 1.3 0.5

Is there a way of tweaking the code cos the output is going to be changed later anyway but I need to get the first line into the array pointers [0][0], [0][1],[0][2] etc and the same for the other lines so i can select 2 array pointers and an display the contents
If you want the whole line to be printed ,then replace every call with..
C++ Syntax (Toggle Plain Text)
  1. pch=strtok(str,"\n");

I don't understand,when you say
Quote ...
tweaking the code
If you want to store each word in your file(if that's what you mean)
then something like this might help:
C++ Syntax (Toggle Plain Text)
  1. //fragment code
  2. string get_content[10][BUFSIZ];
  3. string words_per_line[5];
  4. //if memory isn't your constraint
  5. //if there are 10 lines in your file
  6. //and if there are more than 5 words in your file
  7. int count_line=0;
  8. int count_word=0;
Then update them in your 'while' loop

C++ Syntax (Toggle Plain Text)
  1. while(getline(filename, line))
  2. {
  3. for(int i=0;line[i];++i)
  4. str[i]=line[i];
  5.  
  6.  
  7. char * pch=NULL;
  8. char str[BUFSIZ]={0} ;
  9.  
  10.  
  11. pch = strtok (str," ");
  12. count_word=0;//reset after each line
  13. while (pch!=NULL)
  14. {
  15.  
  16. get_content[count_line][count_word]=pch;
  17. pch = strtok (NULL, " ");
  18.  
  19. array_words[count_line]=++count_word;
  20. }
  21. ++count_line;
  22. }
and display them using loops whose control variables are
count_word
count_line
Reputation Points: 53
Solved Threads: 13
Light Poster
zalezog is offline Offline
47 posts
since Oct 2008
Jan 15th, 2009
0

Re: read line of text from file into array

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

C++ Syntax (Toggle Plain Text)
  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. }
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
AdRock is offline Offline
64 posts
since Dec 2008
Jan 15th, 2009
0

Re: read line of text from file into array

If the compiler is complaining, the code is obviously still not right.

Maybe you should define the missing symbols?

try int array_words[10]; right next to where you declare get_content. (They're both used in the same place.)

The declaration for char str[BUFSIZ] needs to come before the for loop that copies into it:
c++ Syntax (Toggle Plain Text)
  1. for(int i =0; line[i];++i)
  2. str[i]=line[i];

That code sure looks like a strcpy() to me, maybe you should look into it.

PS- When posting c++ code, please use c++ code tags
[code=c++]
// Your code here
[/code]
Reputation Points: 344
Solved Threads: 116
Practically a Master Poster
Murtan is offline Offline
670 posts
since May 2008
Jan 16th, 2009
0

Re: read line of text from file into array

Quote ...
That code sure looks like a strcpy() to me, maybe you should look into it.
C++ Syntax (Toggle Plain Text)
  1. string line;
  2. char str[BUFSIZ];
1. Murtan is absolutely right when he says that declaration of
C++ Syntax (Toggle Plain Text)
  1. str[BUFSIZ];
  2. int array_words[10];
should be before the loop where the content of 'str' is copied into 'line',same goes with array_words[10]
2.As the declarations of 'str' and 'line' are not ,C-styled strings,i had to use a for loop.Perhaps, AdRock tried using
C++ Syntax (Toggle Plain Text)
  1. strtok(..,..)
with string line and got stuck, that's how the whole thread began.
Last edited by zalezog; Jan 16th, 2009 at 5:47 am.
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

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()) ?
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