Thread: Opening a file with a name in a variable

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    48

    Opening a file with a name in a variable

    Hi

    This is a constructor that receives a file name


    Code:
    Bank::Bank(char s[])
    {
     
    
     fp = fopen("s", "r");
    
     if (fp)
      printf("FILE OPENED \n");
    
     else
      printf("NO FILE WAS FOUND!!!");
    }
    But it does not work, if I put the file name in fopen it works, but if the same name is in the variable it will not open. Why doesn't this work?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You are passing the string literal "s" not the variable named s to fopen().

    By the way, are you deliberately using the file handling library inherited from C, or are you just not aware of the C++ <fstream> standard header and its contents?

    Likewise, are you deliberately using a null terminated string as inherited from C, or are you just not aware of std::string?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Code:
    fp = fopen("s", "r");
    Pass string "s" to fopen.

    Code:
    fp = fopen(s, "r");
    Pass contents of variable s to fopen.

    You need to be aware of the difference of passing a variable and a value to functions.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

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. archive format
    By Nor in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 08-05-2003, 07:01 PM
  3. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM
  4. Hmm....help me take a look at this: File Encryptor
    By heljy in forum C Programming
    Replies: 3
    Last Post: 03-23-2002, 10:57 AM
  5. what does this mean to you?
    By pkananen in forum C++ Programming
    Replies: 8
    Last Post: 02-04-2002, 03:58 PM