Hi guys, newbie here, some questions!

This is a discussion on Hi guys, newbie here, some questions! within the C Programming forums, part of the General Programming Boards category; Originally Posted by fuso I think for my purposes, gets() should be fine - the filename will not be longer ...

  1. #16
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by fuso View Post
    I think for my purposes, gets() should be fine - the filename will not be longer than 99 characters.

    Do I define 'filename' as a string?
    And what happens if the user leans on the keyboard producing more than 99 chars?

    Yes, I agree it's the users fault for leaning on the keyboard, leaving something like this
    Code:
    gfffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
    in the filename, but it's still bad practice to use input functions that allow the application to crash with an error message when the user happens to input too much data.

    And why do you believe that a filename should always be less than 99 chars? Admittedly it's unpractical, but MAX_PATH in windows is 260 chars, and in Linux somehting like 1024 chars - so if the OS can support the filename, why should your application deny it from being used in your app?

    "string" is not a concept that really exists in C, as it is part of C++. None of your other code is C++.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  2. #17
    Registered User
    Join Date
    Mar 2008
    Posts
    15
    Ok, I think I'm using fgets properly...

    This program is doing my head in. I'm struggling. It doesn't work. Any freindly help would be greatly appreciated!

    Code:
         FILE *fp;                              
    
         int text[ALPHABET_TOTAL] = {0},          
              text_input,                        
              ASCII_count,                       
              alpha;                             
    
         char filename[20];
          
    /*initializing*/
          
         text_input = 0;   
         ASCII_count = 0;
         alpha = 0;
    
    /*user prompt*/ 
    
         printf("Type a file name, followed by the <enter> key, Ctrl+Z, and then <enter key> again : ");   
    
         fgets(filename,100, stdin);
    
         fp=fopen(&filename[0],"r");
    
       
    /*text analysis*/
    
          while( text_input != EOF )   
    
          {
            alpha = 1;
            text_input = getchar();    
            
    
                    if(text_input >= 'a' && text_input <= 'z')
                    text_input -= 'a';
    
    
          			else if(text_input >= 'A' && text_input <= 'Z')
          			text_input -= 'A';
    
    
           else
           alpha = 0;
    
        
          if(alpha)
           text[text_input]++;
         }
        
    /*display results*/
        
         for (ASCII_count; ASCII_count <= 25; ASCII_count++)
    
         	printf("\nTotal %c or %c: %d", ASCII_count + 'a', ASCII_count + 'A',text[ASCII_count]);
    
    }

  3. #18
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,918
    You're not using it right:

    fgets(filename,100, stdin);
    Should be
    fgets(filename,sizeof(filename), stdin);

    Remember that if you lie about the buffer size to fgets, it's not better than gets.

    fp=fopen(&filename[0],"r");
    Is the same as
    fp=fopen(filename,"r");

    Also from what I can see, you're not actually doing anything with the translated characters. What are you supposed to do with them? Output to screen?
    Last edited by Elysia; 03-13-2008 at 07:12 AM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #19
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Did you even read my reply?

    Well, if you declare a filename[20], then you should not use fgets(filename, 100, stdin), should you? That will allow 80 chars more than what you have space for to be read in.

    I suggested that you use sizeof(filename), and that you use limits.h as an include, then use MAX_PATH to size your filename - that way you are GUARANTEED that all filenames that are allowed on that machine can fit within your fileaname, not just ones that fit in some arbitrary size that you think is big enough, be that 20, 99 or some other number.

    Code:
         fp=fopen(&filename[0],"r");
    is still not fixed [although it technically won't cause any problem].

    I think the point I missed with fgets() is that it leaves a newline at the end of the string, so you need:
    Code:
        len = strlen(filename);
        if (filename[len] == '\n') filename[len-1] = 0;
        else //  ... filename didn't fit in the variable...
    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #20
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    if (filename[len] == '\n') filename[len-1] = 0;
    It's wrong

    len-1 should be in both places...

    But I prefer
    Code:
    char* p = strchr(filename, '\n');
    if(p) *p = '\0';
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  6. #21
    Registered User
    Join Date
    Mar 2008
    Posts
    15
    Are these ammendments a necessity in order for the program to work? Or are they sophisticated tricks?

    I don't think I'm going about the right way of reading characters from the file either; do I need

    Code:
    text_input = getchar(filename);
    or something along those lines? Actually that's definately wrong.

  7. #22
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by vart View Post
    It's wrong

    len-1 should be in both places...

    But I prefer
    Code:
    char* p = strchr(filename, '\n');
    if(p) *p = '\0';
    Good spot - and strchr() and strlen() are about the same performance in the trivial implementation. strlen() can be made more optimal because we KNOW we're searching for a zero, and there are special tricks for doing that on multiword basis. But for this purpose, it's irrelevant.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #23
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,918
    These are not tricks. Not the newline remove. It is required to open your file.
    Oh and you need to use fgetc to read a character from a file.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #24
    Registered User
    Join Date
    Mar 2008
    Posts
    15
    Here is the appended version (I hope!)

    It now freezes after returning a file name...

    Code:
    #include <stdio.h>
    
    #define ALPHABET_TOTAL 26                    
    
    int main()
    
    
    {
    
    /*defining variables*/
    
         FILE *fp;                              
    
         int text[ALPHABET_TOTAL] = {0},          
              text_input,                        
              ASCII_count,                       
              alpha;                             
    
         char filename[20];
    
         char* p = strchr(filename, '\n');
         if(p) *p = '\0';
          
    /*initializing*/
          
         text_input = 0;   
         ASCII_count = 0;
         alpha = 0;
    
    /*user prompt*/ 
    
         printf("Type a file name, followed by the <enter> key : ");   
    
         fgets(filename,sizeof(filename),stdin);
    
         fp=fopen(filename,"r");
    
       
    /*text analysis*/
    
          while( text_input != EOF )   
    
          {
            alpha = 1;
            text_input = fgetc(filename);    
            
    
                    if(text_input >= 'a' && text_input <= 'z')
                    text_input -= 'a';
    
    
          			else if(text_input >= 'A' && text_input <= 'Z')
          			text_input -= 'A';
    
    
           else
           alpha = 0;
    
        
          if(alpha)
           text[text_input]++;
         }
        
    /*display results*/
        
         for (ASCII_count; ASCII_count <= 25; ASCII_count++)
    
         	printf("\nTotal %c or %c: %d", ASCII_count + 'a', ASCII_count + 'A',text[ASCII_count]);
    
    }
    arrrgrhgrgraararararrrrgg!!!

  10. #25
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
         char* p = strchr(filename, '\n');
         if(p) *p = '\0';
    This portion needs to be AFTER the input of the filename.

    You may have to declare char *p up at the top of the function, and then use p =strchr(...) after the fgets(). The purpose of the above snippet is to make sure the newline that fgets() took in is removed from the string itself.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  11. #26
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,918
    Make sure you actually opened the file successfully before trying to read from it; otherwise you are going to get an endless loop.
    (If you can't open the file, you should probably print an error, and optionally, let the user try again.)
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  12. #27
    Registered User
    Join Date
    Mar 2008
    Posts
    15
    Ok so now it's physically opening the file I type in (i.e. it loads notepad), which is intereting - a good thing you say?

    How come it's ignoring the rest of my code after that?

  13. #28
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,918
    You may need to add "getchar()" at the end of the program to prevent the console window from closing. Try that first.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  14. #29
    Cogito Ergo Sum
    Join Date
    Mar 2007
    Location
    Sydney, Australia
    Posts
    463
    Alternatively, for console programs, download cygwin, and compile with gcc, then u don't have a problem of console's closing.

  15. #30
    Registered User
    Join Date
    Mar 2008
    Posts
    15
    Hmm, ok it doesn't notepad anymore, and in fact just freezes the command prompt

Page 2 of 4 FirstFirst 1234 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. questions....so many questions about random numbers....
    By face_master in forum C++ Programming
    Replies: 2
    Last Post: 07-30-2009, 09:47 AM
  2. Pipes, inheritence, newbie questions
    By grasshopa in forum Windows Programming
    Replies: 0
    Last Post: 02-28-2006, 06:17 AM
  3. Newbie IO questions
    By gustavosserra in forum C Programming
    Replies: 5
    Last Post: 08-29-2003, 09:09 AM
  4. More newbie questions
    By sunzoner in forum C++ Programming
    Replies: 3
    Last Post: 06-11-2002, 07:23 AM
  5. questions questions questions.....
    By mfc2themax in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 08-14-2001, 08:22 AM

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