Using sscanf() to extract strings

Discuss the development of new homebrew software, tools and libraries.

Moderators: cheriff, TyRaNiD

Post Reply
reefbarman
Posts: 87
Joined: Mon Jan 08, 2007 12:16 pm
Location: Australia

Using sscanf() to extract strings

Post by reefbarman »

hi everyone,

what i want to do is be able to remove the last character from my c string, now i thought i could do this sscanf(), but its driving me nuts it wasn't working at the start then i added an extra variable called grab and it worked, but it was removing one too many characters, so i changed the variable and now its not working again
here
is my code

Code: Select all

temp = line[linesUsed].data;
int grab = totalChars - 1;
sscanf(temp, "%[grab]s", line[linesUsed].data);
chars--;
totalChars--;
if &#40;chars < 0&#41;&#123;linesUsed--; chars = MAX_CHARS;&#125;	
basically what im trying to do is make a backspace function for my program thanks for your help
User avatar
dot_blank
Posts: 498
Joined: Wed Sep 28, 2005 8:47 am
Location: Brasil

Post by dot_blank »

use the std lib string handling for such things

here is a example file extensions

Code: Select all

#define FILE "some_file.ext"
#define FILE_BASE  "%s/%s"
#define PSP_BASE  "ms0&#58;/PSP/GAME/SOME_GAME/"

char temp&#91;1024&#93;;
char *file;

snprintf&#40;temp, 1024, FILE_BASE, PSP_BASE, FILE&#41;;
file = strrchr&#40;temp, '.'&#41;;

// finish this simple problem to familiarize yourself 
// with using std lib string handling
Last edited by dot_blank on Fri Jan 12, 2007 12:57 pm, edited 1 time in total.
10011011 00101010 11010111 10001001 10111010
reefbarman
Posts: 87
Joined: Mon Jan 08, 2007 12:16 pm
Location: Australia

Post by reefbarman »

mmmm i still dont understand how this will help, isnt what your example is doing is concatenating strings??

sorry im so used to using languages like c++, java and the like that im really having trouble using c style strings as i really never used straight c before so im not really familar with many c functions
kuroneko
Posts: 24
Joined: Thu Dec 08, 2005 11:32 am
Location: Chigasaki, Japan

Re: Using sscanf() to extract strings

Post by kuroneko »

Would this help? I assume line[linesUsed].data is the actual string and temp a char* (ASCII string, no multibyte characters).

Code: Select all

temp = line&#91;linesUsed&#93;.data;
if &#40;temp&#91;0&#93;&#41; /* string not empty */
&#123;
    temp&#91;strlen&#40;temp&#41;-1&#93; = '\0';
&#125;
reefbarman
Posts: 87
Joined: Mon Jan 08, 2007 12:16 pm
Location: Australia

Post by reefbarman »

that worked b e a utifully thanks
Post Reply