Thread: Hi, I'm new to C small question!

  1. #1
    Registered User
    Join Date
    Aug 2003
    Posts
    6

    Hi, I'm new to C small question!

    Here's the code:
    Code:
    int main()
    {
        FILE *in;
        int key;
    
        char filename[0];
        printf("Enter the filename:");
        scanf ("%s", &filename);
    
    
        if ((in = fopen(filename, "r")) == NULL)
        {
            puts("Unable to open the file");
            return 0;
    There is more, but I've got no need to post the rest of it. It does what I want it to, it reads data from a file you specify, and then prints it on the screen. What I'd like to be able to do, is make it only accept a certain length string, since that would teach me more about strings and all, because at this point they are causing me problems!

    For instance, in the above code, before I put in "char filename[8];", thinking that if I did that, when Scanf read it, if the user put in more than 8 char's, it wouldn't accept it, you know? I'm really just curious as to how I could handle something like that, if anyone knows what I mean, and why do you have to have a value after "char" in the brackets? What if I don't know just how big I want the maximum to be? Like, "char[10]" or whatever, what if I don't know how big I want that string to be? When I define it, is there anyway to tell the compiler that I don't care what size it ends up being?

    I hope I haven't made this too confusing, but I think I'm confused, lol....

    thanks in advance guys

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Use fgets and specify the amount you want.

    Now, this doesn't actually limit the amount they can type, it will however, only use the amount you specify.

    Unless you unbuffer the keyboard (or whatever method they're entering), which is OS specific, there is no way to limit it to N number of keystrokes.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Aug 2003
    Posts
    6

    Thanks!

    Yeah thanks, I'm sure that will help. I'm not really needing it to actually stop them from typing more, just say I ask them for 4 char's or whatever, and they enter 5. I'd like for it to say something like "Please reenter that value, it is too high" or what not, ya know?

  4. #4
    Registered User
    Join Date
    Jul 2003
    Posts
    61
    Just use fgets, like Quzah suggested. It will stop accepting the input as soon it encounters '\n' (enter) character.

    HTH,
    $ENV: FreeBSD, gcc, emacs

  5. #5
    Registered User
    Join Date
    Aug 2003
    Posts
    32
    [Off-Topic Post]
    Why does
    Code:
    scanf ("%s", &filename);
    work?

    Shouldn't it be
    Code:
    scanf ("%s", filename);
    As filename is implicitly a fixed pointer to the memory address of the first character (filename == &(filename[0]))

    [/Off-Topic Post]
    Beware the fury of a patient man.

  6. #6
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164

    Re: Hi, I'm new to C small question!

    Originally posted by Phreedom
    Here's the code:
    Code:
    int main()
    {
        FILE *in;
        int key;
    
        char filename[0];
        printf("Enter the filename:");
        scanf ("%s", &filename);
    
    
        if ((in = fopen(filename, "r")) == NULL)
        {
            puts("Unable to open the file");
            return 0;
    There is more, but I've got no need to post the rest of it. It does what I want it to, it reads data from a file you specify, and then prints it on the screen.
    Wow, it should explode!

    First--
    char filename[0];
    creates a character buffer of 0 bytes, in which you attampt to load whatever is typed into the scanf. You have to define an appropriate size. Use quzah's idea of fgets().

    Second--
    scanf ("%s", &filename);
    As Zainny mentioned, filename is a pointer to the array of charactersa so the & is not needed.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  7. #7
    Registered User
    Join Date
    Aug 2003
    Posts
    6

    You guys rock!

    Hey thanks a lot for the replies! I'm learning more everyday, thanks again!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  2. Hi all, I have a small question...
    By Pandora in forum Windows Programming
    Replies: 3
    Last Post: 03-16-2003, 06:21 AM
  3. opengl DC question
    By SAMSAM in forum Game Programming
    Replies: 6
    Last Post: 02-26-2003, 09:22 PM
  4. A small Question
    By CodeJerk in forum C++ Programming
    Replies: 2
    Last Post: 11-20-2002, 09:08 AM
  5. Very simple question, problem in my Code.
    By Vber in forum C Programming
    Replies: 7
    Last Post: 11-16-2002, 03:57 PM