The Origin Forum
File Exchange
Try Origin for Free
The Origin Forum
Home | Profile | Register | Active Topics | Members | Search | FAQ | Send File to Tech support
Username:
Password:
Save Password
Forgot your Password? | Admin Options

 All Forums
 Origin Forum for Programming
 Forum for Origin C
 trimming string
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic Lock Topic Edit Topic Delete Topic New Topic Reply to Topic

ovince

Yugoslavia
Posts

Posted - 10/22/2006 :  4:17:36 PM  Show Profile  Edit Topic  Reply with Quote  View user's IP address  Delete Topic
hi,

Any idea why the attempt to trim strings in the loop does not work (the data are read from file)?

void test4()
{

string name = "file.dat";

FILE *stream;
stream = fopen( name, "r" );


string tmps;
for (int m=0; m<7; m++)
{
fscanf( stream,"%s\n", tmps);
string ts = tmps.Left(5);
printf("%s\n", ts);
}
fclose( stream );


}

thanks
oliver

Deanna

China
Posts

Posted - 10/22/2006 :  11:14:34 PM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
Hi Oliver. The problem is that the address of tmps, which is passed to the fscanf() is not correct. The string varibale tmps has not been properly initialized before its address is used.

Actually, scanf() is a C function while string is a C++ stuff. Better not mix them.

I suggest that you change your code like follows:

void test4()
{
#define MAXLEN 30
char cTmp[MAXLEN];

string name = "f:\\temp\\file.dat";

FILE *stream;
stream = fopen( name, "r" );

for (int m=0; m<7; m++)
{
fscanf( stream, "%s", cTmp);
string ts(cTmp, 5);
printf("%s\n", ts);

}

}


Note: please modify MAXLEN as you need.

Deanna
OriginLab Technical Services

Edited by - Deanna on 10/22/2006 11:23:41 PM
Go to Top of Page

ovince

Yugoslavia
Posts

Posted - 10/25/2006 :  1:24:05 PM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply


thank you Deanna
Go to Top of Page
  Previous Topic Topic Next Topic Lock Topic Edit Topic Delete Topic New Topic Reply to Topic
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
The Origin Forum © 2020 Originlab Corporation Go To Top Of Page
Snitz Forums 2000