RSS Forums RSS

read line of text from file into array

Please support our C++ advertiser: Programming Forums
Reply
Posts: 34
Reputation: AdRock is an unknown quantity at this point 
Solved Threads: 0
AdRock AdRock is offline Offline
Light Poster

read line of text from file into array

  #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:
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

#include <fstream>
#include <iostream>
#include <string>
#include <stdio.h>

using namespace std;

int main()
{
	string GetFileName;

	cout << "System >> Enter file to read: \n";

	cout  << "User >> ";
	cin >> GetFileName;
	
	ifstream filename( GetFileName.c_str() );

	if(! filename )
	{
		cout << "Unable to open file: " << GetFileName << endl;

		return EXIT_FAILURE;
	}
	else
	{
		while (getline(filename, line)) //Loop through lines
		{
			char str[] = getline(filename, line);
			char * pch;
			
			pch = strtok (str," ");
			while (pch != NULL)
			{
			printf ("%s\n",pch);
			pch = strtok (NULL, " ");
			}
		}	
		filename.close();
	}
	return EXIT_SUCCESS;
}
AddThis Social Bookmark Button
Reply With Quote  
Posts: 2,404
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: 209
Colleague
Comatose's Avatar
Comatose Comatose is offline Offline
Taboo Programmer

Re: read line of text from file into array

  #2  
Jan 14th, 2009
Why can't you use a vector? It would be a lot less painful.
Reply With Quote  
Posts: 30
Reputation: zalezog is an unknown quantity at this point 
Solved Threads: 5
zalezog zalezog is offline Offline
Light Poster

Re: read line of text from file into array

  #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
while (getline(filename, line)) //Loop through lines
		{
			char str[BUFSIZ] ;//BUFSIZ 
 // already defined in stdio.h 
	
            for(int i=0;line[i];++i)
			str[i]=line[i];
            
            
            char * pch=NULL;
			
			pch = strtok (str," ");
			while (pch!=NULL)
			{
			printf ("%s\n",pch);
			pch = strtok (NULL, " ");
			}
}
Last edited by zalezog : Jan 14th, 2009 at 3:13 pm.
Reply With Quote  
Posts: 34
Reputation: AdRock is an unknown quantity at this point 
Solved Threads: 0
AdRock AdRock is offline Offline
Light Poster

Re: read line of text from file into array

  #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  
Posts: 2,903
Reputation: VernonDozier has much to be proud of VernonDozier has much to be proud of VernonDozier has much to be proud of VernonDozier has much to be proud of VernonDozier has much to be proud of VernonDozier has much to be proud of VernonDozier has much to be proud of VernonDozier has much to be proud of VernonDozier has much to be proud of 
Solved Threads: 368
VernonDozier VernonDozier is offline Offline
Posting Maven

Re: read line of text from file into array

  #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:
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

#include <fstream>
#include <iostream>
#include <string>
#include <stdio.h>

using namespace std;

int main()
{
	string GetFileName;

	cout << "System >> Enter file to read: \n";

	cout  << "User >> ";
	cin >> GetFileName;
	
	ifstream filename( GetFileName.c_str() );

	if(! filename )
	{
		cout << "Unable to open file: " << GetFileName << endl;

		return EXIT_FAILURE;
	}
	else
	{
		while (getline(filename, line)) //Loop through lines
		{
			char str[] = getline(filename, line);
			char * pch;
			
			pch = strtok (str," ");
			while (pch != NULL)
			{
			printf ("%s\n",pch);
			pch = strtok (NULL, " ");
			}
		}	
		filename.close();
	}
	return EXIT_SUCCESS;
}



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

		while (getline(filename, line)) //Loop through lines
		{
			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  
Posts: 30
Reputation: zalezog is an unknown quantity at this point 
Solved Threads: 5
zalezog zalezog is offline Offline
Light Poster

Re: read line of text from file into array

  #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..
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:
//fragment code
string get_content[10][BUFSIZ];
    string words_per_line[5];
    //if memory isn't your constraint
	//if there are 10 lines in your file 
	//and if there are more than 5 words in your file
int count_line=0;
	 int count_word=0;
Then update them in your 'while' loop

while(getline(filename, line))
{
 for(int i=0;line[i];++i)
str[i]=line[i];
            
            
 char * pch=NULL;
char str[BUFSIZ]={0} ;


pch = strtok (str," ");
count_word=0;//reset after each line
while (pch!=NULL)
{
			
		get_content[count_line][count_word]=pch;
            pch = strtok (NULL, " ");
			
		array_words[count_line]=++count_word;
}
		++count_line;
        }	
and display them using loops whose control variables are
count_word
count_line
Reply With Quote  
Posts: 34
Reputation: AdRock is an unknown quantity at this point 
Solved Threads: 0
AdRock AdRock is offline Offline
Light Poster

Re: read line of text from file into array

  #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

#include <fstream>
#include <iostream>
#include <string>
#include <cstdio>

using namespace std;

int main()
{
	string GetFileName;
	string line;

	cout << "System >> Enter file to read: \n";

	cout  << "User >> ";
	cin >> GetFileName;
	
	ifstream filename( GetFileName.c_str() );

	if(! filename )
	{
		cout << "Unable to open file: " << GetFileName << endl;

		return EXIT_FAILURE;
	}
	else
	{
		//fragment code
		string get_content[10][BUFSIZ];
		string words_per_line[5];
		//if memory isn't your constraint
		//if there are 10 lines in your file 
		//and if there are more than 5 words in your file
		int count_line=0;
		int count_word=0;
	 
		while(getline(filename, line))
		{
			for(int i=0;line[i];++i)
				str[i]=line[i];
                       
				char * pch=NULL;
				char str[BUFSIZ]={0} ;

				pch = strtok (str," ");
				count_word=0;//reset after each line
				while (pch!=NULL)
				{	
					get_content[count_line][count_word]=pch;
					pch = strtok (NULL, " ");
						
					array_words[count_line]=++count_word;
				}
			++count_line;
        }	
		filename.close();
	}
	return EXIT_SUCCESS;
}
Reply With Quote  
Posts: 524
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: 78
Murtan Murtan is offline Offline
Posting Pro

Re: read line of text from file into array

  #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  
Posts: 30
Reputation: zalezog is an unknown quantity at this point 
Solved Threads: 5
zalezog zalezog is offline Offline
Light Poster

Re: read line of text from file into array

  #9  
Jan 16th, 2009
That code sure looks like a strcpy() to me, maybe you should look into it.


string line;
char str[BUFSIZ];
1. Murtan is absolutely right when he says that declaration of
str[BUFSIZ];
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
strtok(..,..)
with string line and got stuck, that's how the whole thread began.
Last edited by zalezog : Jan 16th, 2009 at 4:47 am.
Reply With Quote  
Posts: 524
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: 78
Murtan Murtan is offline Offline
Posting Pro

Re: read line of text from file into array

  #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  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.



Views: 2111 | Replies: 16 | Currently Viewing: 1 (0 members and 1 guests)

 

Thread Tools Display Modes
Forums | Blogs | Tutorials | Code Snippets | Whitepapers | RSS Feeds | Advertising
All times are GMT -4. The time now is 2:53 pm.
Newsletter Archive - Sitemap - Privacy Statement - Acceptable Use Policy - Contact Us
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC