read line of text from file into array

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

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 Quick reply to this message  
Join Date: Dec 2004
Posts: 2,413
Reputation: Comatose is a jewel in the rough Comatose is a jewel in the rough Comatose is a jewel in the rough Comatose is a jewel in the rough 
Solved Threads: 211
Team Colleague
Comatose's Avatar
Comatose Comatose is offline Offline
Taboo Programmer

Re: read line of text from file into array

 
0
  #2
Jan 14th, 2009
Why can't you use a vector? It would be a lot less painful.
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
  #3
Jan 14th, 2009
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
  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.
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
  #4
Jan 14th, 2009
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
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,819
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 501
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: read line of text from file into array

 
0
  #5
Jan 14th, 2009
Originally Posted by AdRock View 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:
  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

  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.

  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.
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
  #6
Jan 15th, 2009
Originally Posted by AdRock View Post
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..
  1. pch=strtok(str,"\n");

I don't understand,when you say
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:
  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

  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
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
  #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 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
  #8
Jan 15th, 2009
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:
  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]
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
  #9
Jan 16th, 2009
That code sure looks like a strcpy() to me, maybe you should look into it.
  1. string line;
  2. char str[BUFSIZ];
1. Murtan is absolutely right when he says that declaration of
  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
  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.
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
  #10
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()) ?
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