Thread: Need Help

  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
    Location
    Inside my computer
    Posts
    24,654
    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
    28,412
    Like Elysia noted, you need to be more specific. How is it not working?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    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.

  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
    Location
    Inside my computer
    Posts
    24,654
    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,336
    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,336
    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,336
    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?

Popular pages Recent additions subscribe to a feed