Do you need to use a flag to open a file?

This is a discussion on Do you need to use a flag to open a file? within the C Programming forums, part of the General Programming Boards category; Can anyone help with the understanding of a couple questions? Just learning C and so we haven't got to pointers. ...

  1. #1
    Unregistered
    Guest

    Do you need to use a flag to open a file?

    Can anyone help with the understanding of a couple questions?
    Just learning C and so we haven't got to pointers.

    int flag = 1; //file closed
    if((file = fopen("somefile.dat", "r"))=NULL)
    printf("somefile will not open.");
    exit(1);
    else flag = 2; //file opened

    1) If this is opened, it returns a pointer, but I don't really understand what that does?
    2) Our book or prof doesn't really talk about the flag except to say to do it like this. I have seen it done a lot without the else condition. Can anyone help explain about the flag and else condition and if it is needed.

    Sorry about no code tags, I tried to use them. Thanks

  2. #2
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    int flag = 1;

    if( (ptr = fpoen("file.dat", "r")) == NULL)
    {
    printf("Error opening file");
    exit(1);
    }
    else
    flag = 2;

    fopen() returns a pointer to the newly opened stream, 'ptr'.
    A flag is just a variable to hold the state of the stream.
    The flag would indicate whether it is open or closed, that's all.

  3. #3
    Registered User Zeeshan's Avatar
    Join Date
    Oct 2001
    Location
    Munich, Germany
    Posts
    224
    Ans1. The pointer points to the memory location which has the handle for the file just opened. It is through the pointer that you would further REFER to the file for information retrieval or information saving or other functions that you wish to perform on the file that you've just fopen-ed...

    Ans2. In the code that you've given, there is no need for a flag. It is because, if the file couldn't be opened the program would just exit. It would only proceed in the case, when the file was successfully opened.

    If you omit the "exit" however. The flag would be used to check the state of the file, whether it had been opened or not....

  4. #4
    Unregistered
    Guest
    Thanks so much for all your help.

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. can some one please tell me the cause of the error ?
    By broli86 in forum C Programming
    Replies: 8
    Last Post: 06-26-2008, 08:36 PM
  3. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  4. Inventory records
    By jsbeckton in forum C Programming
    Replies: 23
    Last Post: 06-28-2007, 04:14 AM
  5. Basic text file encoder
    By Abda92 in forum C Programming
    Replies: 15
    Last Post: 05-22-2007, 01:19 PM

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