Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C++ (http://www.go4expert.com/forums/cpp/)
-   -   problem with open file. (http://www.go4expert.com/forums/file-t24551/)

oror84 15Jan2011 16:57

problem with open file.
 
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.

pein87 17Jan2011 13:14

Re: problem with open file.
 
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


}



All times are GMT +5.5. The time now is 16:05.