getline problem

Started by
7 comments, last by dirkduck 22 years, 1 month ago
Hey everyone. Im using a 'file.getline' to grab a line of text from a file and write it into another file. The problem is that it wont grab the full line in the file. Here is some code: (outline and i_outline are 2 files streams created with ifstream and ofstream)
      
	char param1[256]={0};  //the parameter that holds the text

	char quote='"';  //used to write in quotes

	output<<"blah "<<quote;
                for(i=0;i<i2;i++)
	{
		output<<param1[i];
	}

	output<<quote<<"blah"<<endl;
  
  
This is where its written to the file, then later we want to transfer it to another file:
      
char *file_line=new char[256];

	while(i_output.eof()==false)  //loop while the file isn't done

	{
		i_output.getline(file_line,256);  //grab a line


		o_main<<file_line<<endl;  //output it to the other file

		
	}

      
Now, what I want this code to do, is say that 'param1' held the text 'hello', then it should write to the first file: "blah"hello"blah" but all it writes is blah". Now, I thought that getline will read a while line in a file until it reaches '\n' (by default). When I wrote to the first file, I didnt use 'endl' until the final "blah", so I dont see why it isn't writing the whole line. Anyone know? Thanks http://www.labino.net - freeware and shareware games. Edited by - dirkduck on March 24, 2002 7:54:55 PM Edited by - dirkduck on March 24, 2002 7:55:50 PM Edited by - dirkduck on March 24, 2002 7:56:41 PM Edited by - dirkduck on March 24, 2002 7:57:22 PM
http://www.labino.net
Advertisement
Look very closely at the source you posted.
Notice how the colors are not the way they are supposed to be?
You can''t print out ''"'' directly, it''s an escape sequence,
change it to

''\"''

you also have to do that to single quotes as well.

There''s lots of code I haven''t seen such as assigning values to i and i2 and where "hello" gets put into param1, so I assume that everything else is okay.
Well, I just tried changing 'quote' to equal '\"', but it didn't seem to change it at all. I even took all of the 'quotes' out, and it didnt help. Also, I was messing around with it some more, and I printed out every character in the initial for..loop that fills in each character of 'param1', and it printed out what it was suppost to just fine, did it like this:

    			while(line[quote_slot+i2]!='"')  //start another loop to find the end quote			{								i2++;				param1[i2]=line[quote_slot+i2];  //fill the param array with the text								cout <<param1[i2];  //write the string for debug - THIS SPELLS IT OUT PERFECTLY							}     

but then right after that loop I added a

  cout<<param1;  

and it came out blank. Why it does that stumps me.

http://www.labino.net -
freeware and shareware games.

[edited by - dirkduck on March 24, 2002 12:58:02 AM]
http://www.labino.net
Is param1 been null-terminated? Or is param1 accidentally started with a null?
Well, param1 is defined like this:

char param1[256]={0};

but I can also define it like:

char *param1=new char[256];

and I get the same problem. Am I missing to do something with it? (im prettymuch a beginner at string/chracter array''s), thanks.

http://www.labino.net -
freeware and shareware games.
http://www.labino.net
quote:Original post by RolandofGilead
Notice how the colors are not the way they are supposed to be?

That''s because GDNet''s source tag doesn''t handle nested strings or single quotes that well.

quote:You can''t print out ''"'' directly, it''s an escape sequence...

Not true. You can''t print it out directly in a double quoted string (since it''s the character that demarcates the string), but you can single quote it just fine.

dirkduck: Which version of iostream are you using? I''d advise the new version - the header without the .h extension. Also, if you''re using MSVC6, getline is screwy by default. You''ll need to apply these fixes.

Now, here''s a sliver of code that should work perfectly:

  #include <iostream>void WriteText( char *text, char *filename ){  std::ofstream fout( filename, std::ios_base::out|std::ios_base::app );  if( fout.fail() )    return;  fout << ''"'' << text << ''"'' << endl;  fout.close();}void ReadText( char * text, char *filename ){  // text had better be big enough!  std::ifstream fin( filename );  if( fin.fail() )    return;  int i = 0;  while( !fin.eof() )  {    std::getline( fin, text[i] );    i = strlen( text );  }  fin.close();}  


Finally, any special reason you''re not using std::string?

[ GDNet Start Here | GDNet Search Tool | GDNet FAQ | MS RTFM [MSDN] | SGI STL Docs | Google! ]
Thanks to Kylotan for the idea!
Hey. Well, I just switched to plain iostream, but so far I havn''t noticed any changes. About the fix pack thing, it says its 90$, is there some ''lite'' version or something thats free? Also, about the code, the problem is that if the strings are written in different sections, like I am right now, it wont work. If I write it all in 1 go, then it works fine, but I can''t do that as the ''param1'' becomes empty for some reason. Trying your code from ReadText, I get the error ''getline not a member of std'' or something, so im not sure.

http://www.labino.net -
freeware and shareware games.
http://www.labino.net
quote:Original post by dirkduck
Hey. Well, I just switched to plain iostream, but so far I havn''t noticed any changes. About the fix pack thing, it says its 90$, is there some ''lite'' version or something thats free?

If you read the whole page, you would have seen that the fixes are plain text instructions you need to apply yourself (ie, jump in and change library source files).

quote:
Also, about the code, the problem is that if the strings are written in different sections, like I am right now, it wont work. If I write it all in 1 go, then it works fine, but I can''t do that as the ''param1'' becomes empty for some reason.

I have no idea what you''re talking about. Use null-terminated strings, rather than this "character array" design.
{  char text[256];  text[0] = ''\0'';          // null-termination  strcpy( text, "hello" ); // specify string  std::cout << text << std::endl;} 


quote:Trying your code from ReadText, I get the error ''getline not a member of std'' or something, so im not sure.

You probably included the wrong header. Make sure you include <iostream> (and <fstream> for file I/O), NOT <iostream.h>.

[ GDNet Start Here | GDNet Search Tool | GDNet FAQ | MS RTFM [MSDN] | SGI STL Docs | Google! ]
Thanks to Kylotan for the idea!
Ok, I got it working. Thanks.

http://www.labino.net -
freeware and shareware games.
http://www.labino.net

This topic is closed to new replies.

Advertisement