ifstream in opening a file

This is a discussion on ifstream in opening a file within the C++ Programming forums, part of the General Programming Boards category; Dear experts, i encountered some strange problem need a help from you, what i am doing is i am retrieving ...

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    99

    ifstream in opening a file

    Dear experts,

    i encountered some strange problem need a help from you,

    what i am doing is i am retrieving a file, from certain directory which is successful, i am having a file in my current directory( means script is wriiten so that the file will be fetched to current dorectory which is successfull)

    know i am opening a file

    Code:
    ifstream f;
       f.open(filename_);//this is the filename_ which is retrieved 
       if(f.is_open())
    {
    printf("\n file is open");
    }
    else{printf("\n unable to open the file");}
    which is giving me unable to open a file what is the reason please tell me.
    Last edited by vin_pll; 11-15-2008 at 10:12 PM.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    There's (probably) nothing wrong here. Open requires a char* or a char[], not a std::string. You can print it out to make sure that's right, doesn't have a new-line at the end or anything like that. Otherwise, one of your "successful" bits isn't.

  3. #3
    Registered User
    Join Date
    Nov 2007
    Posts
    99
    Thank you for your reply,

    even i tried this which is givng me right filename,
    Code:
    ifstream f;
    cont char *file=((char *)filename_);
    printf(" file===%s",file);//i am getting the right filename
    
     f.open(file);//this is the filename_ which is retrieved 
     if(f.is_open())
    {
    printf("\n file is open");
    }
    else{printf("\n unable to open the file");}

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    If you have the right filename (without \n or any of that), then your belief that the file exists is incorrect.

  5. #5
    Registered User
    Join Date
    Nov 2007
    Posts
    99
    Dear expert,

    it is not my believe , the file is physically present in my directory,
    any how please tell me how to know is there any \n characters are appended to the filename because when i print the filename it gives me only name of the file no other characters.

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Do something like printf("file===%s=\n", file). If the last equal sign ends up on the next line, then there's a newline there.

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,340
    Try using a full path. The current working directory is not always the same as the directory that you are running the executable from. That means that you might be looking for the file in a different directory that the place the file is at.

    If your program is supposed to assume that the executable directory is the current directory, then you might just have to change the way you run the program during testing. How are you executing it? Through an IDE or debugger? From a command line? Double-clicking?

  8. #8
    Registered User
    Join Date
    Nov 2007
    Posts
    99
    Dear Experts ,

    there is no \n in the filename that is confirmed,
    and also i tried as the daved said this ttime also the file does not open,
    after giving the full path also it shows me unable to open the file,
    what is this , but i have a file in my current directory, with small program i tested it by giving the whole path it works i think there is problem with other code here,

    any suggestions u want to give here

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Given that the code you posted works perfectly if the file exists where it is supposed to, I would say that the error is somewhere (anywhere) else.

  10. #10
    Registered User
    Join Date
    Jan 2005
    Posts
    7,340
    >> after giving the full path also it shows me unable to open the file
    >> with small program i tested it by giving the whole path it works

    What's the difference between the program that works and the one that doesn't? How are you executing your program? Did you type the path correctly in the one that didn't work with the full path (use quotes if the path contains spaces and escape the \ character if writing it directly in code)?

    After answering those questions, your best bet may be to show a complete and compilable example that demonstrates the problem.

  11. #11
    Registered User
    Join Date
    Nov 2007
    Posts
    99
    dear experts,

    may be but when i do getcwd() it is showing me my current directory only,
    any how i there any way to know the full path of a file from the given filename,

    if u help me it will be a great favourite ,
    i am almost strruck at some time plz help me.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File transfer- the file sometimes not full transferred
    By shu_fei86 in forum C# Programming
    Replies: 13
    Last Post: 03-13-2009, 12:44 PM
  2. File Writing Problem
    By polskash in forum C Programming
    Replies: 3
    Last Post: 02-13-2009, 09:47 AM
  3. sequential file program
    By needhelpbad in forum C Programming
    Replies: 80
    Last Post: 06-08-2008, 01:04 PM
  4. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  5. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 01:53 AM

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