Thread: strcat and Database Building Issues

  1. #1
    Registered User
    Join Date
    Apr 2015
    Posts
    8

    strcat and Database Building Issues

    Hey everyone!
    I've been a long time lurker of this forum as it has helped me through my first programming class and helped me learn C well! This is quite a valuable source of information, and I'll be pretty active on this forum through learning C# and C++ in the fall.

    Just had some questions. I did my final assignment for the semester in my Intro to Programming in C class, and it involves building a database for the user to interact with. I chose to do a database on US naval vessels. I got a little lazy with my scanfs and strcpys. I just decided to separate spaces with underscores to save myself some time, because I am still relatively unfamiliar with strcat and had some issues getting it to look the way I want it to.

    Code:
    strcpy(list[1].shipName, "USS_Cole_DDG67");    
    strcpy(list[1].className, "Arleigh_Burke_Class");
    strcpy(list[1].shipType, "Destroyer");
    This is what some of my hard-coded database entries look like. However, in my need to make everything look nice, I'd like to enable it so that when the user inputs it, they don't need to use underscores and it will save the entire string. For example, I'd like the user to be able to input, say "USS New Hampshire SSN778" and have it save the entire string under the ship name. Is there any way I could do this? Would I need to use some cascading strcat functions? Any help is seriously appreciated!

    Intro_to_See

  2. #2
    Registered User
    Join Date
    Mar 2012
    Location
    the c - side
    Posts
    373
    Seems like you are really talking about the limitation scanf() has in reading a string which includes spaces, in that it will just read the first word, and then stop when it detects the following whitespace.

    fgets() is a good choice if you want to read a complete line of input.

    It also generally stores the newline character though, so you might want to read up on the faq here.
    Last edited by gemera; 04-24-2015 at 09:34 PM.

  3. #3
    Registered User
    Join Date
    Apr 2015
    Posts
    8
    gemera,
    Thank you so much! This did it for me. Even though it's in standard library, my professor chose not to teach me about fgets. Now I've got something in my back pocket for my final exam. I appreciate your help!

    Intro_to_See

  4. #4
    Registered User
    Join Date
    Mar 2012
    Location
    the c - side
    Posts
    373
    If your prof complains about using functions he hasn't covered, you can change the default behaviour of scanf() using something like:

    Code:
    char buff[100];
        
    scanf("%99[^\n]%*c",buff);
    But fgets() is the function of choice for line input.

  5. #5
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,616
    Quote Originally Posted by gemera View Post

    Code:
    char buff[100];
        
    scanf("%99[^\n]%*c",buff);
    I hate this format string. It consumes the input, but doesn't always indicate success:

    %99[
    This indicates that you will match whatever is in the brackets (up to 99 bytes)
    ^
    Then you invert that logic and say match everything except what's in the brackets
    \n]
    %*c
    Then you discard the next byte without assigning it.

    So you should always get 1 if the scanf function reads something. But that doesn't happen, because %[ has to match something in the input to count. If you have a blank line in your input though, this happens:
    Code:
    #include <stdio.h>
    
    int main(void)
    {
        char dummy[100] = "";
        const char* blankln = "\n";
        int value = sscanf(blankln, "%99[^\n]%*c", dummy);
        printf("value: %d\n", value);
        return 0;
    }
    
    #if 0
    output:
    value: 0
    
    http://codepad.org/2z9qySfp
    #endif
    It's so broken.

    So no, scanf does not imitate fgets(). It would be better to roll your own function using getchar() if you have to. In fact, it can be a nice approach - another forum member used it to implement the POSIX function getline() here.
    Last edited by whiteflags; 04-25-2015 at 04:42 PM.

  6. #6
    Registered User
    Join Date
    Mar 2012
    Location
    the c - side
    Posts
    373
    Well I only mentioned it in case the OP went away with the idea that you definitely can't read multi word strings using scanf() and then the prof gave him an exercise expecting him to do just that. So I agree that fgets() is generally the safer and more robust choice. Which is why I recommended it first.

    Having said that, and not that I would or do use scanf() for reading text lines myself,(and in fact don't think I ever have) - I don't quite see why you think its usage needs to be THAT broken in this particular instance.

    I think it would depend on the context.

    e.g. in a database in which each string entry is definitely not empty and you are checking for a return value of 1 to make sure you have successfully read the string input for that entry, you would want to retry input entry on getting a return value of zero anyway. So the only issue would appear to be an inability to discriminate between input failure due to an empty line and failure due to some other reason.

    In that context it's usage would certainly seem safe, just not entirely clean.

  7. #7
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,616
    Having said that, and not that I would or do use scanf() for reading text lines myself,(and in fact don't think I ever have) - I don't quite see why you think its usage needs to be THAT broken in this particular instance.
    It's not that it needs to be broken, it is. Once you come across a blank line, scanf just stops working.

  8. #8
    Registered User
    Join Date
    Mar 2012
    Location
    the c - side
    Posts
    373
    Gotcha!

    It is completely useless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. My strcat
    By CStrong in forum C Programming
    Replies: 6
    Last Post: 06-13-2012, 07:10 AM
  2. building a new pc, post issues
    By Shadow in forum Tech Board
    Replies: 14
    Last Post: 10-06-2008, 03:31 PM
  3. strcat issues
    By xwielder in forum C Programming
    Replies: 2
    Last Post: 04-29-2008, 09:34 PM
  4. Database to Database Transfer?
    By draggy in forum C++ Programming
    Replies: 4
    Last Post: 01-17-2007, 10:50 AM
  5. Replies: 1
    Last Post: 10-09-2001, 10:20 PM