Thread: problem with file function

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    60

    problem with file function

    Hey guys I have to functions that I am opening files with and I seem to be doing it wrong.
    The first function contains a null terminated string that contains the name of the file.

    The second function receives a pointer to the file. I just put the file name because I wasn't sure the correct way to open it with the pointer.

    Does anyone know how i can fix these two problems?

    Code:
    #include <cstring>
    #include <cstdio>
    
    FILE* open(const char filename[])
    {
    FILE fp;
       if(fp!=Null)
       {
       fp=fopen("filename","r");//doesnt work
       }
    
          else
          {
          fp=NULL;
          }
    return fp;
    }
    
    
    int registeredA(FILE* fp, int area)
    {
    fp=fopen("prefix.txt","r");  //I know this is wrong this the file I want to open but dont know how with this function
    }

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    The variable filename should not be in quotes...

    Code:
    FILE* open(const char Filename)
     { return fopen(Filename,"r"); }
    then...
    Code:
    fp = open("prefix.txt");
    Thing is that's such a thin wrapper you might just as well call fopen directly...
    fopen returns a file handle or null... so you gain nothing by plumping it out, you still have to check it after it returns.

    Code:
    // your way...
    FILE* fp = open("prefix.txt");
    if (!fp)
      { printf "file error");
         exit (0); }
    
    // opening it directly
    FILE *fp = fopen("prefix.txt");
    if (!fp)
      { printf("Holy crap the file didn't open!");
         exit (0); }
    See what I mean... the wrapper serves no purpose.

  3. #3
    Registered User
    Join Date
    Oct 2010
    Posts
    60
    Quote Originally Posted by CommonTater View Post
    The variable filename should not be in quotes...

    Code:
    FILE* open(const char Filename)
     { return fopen(Filename,"r"); }
    then...
    Code:
    fp = open("prefix.txt");
    Thing is that's such a thin wrapper you might just as well call fopen directly...
    fopen returns a file handle or null... so you gain nothing by plumping it out, you still have to check it after it returns.

    Code:
    // your way...
    FILE* fp = open("prefix.txt");
    if (!fp)
      { printf "file error");
         exit (0); }
    
    // opening it directly
    FILE *fp = fopen("prefix.txt");
    if (!fp)
      { printf("Holy crap the file didn't open!");
         exit (0); }
    See what I mean... the wrapper serves no purpose.
    thanks that fixed my first function.

    But so the second function:int registeredA(FILE* fp, int area)
    The file is being passed by a pointer to it. so it should know the name the name without me
    telling it. Correct?

    If so how to I open it without saying the direct name and using the pointer.
    Something like this fopen("fp","r")?

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    "fp" is a string containing the letters f and p. It is not a file pointer. If you fopen("fp"...etc.), you will have opened a file named fp.

    You want a pointer to the name of the file, or a string literal with the name of the file, not a file pointer itself, as the first parameter for fopen().

  5. #5
    Registered User
    Join Date
    Oct 2010
    Posts
    60
    Quote Originally Posted by Adak View Post
    "fp" is a string containing the letters f and p. It is not a file pointer. If you fopen("fp"...etc.), you will have opened a file named fp.

    You want a pointer to the name of the file, or a string literal with the name of the file, not a file pointer itself, as the first parameter for fopen().
    so by having this function int registeredA(FILE* fp, int area). The proper way to open the file would be

    Code:
    fp=fopen("prefix.txt","r");

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Thegame16 View Post
    so by having this function int registeredA(FILE* fp, int area). The proper way to open the file would be

    Code:
    fp=fopen("prefix.txt","r");
    Correct...

    C Function Reference - fopen Function

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 03:44 PM
  2. Inventory records
    By jsbeckton in forum C Programming
    Replies: 23
    Last Post: 06-28-2007, 04:14 AM
  3. Encryption program
    By zeiffelz in forum C Programming
    Replies: 1
    Last Post: 06-15-2005, 03:39 AM
  4. qt help
    By Unregistered in forum Linux Programming
    Replies: 1
    Last Post: 04-20-2002, 09:51 AM
  5. what does this mean to you?
    By pkananen in forum C++ Programming
    Replies: 8
    Last Post: 02-04-2002, 03:58 PM