DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   C++ (http://www.daniweb.com/forums/forum8.html)
-   -   read line of text from file into array (http://www.daniweb.com/forums/thread168296.html)

AdRock Jan 14th, 2009 7:10 am
read line of text from file into array
 
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:
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

#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;
}

Comatose Jan 14th, 2009 9:23 am
Re: read line of text from file into array
 
Why can't you use a vector? It would be a lot less painful.

zalezog Jan 14th, 2009 4:09 pm
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
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, " ");
                        }
}

AdRock Jan 14th, 2009 5:40 pm
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

VernonDozier Jan 14th, 2009 8:31 pm
Re: read line of text from file into array
 
Quote:

Originally Posted by AdRock (Post 778044)
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.

zalezog Jan 15th, 2009 2:56 pm
Re: read line of text from file into array
 
Quote:

Originally Posted by AdRock (Post 778462)
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
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:
//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

AdRock Jan 15th, 2009 6:19 pm
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

#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;
}

Murtan Jan 15th, 2009 6:39 pm
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:
for(int i =0; line[i];++i)
    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]

zalezog Jan 16th, 2009 5:44 am
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.

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.:icon_lol:

Murtan Jan 16th, 2009 10:35 am
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())
?


All times are GMT -4. The time now is 4:56 am.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC