LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Output display in c++ (https://www.linuxquestions.org/questions/programming-9/output-display-in-c-787818/)

gregarion 02-08-2010 12:40 PM

Output display in c++
 
Hey guys, i am having problem with displaying my text. my text file is displayed in such a way and is called test.......
Code:

Sam Worthington ... Jake SullyasZoe Saldana ... NeytiriasSigourney Weaver ... Dr. Grace AugustineasStephen Lang ... Colonel Miles QuaritchasJoel Moore ... Norm Spellman (as Joel David Moore)asGiovanni Ribisi ... Parker SelfridgeasMichelle Rodriguez ... Trudy ChaconasLaz Alonso ... Tsu'teyasWes Studi ... EytukanasCCH Pounder ... MoatasDileep Rao ... Dr. Max PatelasMatt Gerald ... Corporal Lyle WainfleetasSean Anthony Moran ... Private FikeasJason Whyte ... Cryo Vault Med TechasScott Lawrence ... Venture Star Crew Chiefmore
What i am trying to do is for the program to read "as" and then from there start a new line... thus the expected output is...


Code:


Sam Worthington ... Jake Sully
Zoe Saldana ... Neytiri
Sigourney Weaver ... Dr. Grace Augustine
Stephen Lang ... Colonel Miles Quaritch
Joel Moore ... Norm Spellman (as Joel David Moore)
.........(and so on)




This is my coding..



Code:

string templine ;
string line;
ifstream myfile ("test");


while (getline (myfile,templine) )
line.append(templine);

char str [] = line ;
char delims[] = " ";
char *result = NULL;
result = strtok( str, delims );
while( result != NULL ) {
printf( result );
result = strtok( NULL, delims );
}



I keep getting the error saying ...

Code:

editmain.cpp.98:error: initializer fails to determine size of 'str'

what does this mean, and how can i be able to format my file in the way i want?

David1357 02-08-2010 02:29 PM

Quote:

Originally Posted by gregarion (Post 3856915)
Code:

editmain.cpp.98:error: initializer fails to determine size of 'str'

There are not 98 lines of code in the sample you provided.

To solve the problem mentioned in the error, look at the place where "str" is initialized.

johnsfine 02-08-2010 03:18 PM

I can guess which line is 98
Code:

char str [] = line ;
But I can't guess what you hope to accomplish with that line. You seem to be asking the compiler at compile time to know the run time contents of a string.

Probably you want
Code:

char* str = line.c_str() ;
But I'm far from sure of that. If you use c_str that way, be aware that is not copying the contents of the string, just pointing to them. So str will be valid only as long as line doesn't get modified. That seems to fit the way you use str in strtok, but you haven't shown enough code for me to be sure you don't modify line before you are done with str.


All times are GMT -5. The time now is 12:01 AM.