read line of text from file into array
Please support our C++ advertiser: Programming Forums
![]() |
•
•
Posts: 34
Reputation:
Solved Threads: 0
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:
error message:
I need to be able to read in a line and spilt the words into an array so i can output them
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;
}•
•
Posts: 30
Reputation:
Solved Threads: 5
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
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.
•
•
Posts: 34
Reputation:
Solved Threads: 0
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
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
•
•
Posts: 2,903
Reputation:
Solved Threads: 368
•
•
•
•
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.
•
•
Posts: 30
Reputation:
Solved Threads: 5
•
•
•
•
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
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;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;
} count_word count_line
•
•
Posts: 34
Reputation:
Solved Threads: 0
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
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;
}•
•
Posts: 524
Reputation:
Solved Threads: 78
If the compiler is complaining, the code is obviously still not right.
Maybe you should define the missing symbols?
try
The declaration for
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]
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)
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]
•
•
Posts: 30
Reputation:
Solved Threads: 5
•
•
•
•
That code sure looks like a strcpy() to me, maybe you should look into it.
string line; char str[BUFSIZ];
str[BUFSIZ]; int 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(..,..)
string line and got stuck, that's how the whole thread began. Last edited by zalezog : Jan 16th, 2009 at 4:47 am.
![]() |
Similar Threads
Other Threads in the C++ Forum
- Reading from text file into array (C++)
- Inputting text file into array and finding top 3 values (C++)
- cast text file to an array (C#)
- how to read data from text file into data grid??? (VB.NET)
- inputing a text file into an array (C++)
- Read a specific line from a text file - how to ? (Java)
- Putting Auto Numbers in a File (C#)
- Help Reading Info in Text File Into an Array (C++)
Other Threads in the C++ Forum
- Previous Thread: command time output in Linux
- Next Thread: inherit class from structure
•
•
•
•
Views: 2111 | Replies: 16 | Currently Viewing: 1 (0 members and 1 guests)






Linear Mode