need help on a primitive problem

This is a discussion on need help on a primitive problem within the C++ Programming forums, part of the General Programming Boards category; hello all, i have problems implementing a path for the following program well , the prototype says it only accepts ...

  1. #1
    بابلی ریکا Masterx's Avatar
    Join Date
    Nov 2007
    Location
    Somewhere nearby,Who Cares?
    Posts
    493

    need help on a primitive problem

    hello all, i have problems implementing a path for the following program
    well , the prototype says it only accepts , *char and char! no string! so im stuck!
    im talking about this (
    Code:
    std::ifstream f(path, std::ios::binary);
    )

    I dont know how to get it to work!


    Code:
    #include <iostream>
    #include <fstream>
    #include <iomanip>
    #include <string>
    using namespace std;
    int main()
    {
    
             unsigned char c;
             string path;
             char *route;
    
    
             cin>>route;
             std::ifstream f(route, std::ios::binary);
    
    
    
    
             while (f >> c)
            {
                    std::cout << std::hex << std::setw(2) << std::setfill('0') << (unsigned int)c <<"  ";//or << (unsigned int)c << "\t"
            }
    
    return 0;
    
    }
    Code:
    std::ifstream f(route, std::ios::binary);
    either This is not working,
    it compiles well, but , at run time when you enter a path! and press enter! it crashes! really dont know how to go about it!
    Highlight Your Codes
    The Boost C++ Libraries (online Reference)

    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.."
    Bill Bryson


  2. #2
    Registered User
    Join Date
    Mar 2009
    Posts
    31
    you can make your std::string return a valid 'char *' to itself by calling it's <string>.c_str();

    Code:
    std::string path  ("./my_file.txt");
    std::ifstream f (path.c_str(), std::ios::binary);
    And about your crashing program: You must initialize your 'char *route' to something and make it point to a valid location for std::cin to write to before calling.. std::cin

  3. #3
    بابلی ریکا Masterx's Avatar
    Join Date
    Nov 2007
    Location
    Somewhere nearby,Who Cares?
    Posts
    493
    Quote Originally Posted by edoceo View Post
    you can make your std::string return a valid 'char *' to itself by calling it's <string>.c_str();

    Code:
    std::string path  ("./my_file.txt");
    std::ifstream f (path.c_str(), std::ios::binary);
    And about your crashing program: You must initialize your 'char *route' to something and make it point to a valid location for std::cin to write to before calling.. std::cin
    thanks alot!
    Will char* route = NULL; do it?
    Last edited by Masterx; 03-18-2009 at 08:39 PM.
    Highlight Your Codes
    The Boost C++ Libraries (online Reference)

    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.."
    Bill Bryson


  4. #4
    Registered User
    Join Date
    Mar 2009
    Posts
    31
    No, you will have to make char *route point to a location where you can write data read from stdin (std::cin)

    Code:
    char *p = new char[1024];
    std::cin >> p;
    std::cout << "input: " << p << std::endl;
    
    delete p;
    But you should use std::string instead of defining your own char-buffers. The above program might not behave the way it should if the user inputs more then 1023 characters! This is because you will write out-of-bounds and might cause memorycorruption.

  5. #5
    Or working on it anyways mramazing's Avatar
    Join Date
    Dec 2005
    Location
    Lehi, UT
    Posts
    121
    when you use strings in a char area you should can use the string method c_str();

    so you could do
    Code:
    using namespace std;
    ...
    ifstream fp (path.c_str(), ios::binary);
    -- Will you show me how to c++?

  6. #6
    بابلی ریکا Masterx's Avatar
    Join Date
    Nov 2007
    Location
    Somewhere nearby,Who Cares?
    Posts
    493
    Quote Originally Posted by edoceo View Post
    No, you will have to make char *route point to a location where you can write data read from stdin (std::cin)

    Code:
    char *p = new char[1024];
    std::cin >> p;
    std::cout << "input: " << p << std::endl;
    
    delete p;
    But you should use std::string instead of defining your own char-buffers. The above program might not behave the way it should if the user inputs more then 1023 characters! This is because you will write out-of-bounds and might cause memorycorruption.
    thanks , in case im using the upper and below solutions ( which are the same! ) , is there any reason to initialize the *char var yet?

    Quote Originally Posted by mramazing View Post
    when you use strings in a char area you should can use the string method c_str();

    so you could do
    Code:
    using namespace std;
    ...
    ifstream fp (path.c_str(), ios::binary);
    tanx.
    Highlight Your Codes
    The Boost C++ Libraries (online Reference)

    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.."
    Bill Bryson


  7. #7
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,305
    Quote Originally Posted by edoceo View Post
    Code:
    char *p = new char[1024];
    std::cin >> p;
    std::cout << "input: " << p << std::endl;
    
    delete p;
    That uses the wrong delete operator and you should not needlessly put small things that exist for a small scope on the heap. This is what the stack is for.
    This is far better:
    Code:
    char p[1024];
    std::cin >> p;
    std::cout << "input: " << p << std::endl;
    No chance of a leak now, but you could still get a buffer overrun, and p is a poor variable name.
    Code:
    std::string route;
    std::cin >> route;
    std::cout << "input: " << route << std::endl;
    Now you're talking!
    Last edited by iMalc; 03-19-2009 at 12:24 AM.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  8. #8
    Registered User
    Join Date
    Mar 2009
    Posts
    31
    Quote Originally Posted by iMalc View Post
    That uses the wrong delete operator and you should not needlessly put small things that exist for a small scope on the heap. This is what the stack is for.
    This is far better:
    Code:
    char p[1024];
    std::cin >> p;
    std::cout << "input: " << p << std::endl;
    No chance of a leak now, but you could still get a buffer overrun, and p is a poor variable name.
    Code:
    std::string route;
    std::cin >> route;
    std::cout << "input: " << route << std::endl;
    Now you're talking!
    yes, delete[] should have been used. I was in a hurry catching my ride down to Gothenburg (Sweden) (and it was 3:41 am). no sleep for 38 hours + stress = bad combination

    And since OP wanted to initiate a 'char *' I wrote an example using just that.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 10:22 AM
  2. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  3. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  4. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  5. beginner problem
    By The_Nymph in forum C Programming
    Replies: 4
    Last Post: 03-05-2002, 04:46 PM

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