Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

C- programming: Reading filename from keyboard

  1. Mar 5, 2008 #1

    malawi_glenn

    User Avatar
    Science Advisor
    Homework Helper

    [SOLVED] C- programming: Reading filename from keyboard

    Hi!

    I am trying to write a small program for open a file by entering filename with keyboard, but it does not work. Why?


    int main(){


    FILE *fp;


    printf("Give filename ");

    char str[100], *s;

    s = fgets(str, 100 , stdin);

    printf("\n");

    fp = fopen(str, "r");

    -----------

    No file opens.. :(
     
  2. jcsd
  3. Mar 5, 2008 #2

    jim mcnamara

    User Avatar
    Science Advisor
    Gold Member

    try:
    Code (Text):

    #include <stdio.h>
    #include <string.h>

    int main()
    {
       FILE *fp=NULL;;
       char str[100]={0x0};
       char *s=NULL;;

       printf("Give filename ");
       s=fgets(str, 100 , stdin);
       s=strchr(str,'\n');
       if(s!=NULL) *s=0x0;
       fp = fopen(str, "r");
       printf("%s open = ", str);
       if(fp!=NULL)
         printf("success\n");
       else
         printf("failure");
       fclose(fp);
       return 0;
    }


     
     
  4. Mar 5, 2008 #3

    malawi_glenn

    User Avatar
    Science Advisor
    Homework Helper

    I tried:

    printf("Give filename ");

    gets(filename);


    fp = fopen(filename, "r");

    -------

    and it worked.
     
  5. Mar 5, 2008 #4

    jim mcnamara

    User Avatar
    Science Advisor
    Gold Member

    fgets returns the trailing \n which is not part of the filename.
     
  6. Mar 5, 2008 #5

    malawi_glenn

    User Avatar
    Science Advisor
    Homework Helper

    ok? but it worked
     
  7. Mar 5, 2008 #6

    jim mcnamara

    User Avatar
    Science Advisor
    Gold Member

    Oh - in general, gets is a BAD idea - it allows string overrun exploits. For your own code it is fine doing desktop programming.
     
  8. Mar 5, 2008 #7

    jim mcnamara

    User Avatar
    Science Advisor
    Gold Member

    gets and fgets are not the same thing.
     
  9. Mar 5, 2008 #8

    malawi_glenn

    User Avatar
    Science Advisor
    Homework Helper

    what is desktop programming?
     
  10. Mar 5, 2008 #9

    malawi_glenn

    User Avatar
    Science Advisor
    Homework Helper

    that fgets and gets is not the same thing i know :-)
     
  11. Mar 5, 2008 #10

    jim mcnamara

    User Avatar
    Science Advisor
    Gold Member

    desktop programming - writing code for personal use in your profession. Also called 'skunkworks' coding. So if you play with quantum gravity, you might write some quick and dirty Mathematica routines to do a quick analysis. You probably would not consider it suitable for a publication.
     
  12. Mar 5, 2008 #11

    malawi_glenn

    User Avatar
    Science Advisor
    Homework Helper

    yeah now I see. This is just for personal use, I want to learn C. Iknow Java very good, and want to do C++, but thinks is funnier to go to basics. Also we have a C-programming project in school, and I just thinks C is very cool. After this I will try Fortran.

    thanx for your help!
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?



Similar Discussions: C- programming: Reading filename from keyboard
Loading...