|
Something like:
CString headline;
...
CString Token;
int Pos = 0;
while (!(Token = headline.Tokenize(_T(","), Pos)).IsEmpty())
{
}
> The problem with computers is that they do what you tell them to do and not what you want them to do. <
> Life: great graphics, but the gameplay sux. <
|
|
|
|
|
After trying yours, I got:
'Tokenize' : is not a member of 'CString'
Please check and help
|
|
|
|
|
Works for me, maybe it is some version difference or so...hmmm...that CString is the MFC CString, not some custom class, right?
> The problem with computers is that they do what you tell them to do and not what you want them to do. <
> Life: great graphics, but the gameplay sux. <
|
|
|
|
|
Yes, CString is a MFC CString. I am using C++ 6.
|
|
|
|
|
7 here, i guess Tokenize didn't exist in version 6 yet. Well, you can either try using this[^], or hope someone who knows version 6 comes along and helps you.
> The problem with computers is that they do what you tell them to do and not what you want them to do. <
> Life: great graphics, but the gameplay sux. <
|
|
|
|
|
Thanks. I think I need to port my program to a newer version.
Is it be much trouble to use a newer version of compiler for the present program ?
Any comments
Thanks.
|
|
|
|
|
mrby123 wrote: How to parse a CString: 1,1,E1,0.13060,-0.40193,-0.90631
which is a line delimited with commas of a mixture of numvers and characters.
By using AfxExtractSubString() .
Also, since you are using MFC, you can forgo the use of FILE and fscanf() and use CStdioFile instead. Calling GetBuffer() like you are is unnecessary and potentially dangerous.
"Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
|
|
|
|
|
Why not just copy it to a char * and use strtok
a programmer traped in a thugs body
|
|
|
|
|
You might want to suggest that to the original poster, rather than David.
(I suspect he knows strtok)
Iain.
Codeproject MVP for C++, I can't believe it's for my lounge posts...
|
|
|
|
|
Hello guys,
i need you help with a C program that reads names from a .txt file and sort them in alphabetical order.
I have written the code for the reading part and it seems to work, but I don't know how to orgnize the bubble sort algorithm in order to compare strings and swap them.
Could you please give me some hints?
Thanks a lot,
raeiko
Here is my code:
#define SIZE 30
int main()
{
char name[ SIZE ];
FILE *fpPtr;
if ( ( fpPtr = fopen( "sorted_file.txt", "r" ) ) == NULL ) {
printf( "File could not be opened\n" );
}
else {
printf( "%s\n", "Name" );
fscanf( fpPtr, "%s", name );
while( !feof( fpPtr ) ) {
printf( "%s\n", name );
fscanf( fpPtr, "%s", name );
}
fclose( fpPtr );
}
return 0;
}
|
|
|
|
|
Why write your own bubble sort when the standard library has a quicksort built in. That way, you get a fast sort routine that you know works...
99.9999% of the time, it's better to use someone else's library code rather than write your own.
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
|
|
|
|
|
Stuart Dootson wrote: Why write your own bubble sort when the standard library has a quicksort built in.
Perhaps it's an academic exercise.
"Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
|
|
|
|
|
That's what I figured - in which case, helping him isn't helping him
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
|
|
|
|
|
raeiko wrote: ...but I don't know how to orgnize the bubble sort algorithm in order to compare strings and swap them.
You first need to get a working bubble-sort algorithm before moving on.
int nNumbers[] = { 1, 7, 4, 10, 3, 5, 6, 9 };
int nCount = sizeof(nNumbers) / sizeof(nNumbers[0]);
...
BubbleSort(nNumbers, nCount); Work on this, and when you get stuck, we'll be glad to help.
"Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
|
|
|
|
|
I think tree structer is far more better than the bubble sort in actual work, if you are doing a actual work not homework. there is less moving when the tree is used.
|
|
|
|
|
Hello,
I'm writing this program for an academical assignment that is why i need to use the bubble sort algorithm, even though, from what i undersand, is not the most suitable solution.
I have tried to go on with my code but it's still not working and i'm getting more and more confused because i don't have really clear in my mind how to handle pointers...
Here is what i have done but i'm sure it's completely wrong
#include <stdio.h>
#include <string.h>
#define SIZE 30
int main()
{
char name[ SIZE ];
FILE *fpPtr;
if ( ( fpPtr = fopen( "sorted_file.txt", "r" ) ) == NULL ) {
printf( "File could not be opened\n" );
}
else {
printf( "%s\n", "Name" );
fscanf( fpPtr, "%s", name );
while( !feof( fpPtr ) ) {
printf( "%s\n", name );
fscanf( fpPtr, "%s", name );
}
fclose( fpPtr );
}
for ( pass = 1; pass < SIZE; pass++ ){
for ( counter = 0; counter < SIZE - 1; counter ++ ){
if ( *name [ counter ] > *name [ counter + 1 ] ) {
hold = *name[ counter ];
*name[ counter ] = *name[ counter + 1 ];
*name[ counter + 1 ] = hold;
}
}
}
return 0;
}
|
|
|
|
|
Please help:
Here is code I tried, but not working.
*/
char string[150];
char * dummy;
fscanf(f_ptr1,"%s\n",string); reading a line: 1,1,E1,0.13060,-0.40193,-0.90631
nl = int(strtok(string, seps)); // Get the first token
nd = int(strtok(string, seps)); // Get the second token
dummy = strtok(string, seps); // Get the third token
xx = atof(strtok(string, seps)); // Get the fouth token
yy = atof(strtok(string, seps)); // Get the fifth token
zz = atof(strtok(string, seps)); // Get the sixth token
Here nl, nd are integer, xx,yy,zz, are float.
Please point out how to make codes work. Thanks a lots
|
|
|
|
|
Two mistakes:
- You can't get an integer from a string by casting - use atol (that's like atof for integers)
- You only pass the string pointer the first time you call strtok for a string. The other times, pass a null pointer
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
|
|
|
|
|
Could you fix the codes and post a working ones ?
That would be greatly appriaciated.
|
|
|
|
|
It'll help you more to fix it yourself.
Some hints:
- Replace the 'int' in the nl and nd casts by 'atol'
- A null pointer is zero. Replace the 'string' argument in all except the first strtok call. Look at the strtok documentation[^]
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
|
|
|
|
|
You are a good coach.
Thanks tons. It works now. Do you see any improvement from the following code. Please point out to me.
Thanks alots
char string[80]=" ";
char * dummy;
char seps[] = ",";
for (i=0;i<nhole;i++)
{
// fscanf(f_ptr1,"%i,%i,%i,%s,%f,%f,%f\n",&kdum[i],&nlinks[i],&ndih[i],buffer,&cos_xx[i],&cos_yy[i],&cos_zz[i]);
fscanf(f_ptr1,"%s\n",string);
kdum[i] = atol(strtok(string, seps));
nlinks[i] = atol(strtok(NULL, seps));
ndih[i] = atol(strtok(NULL, seps));
dummy = strtok(NULL, seps);
cos_xx[i] = atof(strtok(NULL, seps));
cos_yy[i] = atof(strtok(NULL, seps));
cos_zz[i] = atof(strtok(NULL, seps));
}
//
|
|
|
|
|
Looks OK to me
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
|
|
|
|
|
"atof" returns double. Do you have one returning to float ?
Thanks.
|
|
|
|
|
No. Doesn't really matter - double will get trruncated into a float.
To be honest, you're better off using double, so long as 8 rather than 4 byte isn't an issue. double is the native format of your CPU, which means double operations are often quicker than float operations.
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
|
|
|
|
|
Thanks for clarify to me. I am learner for C++.
Thanks again.
|
|
|
|
|