Thread: How to prompt user to enter name of file to be opened??

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    32

    Question How to prompt user to enter name of file to be opened??

    I've tried using the following codes:

    Code:
    # include <stdio.h>
    
    main()
    {
    char FileName;
    
    scanf("%s", &FileName);
    
    FILE *filePtr;
    filePtr = fopen("d:\\FileName", "r");
    if (filePtr == NULL)
    printf("File Cannot Be Opened!(1)");
    
    
    
    }<stdio.h>
    But does not work......So how do you people actually do it....??
    _____

  2. #2
    Registered User
    Join Date
    Nov 2004
    Location
    USA
    Posts
    516
    first of all, you need to make changes..
    1) main() returns an int. it should be int main() and not just main()
    2) very poor formatting of the code..there is no alignment whatsoever..so if you write like this, its going to be very difficult for people to read it and i doubt if anyone will even read what you have posted if your code is of considerable size and without proper alignment..
    3) char filename.. here filename is a char type variable...you cannot store strings inside one variable...you need a char array
    what you need is something of this sort
    Code:
         char filename[20];
         scanf("%s",filename);
         FILE *fileptr;
         fileptr=fopen(filename,"r");
    Code:
    >+++++++++[<++++++++>-]<.>+++++++[<++++>-]<+.+++++++..+++.[-]>++++++++[<++++>-] <.>+++++++++++[<++++++++>-]<-.--------.+++.------.--------.[-]>++++++++[<++++>- ]<+.[-]++++++++++.

  3. #3
    Registered User
    Join Date
    Mar 2004
    Posts
    494
    Why do you have another <stdio.h> at the end of your program? You should know by now header files go at the top after #include.
    Last edited by InvariantLoop; 03-15-2005 at 08:30 AM. Reason: typo
    When no one helps you out. Call google();

  4. #4
    Registered User coolshyam's Avatar
    Join Date
    Mar 2005
    Posts
    26

    Cool Arguements

    when you take the name of the file from the user,it is better to use arguements.
    for example,
    Code:
    int main(int argc, char *argv[])
    {
         FILE *fp;
         fp=fopen(argv[1],"w+");
         ..............
         ..............
    }
    try it

    any questions any type in programming

    a ready made answer
    -----------------------------------------------------------------------------------
    FORTUNE FAVOURS THE BOLD!
    -----------------------------------------------------------------------------------

  5. #5
    Registered User
    Join Date
    Mar 2004
    Posts
    494
    Code:
    w+
    That will create a text file for reading or writing. The OP only wants to read an existing file so he uses
    Code:
    r
    to open the file and read it.
    When no one helps you out. Call google();

  6. #6
    Registered User coolshyam's Avatar
    Join Date
    Mar 2005
    Posts
    26

    Cool

    Quote Originally Posted by InvariantLoop
    Code:
    w+
    That will create a text file for reading or writing. The OP only wants to read an existing file so he uses
    Code:
    r
    to open the file and read it.
    sorry didnt notice what the user wanted. actually i forgot.so, i used w+ mode.
    Code:
    fp=fopen(argv[1],"r");

    any questions any type in programming

    a ready made answer
    -----------------------------------------------------------------------------------
    FORTUNE FAVOURS THE BOLD!
    -----------------------------------------------------------------------------------

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by InvariantLoop
    Why do you have another <stdio.h> at the end of your program? You should know by now header files go at the top after #include.
    Actually that's prefectly legal. For that matter, the standard library header assert.h can be different every time you include it, and may be included multiple times to get different behavior when it's used. Let me quote:
    The Standard C Library, P. J. Plauger
    Page 19, beginning on pharagraph 4.

    This header has an additional peculiarity. As I mentioned in the previous
    chapter, all other headers are idempotent. Including any of them two or
    more times has the same effect as including teh header just once. In the case
    of <assert.h>, however, its behavior can vary each time you include it. The
    header alters the definition of assert to agree with the current definition
    status of NDEBUG.

    The net effect is that you can control assertions in different ways
    throughout a source file. Performance may suffer dramatically, for exam-
    ple, when assertions occur inside frequently executed loops. Or an earlier
    assertion may terminate execution before you get to the revealing parts. In
    either case, you may need to turn assertions on and off at various places
    throughout a source file.

    So to turn assertions on, you write:
    Code:
    #undef NDEBUG
    #include <assert.h>
    And to turn assertions off, you write:
    Code:
    #define NDEBUG
    #include <assert.h>
    Note that you can safely define the macro NDEBUG even if it is already
    defined. It is a benign redefinition, as I described on page 12. Benign
    redefinition was added to Standard C for just this purpose. It eliminates
    the need to protect multiple definitions of the same macro with macro
    guards and conditional directives.
    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    Registered User
    Join Date
    Apr 2004
    Posts
    173
    Also you should use fclose() on the file whenever you have finished with it.

  9. #9
    Registered User
    Join Date
    Feb 2005
    Posts
    32
    Code:
     char filename[20];
         scanf("%s",filename);
         FILE *fileptr;
         fileptr=fopen(filename,"r");
    Cannot work!!
    Code:
    int main()
    {
    
    char filename[20];
         scanf("%s",filename);
         FILE *fileptr;
         fileptr=fopen(filename,"r");
    }
    End up with 2 errors & 1 warning...

    _____

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    That's fabulous. How about actually posting your errors next time? Come on now, you've got 30 posts behind you, you're telling us you still don't know the forum guidelines / etiquette?


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

  11. #11
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by AssistMe
    So how do you people actually do it....??
    FAQ > How do I... (Level 1) > Get a line of text from the user/keyboard (C)
    Quote Originally Posted by AssistMe
    Code:
     char filename[20]; /* declaration */
         scanf("%s",filename); /* statement */
         FILE *fileptr; /* declaration */
         fileptr=fopen(filename,"r"); /* statement */
    End up with 2 errors & 1 warning
    This is okay for C99 or C++, but with C90 you should declare your variables before any statements.
    Code:
     char filename[20]; /* declaration */
         FILE *fileptr; /* declaration */
         scanf("%s",filename); /* statement */
         fileptr=fopen(filename,"r"); /* statement */
    But don't use scanf, read the FAQ.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. opening empty file causes access violation
    By trevordunstan in forum C Programming
    Replies: 10
    Last Post: 10-21-2008, 11:19 PM
  2. Totally confused on assigment using linked lists
    By Uchihanokonoha in forum C++ Programming
    Replies: 8
    Last Post: 01-05-2008, 04:49 PM
  3. SSH Hacker Activity!! AAHHH!!
    By Kleid-0 in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 03-06-2005, 03:53 PM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM