How to open file?

This is a discussion on How to open file? within the C++ Programming forums, part of the General Programming Boards category; Hello, I am a beginner. I read this page already, but just one things that I don't understand. How to ...

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    7

    Question How to open file?

    Hello, I am a beginner. I read this page already, but just one things that I don't understand. How to open the example.txt when i type open? And the code below is the whole code.

    Code:
    #include <fstream>
    #include <iostream>
    
    using namespace std;
    
    int x ;
    char y ;
    char op ;
    
    int main()
    {
        int x ;
        char y[1000] ;
      char str[10];
      char op [5] ;
    
      //Creates an instance of ofstream, and opens example.txt
      ofstream a_file ( "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();
      
      // Close the file stream explicitly
    
       a_file.close();
     
      //Opens for reading the file
      ifstream b_file ( "example.txt" );
      //Reads one string from the file
      b_file>> str;
      //Should output 'this'
      cout<< str <<"\n";
      cin.get();    // wait for a keypress
      // b_file is closed implicitly here
    }

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,806
    You want to open the file but only when the user types "open"? Seems you'll need some kind of input processing loop that asks the user for a command and handles the options that the user enters. You can prompt the user and read their choice into a std::string variable. After that, it's just a series of if/else-if constructs to determine what the user entered and to execute the desired functionality.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User
    Join Date
    Feb 2009
    Posts
    7
    I also tried that, but i don't know to check weather the user type open to open the file. I tried this:
    Code:
    char op[6] ;//i put this after using namespace std;
    /////////////////////////////////////////////////////////////////
    char op[6] ;// i put this after the int main(){
    ///////////////////////////////////////////////////////////////
    cin>> op ;
    if  ( op = 'open' ) {
    cout<<"this is to test weather the code right ..." ;
    }
    but it gave me error: 32 G:\ccc\dllmain.cpp incompatible types in assignment of `int' to `char[6]'
    i can't figure out how to fix it.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Use double quotes for a string, and use == for comparing if equal. One = means "assign".

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Feb 2009
    Posts
    7
    Code:
    cin>> op ;
    if  ( op == "open" ) {
    cout<<"this is to test weather the code right ..." ;
    }
    the error didn't come out again but the message didn't show up even i type open.

  6. #6
    Registered User
    Join Date
    Feb 2009
    Posts
    2
    use strcmp to compare string, don't use ==

    Code:
    #include <iostream>
    #include <fstream>
    
    using namespace std;
    
    int main(int argc, char *argv[])
    {
    
        int x ;
        char y[1000] ;
        char str[10];
        char op [5] ;
    
        cout << "Wait for input: ";
        // get input, if the input is not "open", wait for another input
        while (true) {
            cin >> op;
            if(strcmp(op, "open")== 0) break;
            else cout << endl << "Hint: open" << endl << "type again:" ;
        }
    
        
        //Creates an instance of ofstream, and opens example.txt
        ofstream a_file ( "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();
        
        // Close the file stream explicitly
        
        a_file.close();
        
        //Opens for reading the file
        ifstream b_file ( "example.txt" );
        //Reads one string from the file
        b_file>> str;
        //Should output 'this'
        cout<< str <<"\n";
        
        system("pause");
    }

  7. #7
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    Quote Originally Posted by w.chen View Post
    use strcmp to compare string, don't use ==
    or even better - do not use C-strings, use std::string instead
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  8. #8
    Registered User
    Join Date
    Feb 2009
    Posts
    7
    Yea, thanks w.chen. But if i want to open the output file when i type open, does that possible?

  9. #9
    Registered User
    Join Date
    Feb 2009
    Posts
    2
    I don't understand what do you mean "open the output file", if you mean open it using some sort of texteditor, you can call system("command") to do it. e.g. system("notepad.exe example.txt")

    if you want to open the file and do something with it in your c++ program, using ofstream and ifstream as the code you've provided.

    if you mean just type open (without enter) to open the file, it is possible but I won't suggest you to do so.

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,022
    Quote Originally Posted by w.chen View Post
    use strcmp to compare string, don't use ==
    Code:
    #include <iostream>
    #include <fstream>
    
    using namespace std;
    
    int main(int argc, char *argv[])
    {
    
        int x ;
        std::string y;
        std::string str;
        std::string op;
    
        cout << "Wait for input: ";
        // get input, if the input is not "open", wait for another input
        while (true) {
            cin >> op;
            if (op == "open") break;
            else cout << endl << "Hint: open" << endl << "type again:" ;
        }
    
        
        //Creates an instance of ofstream, and opens example.txt
        ofstream a_file ( "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();
        
        // Close the file stream explicitly
        
        a_file.close();
        
        //Opens for reading the file
        ifstream b_file ( "example.txt" );
        //Reads one string from the file
        b_file>> str;
        //Should output 'this'
        cout<< str <<"\n";
        
        system("pause");
    }
    It's truly amazing how people come into the C++ forum and offer C-style solutions...
    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.

  11. #11
    Registered User
    Join Date
    Feb 2009
    Posts
    7
    f you want to open the file and do something with it in your c++ program, using ofstream and ifstream as the code you've provided.
    Do you mean this code:
    Code:
     
    f you want to open the file and do something with it in your c++ program, using ofstream and ifstream as the code you've provided.
    or this:
    Code:
    a_file.close();
        
        //Opens for reading the file
        ifstream b_file ( "example.txt" );
        //Reads one string from the file
        b_file>> str;
        //Should output 'this'
        cout<< str <<"\n";
    after the user type some numbers and alphabet , it will make a notepad with name : example.txt . If the user type "open" and pressed enter, i want it to open the example.txt.
    Last edited by heitohell; 02-20-2009 at 11:18 PM.

  12. #12
    Registered User
    Join Date
    Feb 2009
    Posts
    7
    Ok, now i saw a code like this:
    Code:
    #include <stdio.h>
    #include <iostream>
    
    using namespace std;
    
    
    int main ()
    {
      FILE * pFile;
      pFile = fopen ("myfile.txt","w");
      fopen ("myfile.txt","r");
      if (pFile!=NULL)
      {
        fputs ("fopen example",pFile);
        fclose (pFile);
        cin.ignore();
      }
      return 0;
    }
    i tried ran this program, but nothing opened, why?
    I want to sure weather the fopen got error or the location is unspecific enough?

  13. #13
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,022
    1) This is the C way. This is way you do in C, not C++.
    2) And, it's open in read-only mode. Meaning no writes can be done on the file.
    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.

  14. #14
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    Quote Originally Posted by Elysia View Post
    2) And, it's open in read-only mode. Meaning no writes can be done on the file.
    It opens same file twice - first for writing, then for reading (without storing the pointer to the file)
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  15. #15
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,022
    Ah, that's true too. I missed that...
    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.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 09:35 AM
  2. Need Help Fixing My C Program. Deals with File I/O
    By Matus in forum C Programming
    Replies: 7
    Last Post: 04-29-2008, 08:51 PM
  3. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  4. Encryption program
    By zeiffelz in forum C Programming
    Replies: 1
    Last Post: 06-15-2005, 04:39 AM

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