| | |
read line of text from file into array
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Dec 2008
Posts: 57
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:
C++ Syntax (Toggle Plain Text)
char str[] = getline(filename, line);
error message:
•
•
•
•
error c2440: 'initializing' : cannot convert from 'std::basic_istream<_Elem,_Traits>' to 'char[]'
C++ Syntax (Toggle Plain Text)
#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; }
•
•
Join Date: Oct 2008
Posts: 44
Reputation:
Solved Threads: 11
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
C++ Syntax (Toggle Plain Text)
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 4:13 pm.
•
•
Join Date: Dec 2008
Posts: 57
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
•
•
Join Date: Jan 2008
Posts: 3,828
Reputation:
Solved Threads: 501
•
•
•
•
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)
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)
#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. C++ Syntax (Toggle Plain Text)
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.
•
•
Join Date: Oct 2008
Posts: 44
Reputation:
Solved Threads: 11
•
•
•
•
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
C++ Syntax (Toggle Plain Text)
pch=strtok(str,"\n");
I don't understand,when you say
•
•
•
•
tweaking the code
then something like this might help:
C++ Syntax (Toggle Plain Text)
//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;
C++ Syntax (Toggle Plain Text)
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
•
•
Join Date: Dec 2008
Posts: 57
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
C++ Syntax (Toggle Plain Text)
#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; }
•
•
Join Date: May 2008
Posts: 576
Reputation:
Solved Threads: 93
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]
•
•
Join Date: Oct 2008
Posts: 44
Reputation:
Solved Threads: 11
•
•
•
•
That code sure looks like a strcpy() to me, maybe you should look into it.
C++ Syntax (Toggle Plain Text)
string line; char str[BUFSIZ];
C++ Syntax (Toggle Plain Text)
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
C++ Syntax (Toggle Plain Text)
strtok(..,..)
string line and got stuck, that's how the whole thread began. Last edited by zalezog; Jan 16th, 2009 at 5:47 am.
![]() |
Similar Threads
- inputing a text file into an array (C++)
- 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)
- 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: How to Read text file into array and skip the delimeters
| Thread Tools | Search this Thread |
api array arrays based binary bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete deploy developer dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game generator getline givemetehcodez graph gui homeworkhelp iamthwee ifstream input int java lib linkedlist linker list loop looping loops map math matrix memory multiple news node number numbertoword output pointer problem program programming project python random read recursion recursive reference rpg sorting string strings temperature template test text text-file tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






