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 wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Missing spaces 1

Status
Not open for further replies.

deepsheep

Programmer
Sep 13, 2002
154
CA
I am pretty inexperianced with C++ Builder and can't find an answer to my problem elsewhere. I have a bitmap that I am overlaying text. I take the text from a file, manipulate it, put it on the bitmap and print it all. Somewhere, I am losing the spaces in the text. I suspect it is one fucntion call doing it.
I am using the following functions:
while (!feof(f)){ //Get all the data in from the file
fscanf(f, "%s", msg);
StrCat(temp, msg);
}//wend

tempmsg= StrScan(temp,';') ; //find the 1st delimiter
StrLCopy(msg, temp, StrLen(temp)-StrLen(tempmsg)); //Get data before 1st delimiter
StrLCopy(temp, tempmsg+1, 1000);// cut off 1st chunk of data, leaving rest to be delt with

tempSTR=StringReplace(tempSTR,";","/n",TReplaceFlags() << rfReplaceAll) ;
Image1->Canvas->TextOutA(150,380,tempSTR) ;

If I didn't have a dead line, I'd just fumble through. I'll keep looking anyway. I'd appreciate any input.
Thanks!
 
You're losing the spaces in your fscanf function. fscanf reads until it comes to whitespace (space,tab,eol) and throws the whitespace away. So if my line is:

"one two three"

Then the first string read with fscanf is "one", the next string is "two", the next is "three". When these are concatenated, you end up with "onetwothree"

Since you're using the C stdio function fscanf, try using fgets instead. It will read all of the data, including spaces. Be aware, however that it will also read the newline character ('\n') at the end of a line.

Also, using fscanf as you did (checking for eof at the start of your loop) won't work correctly for fscanf or fgets if there's a linefeed at the end of your file, you'll get the last string read in concatenated twice. Try modifying your code as follows:

Code:
 while (fgets(msg,LENGTH_OF_msg,f)){ //Get all the data in from the file

  // check for and remove the newline character if you need to here...

    StrCat(temp, msg);
  }//wend
 
That was it! I knew it was going to be something silly, I haven't done C in far too long.

Thanks!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top