![]() |
| ||
| 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:
#include <fstream> |
| ||
| Re: read line of text from file into array Why can't you use a vector? It would be a lot less painful. |
| ||
| 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 |
| ||
| 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 |
| ||
| Re: read line of text from file into array Quote:
Is this the whole program? I don't see where lineis declared. You have two getlinestatements when you only need one. while (getline(filename, line)) //Loop through lines 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. |
| ||
| Re: read line of text from file into array Quote:
pch=strtok(str,"\n"); I don't understand,when you say Quote:
then something like this might help: //fragment codeThen update them in your 'while' loop while(getline(filename, line))and display them using loops whose control variables are count_word |
| ||
| 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> |
| ||
| 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) 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] |
| ||
| Re: read line of text from file into array Quote:
string line;1. Murtan is absolutely right when he says that declaration of str[BUFSIZ];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 lineand got stuck, that's how the whole thread began.:icon_lol: |
| ||
| 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