String manipulation and scanf vs fgets

This is a discussion on String manipulation and scanf vs fgets within the C Programming forums, part of the General Programming Boards category; First time on the board and I would like to say I've been enjoying meself reading many of the posts ...

  1. #1
    Nit
    Nit is offline
    Registered User
    Join Date
    Mar 2002
    Posts
    42

    Question String manipulation and scanf vs fgets

    First time on the board and I would like to say I've been enjoying meself reading many of the posts that I have found here. I would like to start off by saying that I did use the search feature before this question was asked


    A little background on my project: I have to implement a double-linked list (which is done). After the double-linked list is implemented I have to create a structure to keep track of each double-linked list I create by name (partially done, just coded it to be a single-linked list pointing to each double-linked list).

    Anways, the user is allowed to type in commands to interact with each double-linked list that is created, by name.

    ** commands **
    d x s - delete the string x in list s.
    pb s - print the contents of s in reverse alphabetical order.
    ** commands **

    I read all about scanf and how inefficient it is and started using fgets, which does indeed get all of my input from each line without me having to flush the stdin or do any other 'magic'.

    How would I get d into a variable named, userCommand? x into another one, and s...?

    My problem starts with having to separate each string from the line of input. I found a post that referred me to: strpbrk(.., ..) (http://www.rt.com/man/strpbrk.3.html). I don't think strpbrk is exactly what I need. Since I'm dealing with a dynamic number of characters in my input fields I just can't do it by array indexes into the string. Any help is appreciated.

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    There's a function called sscanf that allows you to read formatted input from a string, usually a string that you read using fgets. This way you can break the line up into the parts you need.

    -Prelude
    My best code is written with the delete key.

  3. #3
    Nit
    Nit is offline
    Registered User
    Join Date
    Mar 2002
    Posts
    42
    Thank you Prelude. Seems to me that I passed right over that function. It was exactly what I needed.

    I'm going to try a couple ideas I have on the different formats that my program may receive for input before I ask my next question. Thanks again.

    *was worried that I'd get flamed up and down and all about my body for being this my first question*

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >*was worried that I'd get flamed up and down and all about my body for being this my first question*
    That only happens if your first question is how to clear the screen

    -Prelude
    My best code is written with the delete key.

  5. #5
    B26354 Deckard's Avatar
    Join Date
    Jan 2002
    Posts
    633
    Originally posted by Prelude
    That only happens if your first question is how to clear the screen
    Or has a code example declaring main as returning void ;)
    Jason Deckard

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >Or has a code example declaring main as returning void
    *blush* Yea, that too

    -Prelude
    My best code is written with the delete key.

  7. #7
    Registered User
    Join Date
    Mar 2002
    Posts
    12
    Hi,
    I need to use the sscanf function in a programming assignment. I didn't even know about it until I came across this message board, but its exactly what I need since we are not allowed to use any string.h functions for this assignment. Its format is: int sscanf(const char*str, const char*format, ...).
    My question is: what is the char* format? Is that the characters that you use to deliminate the string, such as a space or comma?

    thanks in advance

  8. #8
    Unregistered
    Guest
    Code:
    #include "conio.h"
    #declare goto  goto
    
    void main(void)
    {
         A;
        clrscr();
        goto A;
    
        return void;
    }
    Why won't this work? And please hurry because my teacher want this homework before noon

  9. #9
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >My question is: what is the char* format?
    Here's a quick example of breaking a string into variables with sscanf.
    Code:
    while ( fgets ( buffer, (int)sizeof buffer, INPUT ) != NULL ) {
      if ( sscanf ( buffer, "%d,%d", &record.id, &record.idle ) == 2 ) {
        printRecord ( &record );
      else {
        fprintf ( stderr, "ERROR %d: Invalid data input\n", errno );
        cleanUp( &record, INPUT );
        return EXIT_FAILURE;
      }
    }
    >Why won't this work?
    #declare isn't a valid preprocessor directive, there's no point in redefining a keyword with the same name. Labels end with a colon, not a semicolon, main returns an int and conio should be surrounded with <>, not " " since it's a library that comes with your compiler, not a header that you defined yourself.
    Code:
    #include <conio.h>
    #define goto goto /* Serves no purpose */
    
    int main(void)
    {
    A:
      clrscr();
      goto A;
      return 0;
    }
    -Prelude
    My best code is written with the delete key.

  10. #10
    Nit
    Nit is offline
    Registered User
    Join Date
    Mar 2002
    Posts
    42
    Or has a code example declaring main as returning void -

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. String break to words and numbers
    By westm2000 in forum C Programming
    Replies: 3
    Last Post: 03-16-2002, 03:53 PM
  2. simple input and string manipulation question
    By Stig in forum C Programming
    Replies: 1
    Last Post: 12-15-2001, 01:33 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21