Thread: Opening a file

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    7

    Opening a file

    I am having trouble opening file.

    [code]
    // when file location is passed as an argument on command line
    /(1)

    FILE *fp;
    fp = fopen(argv[2],"r");
    // where argv[2] holds the file location
    // this operation works for me

    BUT

    //(2)
    FILE *fp;
    char *filename;
    filename="dic.f" // where dic.f is an actual file with correct permissions

    fp=fopen(filename,"r");

    //my problem is that fp==NULL, even though when i put 'dic.f' in (1) it works fine.

    [code]

    Where is my problem??

  2. #2
    Unregistered User
    Join Date
    Sep 2005
    Location
    Antarctica
    Posts
    341
    are both versions being run in the same directory? Try a couple things. First, try to put "dic.f" in the fopen() call instead of the filename variable. Next try to see if you put the full path to dic.f in if that makes a difference.

  3. #3
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    Code:
    filename="dic.f"
    Is that how the code actually looks in your program? (You are missing the semi-colon). Perhaps you could post a little more code so we can have a better idea of what is happening.

    ~/

  4. #4
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Please excuse me if any of these make you feel ignorant, I'm sure you checked for them, as you put "actual file with correct permissions", but be sure that all the following are true.

    If FOPEN is returning null, make sure of the following:

    Your file is in the same directory as the program (even if it is, try putting the full path name in the variable)
    Your file contains nothing more than a standard text file would contain. No fancy symbols or fonts, etc.
    Your file isn't write protected (I'd assume you checked this)

    Also, I'd say that if the first code you posted using the command line arguement to pass the file name, then did you try passing "dic.f" through that code? Does it work?

    Again, if you did do all these things, I wouldn't be suprised, I'm just posting them because sometimes basic things slip people's mind.
    Sent from my iPadŽ

  5. #5
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    After fopen returns NULL, check errno, and output the error with strerror(errno).

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    And once you're finished with a file, you should close it with fclose():
    Code:
    fclose(file_pointer);
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 09:35 AM
  2. To find the memory leaks without using any tools
    By asadullah in forum C Programming
    Replies: 2
    Last Post: 05-12-2008, 07:54 AM
  3. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM