|
i have a string
login#user#1 username password
i want to trim login# and extract user#1 username password
i am unable to sole it using strtok
please help
|
|
|
|
|
Use
std::string::find_first_of() To find the position of
#. Then use
std::string::substring() to extract the string you required.
Code might look like this:
std::string test = login#user#1 username password;
size_t pos = test.find_first_of("#");
std::string requiredstr = test.substr(pos+1);
|
|
|
|
|
is there any way to do it using strchr
|
|
|
|
|
|
hey i am programming in C...can u give a sample code of how to do it using strchr(), strtok or any other function in C
|
|
|
|
|
|
yeah i know this but this does not work for the string
Login#User#1 username password if want to trim Login# and extract User#1 username password
|
|
|
|
|
You should be able to figure out what changes to the program needs to be made for it to work with your string.
|
|
|
|
|
I've provided you a link with source code. Use the formula
in the printf statement inside the while loop.
char str[] = "Your string goes here";
char * pch;
printf ("Looking for the '#' character in \"%s\"...\n",str);
pch=strchr(str,'s');
while (pch!=NULL)
{
printf ("found at %d\n",pch-str+1);
pch=strchr(pch+1,'s');
}
pch is the required string.
|
|
|
|
|
Use strchr() to find the first # character, and then you can use strtok() to split into the three tokens delimited by spaces.
Unrequited desire is character building. OriginalGriff
I'm sitting here giving you a standing ovation - Len Goodman
|
|
|
|
|
After so many questions regarding strings, parsing and related stuff, don't you think it's about time to read some actual books or articles to learn all this? Or do you intend to keep asking for the rest of your life?
Seriously, the time you keep spending on low level programming questions like this would have been better spent by reading up and learning something by yourself. You are doing yourself and everyone else a disservice by asking questions without ever even trying to look something up and really understand the things you are dealing with.
Regarding this particular question, simply reading up on strtok would solve it, easily.
|
|
|
|
|
Have you tried something like:
char *pszSource = "login#user#1 username password";
char *pszPos = strstr(pszSource, "user#1");
int nLen = pszSource - pszPos;
"One man's wage rise is another man's price increase." - Harold Wilson
"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
"Show me a community that obeys the Ten Commandments and I'll show you a less crowded prison system." - Anonymous
|
|
|
|
|
I get these compile errors "warning C4129: 'N' character escape sequence.
I think I just did something dumb, because I have other lines that are fine in the same code block. I'm not really sure what to make of it.
L" /SAPWD=\"securePassword1\";"
but this line errors on \"N
L" /AGTSVCACCOUNT=\"NT_AUTHORITY\NETWORK SERVICE\""
WCHAR *szParameters =
L" /Q /IACCEPTSQLSERVERLICENSETERMS"
L" /ACTION=install"
L" /ERRORREPORTING=1"
L" /INDICATEPROGRESS"
L" /FEATURES=SQL,FULLTEXT,AS,RS,IS,TOOLS"
L" /INSTANCENAME=SQLExpress"
L" /SECURITYMODE=SQL"
L" /SAPWD=\"securePassword1\";"
L" /NPENABLED=1"
L" /TCPENABLED=1"
L" /BROWSERSVCSTARTUPTYPE=AUTOMATIC"
L" /SQLSVCSTARTUPTYPE=AUTOMATIC"
L" /AGTSVCSTARTUPTYPE=AUTOMATIC"
L" /RSSVCStartupType=Automatic"
L" /AGTSVCACCOUNT=\"NT_AUTHORITY\NETWORK SERVICE\""
L" /SQLSYSADMINACCOUNTS=\"BUILTIN\ADMINISTRATORS\""
L" /SQLSVCACCOUNT=\"NT AUTHORITY\NETWORK SERVICE\""
L" /FTSVCACCOUNT=\"NT AUTHORITY\NETWORK SERVICE\""
L" /RSSVCACCOUNT=\"NT AUTHORITY\NETWORK SERVICE\""
L" /RSSVCPASSWORD=\"securePassword1\" "
L" /ISSVCACCOUNT=\"NT AUTHORITY\NETWORK SERVICE\""
L" /ISSVCPASSWORD=\"securePassword1\"";
return szParameters;
|
|
|
|
|
|
You have to escape the backslashes in the middle of the string, like this:
L" /AGTSVCACCOUNT=\"NT_AUTHORITY\\NETWORK SERVICE\""
|
|
|
|
|
I get it now, I escaped out the doublequote, but I didn't bother to look in the center. I was too busy looking at the doublequote.
It was a dumb mistake, Thanks for your help.
|
|
|
|
|
Dear All,
The code below is C Programming that i wrote and is working but I am having problem in reading any records I am saving into text File called "MY FILE".Also anytime to ADD NEW RECORD in my switch case selection,the initial record that was in the is been deleted automatically.Did any one know what is wrong.I can figure it out,i have been tried debugging but all to no avail.I will appreciate your ideas.Thanks
***********************************************************'''''****
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define SIZE 10
#define Income_type 8
#define Expense_type 7
typedef struct{
int day;
int month;
int year;
}Date_of_transaction;
typedef struct{
int type_transaction;
char desc_transaction[80];
float amount_of_money;
Date_of_transaction date;
}Trecord;
typedef struct{
Trecord r;
}Tarray;
int count(int *count);
void add_new_record(Trecord records,Tarray t[],int *counter,int *count);
void read_data_from_disc(Trecord *records);
void save_curiculum_to_disc(Tarray records[]);
void add_money(Trecord *records);
void spend_money(Trecord *records);
void print_a_report(Tarray *records,int *counter);
void delete_a_record(Trecord *record);
void initialize(Trecord records);
void initializeRecord(Tarray records[]);
int main(int argc, char** argv) {
int quit = 0;
int choice = 0;
static int counter = 0;
int count = -1;
Trecord records;
Tarray recordData[SIZE];
initializeRecord(recordData);
while(!quit) {
printf("\nChoose an option:\n"
"1. Add New Record\n"
"2. Read Data from File\n"
"3. Save Data to File\n"
"4. Print Report\n"
"5. Quit\n" );
scanf("%d", &choice);
switch(choice){
case 1:
count++;
add_new_record(records,recordData,&counter,&count);
printf("%d",count);
break;
case 2:
read_data_from_disc(&records);
break;
case 3:
save_curiculum_to_disc(recordData);
break;
case 4:
count++;
print_a_report(recordData,&count);
break;
case 5:
quit = 1;
break;
default:
printf("Invalid Selection\n");
}
}
return (EXIT_SUCCESS);
}
void initializeRecord(Tarray records[]){
int i;
for(i=0;i
modified 26-Nov-11 12:57pm.
|
|
|
|
|
Please wrap your code using <pre> and </pre> tags... this is painful to look at.
|
|
|
|
|
idbee2k3 wrote: ...is working but I am having problem...
That appears to be contradictory. It's either working or it isn't.
idbee2k3 wrote: Did any one know what is wrong.
Hard to tell since you've not shown any code that reads or writes. The only thing you've shown is menu code which is completely irrelevant to the problem.
Use the debugger to narrow the problem down to just a handful of lines, then come back and post those.
"One man's wage rise is another man's price increase." - Harold Wilson
"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
"Show me a community that obeys the Ten Commandments and I'll show you a less crowded prison system." - Anonymous
|
|
|
|
|
First off, I'm not sure if I'm quite in the right forum or not, so just let me know if I should post elsehwere. With that said...
I'm brand new to C++ and am trying to wrap my brain around something. I know that I can use a MAIN function in a .CPP file and use it to grab command line arguments. I'm trying to add this feature to an existing clump of code we've got at work (I'm trying to expand my skills, and it's a small utility we techies use only occasionally). Anyway, I can't figure out how to use a MAIN function if there isn't already one (I can make it work in a brand new test project just fine). My current attempt to tweak the code sort of looks like this:
#include "stdafx.h"
#include ...a few other files.....
class CApp : public CWinAppEx
{
public:
CApp();
public:
virtual BOOL InitInstance();
};
extern CApp theApp;
int main(int argc, char *argv[])
{
MessageBox(NULL, L"This should display first, but doesn't currently show up at all.", NULL, NULL);
}
CApp::CApp()
{
MessageBox(NULL, L"This should display second, but currently shows up first.", NULL, NULL);
}
CApp theApp;
BOOL CApp::InitInstance()
{
MessageBox(NULL, L"This should display third, but currently shows up second.", NULL, NULL);
...does other stuff here...
}
So what do I need to do to use MAIN correctly to grab the command line arguments?
Thanks!
|
|
|
|
|
|
You have code for a Windows application there. And it also looks like it is using MFC. You won't find a main(), nor will you need to. Odd, though is that you have 'theApp' declared twice. That's definitely not a good start.
There are methods of the CWinAppEx class that can be called in InitInstance to assist with command line handling.
Chris Meech
I am Canadian. [heard in a local bar]
In theory there is no difference between theory and practice. In practice there is. [Yogi Berra]
posting about Crystal Reports here is like discussing gay marriage on a catholic church’s website.[Nishant Sivakumar]
|
|
|
|
|
As others have said, you either have main() or you have your WinApp (theApp ) but not both. And definitely not two instantiations of theApp .
The command line arguments can be accessed during your InitInstance() function by looking at the m_lpCmdLine data member of your class.
There are a number of classes / functions that can help you parse it in the argc/argv style you're accustomed to, links are in earlier answers to this question.
|
|
|
|
|
|
in your CWinApp:InitInstance, you should be able to access the __argv and __argc variables.
|
|
|
|