Opening a file with a name in a variable

This is a discussion on Opening a file with a name in a variable within the C++ Programming forums, part of the General Programming Boards category; Hi This is a constructor that receives a file name Code: Bank::Bank(char s[]) { fp = fopen("s", "r"); if (fp) ...

  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
    22,112
    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?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,888
    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-22-2008, 12:19 AM
  2. archive format
    By Nor in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 08-05-2003, 08:01 PM
  3. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 05: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

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