problem with open file.

Discussion in 'C++' started by oror84, Jan 15, 2011.

  1. oror84

    oror84 New Member

    Joined:
    Jan 15, 2011
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0
    Hi, i have a problem.
    i have a file that there is a line in the file : #include "hello.h"
    i have to open the "hello.h" file and copy it to another file.
    i did :

    char getline[MAX_LINE];
    char *fp1;


    fp1 = strchr(getline2,'#'); /* fp1 = #include ..., taking the include from the line */
    fp1 = strchr(fp1,'"'); /* fp1 = "hello.h" */
    FILE *file = fopen(fp1,"w");

    the file hello.h is in the main libary and the file not opened.

    i hope i was understandable, and if someone can help me i`ll appreciate it.
    Thanks,
    Or.
     
  2. pein87

    pein87 Active Member

    Joined:
    Aug 6, 2010
    Messages:
    173
    Likes Received:
    47
    Trophy Points:
    28
    Occupation:
    Web Dev
    Location:
    Limbo
    You need to created a file stream to open the file. Try the code below I'm rusty in C++ but to open a file you need to create a new fstream, you also need to include fstream, and istream if you want to read and open the file. In C you would use fopen and fread functions instead. Make sure your directory for the hello.h is correct that maybe why its not being included and causing the compile error.

    Code:
    #include<fstream>
    #include<istream>
    #include<ostream>
    use name space std;
    
    int main()
    {
    
    fstream file; // create new file stream
    
    file.open("hello.h"); // open file
    
    char contents = file.getline();// get all contents till end of file
    
    
    cout<<contents<<endl; // print the files content to the cmd prompt
    
    
    }
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice