problem with open file.

oror84's Avatar, Join Date: Jan 2011
Light Poster
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.
0
pein87's Avatar
Ambitious contributor
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: C++
#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


}