0
hei's Avatar
hei
Light Poster
fopen is the code that return the pointer of a file structure, but i don't know very know how to return the pointer correctly that make the file open.
ofstream is a code use to open the file mentioned.
Code:
ofstream  a_file ( "test.txt", ios::app )
I tried this but still not work as I want(open the out put file/a_file/file with location:G:\ccc\example.txt).

Code:
#include <iostream>
#include <fstream>
#include <stdio.h>
#include <cstdio>

using namespace std;

int main(int argc, char *argv[])
{

    int x ;
    char y[1000] ;
    char str[10];
    char op [5] ;

FILE *fp;
    fp=fopen("g:\\ccc\\example.txt", "r");

    cout << "Wait for input: ";
    // get input, if the input is not "open", wait for another input
   

    
    //Creates an instance of ofstream, and opens example.txt
    ofstream a_file ( "G:\\ccc\\example.txt" );
    // Outputs to example.txt through a_file
    cout<<"type some numbers :";
    cin>> x ;
    a_file<<"number :" <<x ;
    cout<<"type some alphabet :";
    cin>> y ;
    a_file<<"alphabet: "<<y ;
    
    cin.ignore();
     while (true) {
        cin >> op;
        if(strcmp(op, "open")== 0){
                      ofstream a_file ( "g:\\ccc\\example.txt", ios::app ); break;
                      }
        else cout << endl << "Hint: open" << endl << "type again:" ;
    }
    
    // Close the file stream explicitly
    
    a_file.close();
    
    //Opens for reading the file
    ifstream b_file ( "g:\\ccc\\example.txt" );
    //Reads one string from the file
    b_file>> str;
    //Should output 'this'
    cout<< str <<"\n";
    
    system("pause");
}

Last edited by shabbir; 13Mar2009 at 13:40.. Reason: Local IMG Dropped
0
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
You are linking to your locale image file. Upload in sites like imageshack and then use that link to display it
0
hei's Avatar
hei
Light Poster
Quote:
[I MG]file:///C:/DOCUME%7E1/home/LOCALS%7E1/Temp/moz-screenshot.jpg[/IM G]
Opps! sorry about that, i don't where that things come from and i did not it notice.
0
xpi0t0s's Avatar, Join Date: Aug 2004
Mentor
This is where you do it correctly:
Code:
    FILE *fp;
    fp=fopen("g:\\ccc\\example.txt", "r");
You can then use fp in functions like fseek and fread to manipulate example.txt, which due to the 2nd parameter "r" has been opened in read only mode.

But I'm still not sure what you are intending. Your code also uses ifstream and ofstream on the same file and these on their own should be sufficient for the operations you want to perform. So my question is: what exactly do you think that fopen is adding to the mix? Do you think [i/o]fstream aren't opening the files properly and need extra help? (Doesn't work that way.) If you want to use fopen to open the file then you need to use fopen-related functions to access the file; you can't fopen a file then modify it with [i/o]fstream.

So here's my suggestion. Remove the FILE and fopen stuff altogether on the grounds that it's completely unnecessary.

What is your while loop doing? a_file is already open at this stage. The flow here is: if the user types open, open the already open file, break the loop, then close the file. This I don't get at all. What are you trying to achieve here?

One step at a time. If you close the file and end the program after "a_file<<"alphabet: "<<y ;", does the file contain what you expect? Open the file with your favourite text editor to find out.
0
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
Quote:
Originally Posted by hei View Post
Opps! sorry about that, i don't where that things come from and i did not it notice.
Removed from the OP
0
hei's Avatar
hei
Light Poster
Quote:
The flow here is: if the user types open, open the already open file, break the loop, then close the file. This I don't get at all. What are you trying to achieve here?
You mean that break; closed the file after open it?

Actually what am i want to achieve is a simple things, but i don't know how to make it.
1. It will ask the user type numbers then some alphabets.
2. The things the user typed will save the file named example.txt that i mentioned.
3. I want it open when the user typed open and pressed enter.
0
xpi0t0s's Avatar, Join Date: Aug 2004
Mentor
No, not the break, that doesn't affect files at all. I was referring to this code:
Code:
    // Close the file stream explicitly
    
    a_file.close();
0
xpi0t0s's Avatar, Join Date: Aug 2004
Mentor
> 3. I want it open when the user typed open and pressed enter.

You need to open a file before you can do anything with it. You cannot write data to a file that is not open (for writing).
So if the 1.2.3 above indicates the sequence of events the program should follow, then you need to keep the entered data in memory until the user types "open" to get the file open.
Only when the file is open can you enter data. So you need to move the a_file<<"number :" <<x ;
and the alphabets one to AFTER you've opened the file.

Because contrary to your design what you've actually got is:
1. open the file
2. get some numbers and write them to the file
3. get some alphabets and write them to the file
4. prompt for "open"
5. ...and now your code gets really confused (and I think you do too) because somehow you've forgotten that you've already opened the file and written the data to it.
The loop loops until the user enters "open".
If they don't then it keeps looping.
If they do it opens the file (WHICH IS ALREADY OPEN), and the break ends the loop.
After exiting the loop you then close the file (so I'm uncertain of the point of the loop - it seems to achieve absolutely nothing)

Last edited by xpi0t0s; 14Mar2009 at 14:28..
0
hei's Avatar
hei
Light Poster
Quote:
> 3. I want it open when the user typed open and pressed enter.

You need to open a file before you can do anything with it. You cannot write data to a file that is not open (for writing).
So if the 1.2.3 above indicates the sequence of events the program should follow, then you need to keep the entered data in memory until the user types "open" to get the file open.
Only when the file is open can you enter data. So you need to move the a_file<<"number :" <<x ;
and the alphabets one to AFTER you've opened the file.

Because contrary to your design what you've actually got is:
1. open the file
2. get some numbers and write them to the file
3. get some alphabets and write them to the file
4. prompt for "open"
5. ...and now your code gets really confused (and I think you do too) because somehow you've forgotten that you've already opened the file and written the data to it.
The loop loops until the user enters "open".
If they don't then it keeps looping.
If they do it opens the file (WHICH IS ALREADY OPEN), and the break ends the loop.
After exiting the loop you then close the file (so I'm uncertain of the point of the loop - it seems to achieve absolutely nothing)
Oh! I finally understand it, thanks everyone