Need Help

This is a discussion on Need Help within the C++ Programming forums, part of the General Programming Boards category; I'm making a program where the user type in a file name and opens the file. This is what I ...

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    45

    Need Help

    I'm making a program where the user type in a file name and opens the file.

    This is what I did, but it's not working.

    What do I do?

    Plz help thx.

    Code:
    #include <iostream>
    #include <string>
    #include <fstream>
    
    
    using namespace std;
    
    
    // Function prototypes                                                          
    
    
    int main()
    {
    
    
       char name[20];
       char name1[20];
    
    cout << "Please enter the filename of the encoded message:";
    
       cin >> name;
    
       cout << "Please enter the known word:";
    
       cin >> name1;
    
       ofstream outfile;
       outfile.open(name);
    
    
    
       ifstream infile;
       infile.open(name);
       if (infile.fail()) {
          cout << name << " fail to open." << endl;
          exit(1);
       }
    
    
        outfile.close();                                                         
    
    
       return 0;
    
    }

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,918
    Can you be a little more... specific?
    Also, you should probably use std::string instead of char. And std::getline instead of cin >>.
    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.

  3. #3
    Registered User
    Join Date
    Sep 2008
    Posts
    45
    Quote Originally Posted by Elysia View Post
    Can you be a little more... specific?
    Also, you should probably use std::string instead of char. And std::getline instead of cin >>.
    Well that isn't the problem I do it this way.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,166
    Like Elysia noted, you need to be more specific. How is it not working?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,764
    Perhaps it's something to do with trying to open the same file for output, then trying to open it for input as well.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    Registered User
    Join Date
    Sep 2008
    Posts
    45
    Quote Originally Posted by laserlight View Post
    Like Elysia noted, you need to be more specific. How is it not working?
    Well I'm trying to open the file, but it's not opening.

  7. #7
    Hail to the king, baby. Akkernight's Avatar
    Join Date
    Oct 2008
    Location
    Faroe Islands
    Posts
    717
    What are you using name1 for?
    And to me, that program doesn't look like it's doing anything, but I'm no expert, only trying to learn :P
    Currently research OpenGL

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,918
    Perhaps the file you are entering does not exist?
    Or perhaps you are fooled to think it exists in the current directory, but it actually exists in another directory.
    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.

  9. #9
    Registered User
    Join Date
    Sep 2008
    Posts
    45
    Code:
    #include <iostream>
    #include <string>
    #include <fstream>
    
    
    using namespace std;
    
    
    // Function prototypes                                                          
    
    
    int main()
    {
    
    
       string name;
    
       cout << "Please enter the filename of the encoded message:";
    
       getline(cin,name);
    
    
       ifstream infile;
    
       infile.open(name.c_str());
    
       if (infile.fail()) {
          cout << name << " fail to open." << endl;
          exit(1);
       }
    
       cout << " File successfully opened" << endl;
    
       infile.close();
    
       return 0;
    
    }
    Ok I got this now it has getline I run it and when I put the file name all it says is
    "File successfully opened". BUT the file DOESN'T OPEN!!!!
    ugh!!1

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    How do you know it doesn't open?

  11. #11
    Registered User
    Join Date
    Sep 2008
    Posts
    45
    Quote Originally Posted by tabstop View Post
    How do you know it doesn't open?
    Because I don't see it......

  12. #12
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    What do you expect to see?

  13. #13
    Registered User
    Join Date
    Sep 2008
    Posts
    45
    Quote Originally Posted by tabstop View Post
    What do you expect to see?
    the stuff that's inside the file. What do u think?

  14. #14
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Why would you expect to see it? You don't read the file, you just open it and close it again.

  15. #15
    Registered User
    Join Date
    Sep 2008
    Posts
    45
    Quote Originally Posted by tabstop View Post
    Why would you expect to see it? You don't read the file, you just open it and close it again.
    So I opened it and closed it?

    Why did it close?

Page 1 of 3 123 LastLast
Popular pages Recent additions subscribe to a feed

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