Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Reading in files

Status
Not open for further replies.

ferryjenny

Technical User
Nov 22, 2000
24
GB
I have a text file that i want to open in an edit box however when i try to do this only the first line of the file is displayed in the box.

Any suggestions as to how to make this work would be greatly appreciated.
 
First make sure you have the 'multi-line' property set on the edit box.

I would need to see some code to offer more than that.

-pete
 
I am using the following code

FILE* stream;

if( (stream = fopen("standletter.txt", "r") )== NULL)
{
interfaceDialog.m_writing_area="has not worked";
}

CString text;
char temp[200];


text="";
strcpy(temp,"");

do{
text=text+temp;
fgets(temp,200,stream);

}while(strcmp(temp,"~#")!=0&&strcmp(temp,"~#\n")!=0);

interfaceDialog.m_writing_area=text;

m_writing_area is the edit box

This opens the file however it only displays the first line of text in the file. It dosent seem to be able to deal with the rest of the text.
 
> only the first line of the file is displayed in the box.

That's not what happens when I run it! I see all the lines separated by a 0x0A character. The reason is that fgets() is stripping the 0x0D character. This causes the edit box to display the 0x0A character as a bar symbol with the next line from the file immediately following that character.

-pete
 
The reason is that fgets() is stripping the 0x0D character.

Is there any way that I can fix this and display the text file in the edit box with multiple lines.
 
> Is there any way that I can fix this

I don't know if you can alter the behavior of fgets() but you can certainly manipulate your own data can't you? Perhaps a quick overview of your background would help us decide how much information you need for a given solution.


CString buf;
char * temp;
do{
if( 0 < buf.GetLength()){
buf.Remove(0x0A); // remove the remaining LF character
buf += &quot;\r\n&quot;; // add a complete CRLF sequence
_editView += buf;
}
temp = buf.GetBufferSetLength(200);
fgets(temp,200,stream);
buf.ReleaseBuffer(-1);
}while(strcmp(temp,&quot;~#&quot;)!=0&amp;&amp;strcmp(temp,&quot;~#\n&quot;)!=0);


Hope this helps
-pete
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top