Location of Saved File

This is a discussion on Location of Saved File within the C++ Programming forums, part of the General Programming Boards category; For one reason or another i decided not to learn fstream back long ago when learning C++ and now i ...

  1. #1
    Call me AirBronto
    Join Date
    Sep 2004
    Location
    Indianapolis, Indiana
    Posts
    195

    Location of Saved File

    For one reason or another i decided not to learn fstream back long ago when learning C++ and now i decided to do just that. I got a good hold on it but i have one question. how do you denote where the file will be saved, i thought this would put the file in my C drive.
    Code:
    char filename[] = "c:inutput.txt";
    but it did not, it just ended up in the folder my program was in.
    Thanks for any help

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,328
    When specifying a path in windows you need to add a slash between the drive and the filename. You can use forward or backslash ('/' or '\'). The forward slash works on both windows and other platforms, but the backslash is more commonly used with windows.

    When programming, the backslash has a special meaning. It is used to create escape characters (like '\n'), So if you use the backslash you must put two into your string:

    "c:/input.txt"
    "c:\\input.txt"

  3. #3
    Call me AirBronto
    Join Date
    Sep 2004
    Location
    Indianapolis, Indiana
    Posts
    195
    worked like a dream, thanks man

  4. #4
    Registered User Code Monkey's Avatar
    Join Date
    Jan 2006
    Location
    Denton
    Posts
    11
    Another useful tip is that if you know the file is in the directory of the programs .exe, you can use relative paths:

    "./input.txt" <- if the file is in the main directory folder

    "./folder_name/input.txt" <- if the file is in a subdirectory in the main directory

  5. #5
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    No, relative paths are relative to the current working directory, which may not be the directory of your exe.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  6. #6
    Registered User Code Monkey's Avatar
    Join Date
    Jan 2006
    Location
    Denton
    Posts
    11
    Well say I send you a program such as my pong or snake game and they use relative paths to locate all the files that the game needs, now if you just move the directory we ever you want it the game still knows were its at because of the relative path. But you can only move the main directory Ex: Snake, but if you move Ex: Snake/screens some where the relative path will fail. But who would move the screens folder out of the game directory. = ]

    "./screens/mainmenu.txt"

    The "." at the front of the path pretty much will replace the c:/blah/blah/blah stuff all the time.

    Try it out and you will see why relative is better. Also "No, relative paths are relative to the current working directory, which may not be the directory of your exe" lets hope that the directory that contains the .exe also has the files needed to run. haha : )
    Last edited by Code Monkey; 01-22-2006 at 12:24 PM.

  7. #7
    Registered User Code Monkey's Avatar
    Join Date
    Jan 2006
    Location
    Denton
    Posts
    11
    Here if you compile this and you put the .exe and the input.txt file any where on you hard drive (And both .exe and input.txt are in the same folder) the .exe will still know where the input.txt is at because they are still in the same directory because of relative paths. = ]

    Code:
    #include <fstream>
    #include <conio.h>
    #include <iostream>
    
    int main() {
    	std::ifstream file_buffer;
    
    	file_buffer.open("./input.txt", std::ios::in | std::ios::beg);
    
    	if(!file_buffer) {
    		std::cout << "Couldn't find/open file" << std::endl;
    	}
    	else
    		std::cout << "File Found" << std::endl;
    
    	while(!kbhit()) {
    	}
    
    	file_buffer.close();
    	file_buffer.clear();
    
    	return 0;
    }
    Edit: I added conio.h -> kbhit()
    Last edited by Code Monkey; 01-22-2006 at 12:25 PM.

  8. #8
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    You can laugh all you want. The moment a program is in the PATH, the working directory will seldom be the program's exe directory.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  9. #9
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by Code Monkey
    Here if you compile this and you put the .exe and the input.txt file any where on you hard drive (And both .exe and input.txt are in the same folder) the .exe will still know where the input.txt is at because they are still in the same directory because of relative paths. = ]
    You completely missunderstand relative path.
    The path is relative to the current working directory and that has little to do with the location of the executable.
    Kurt

  10. #10
    Registered User Code Monkey's Avatar
    Join Date
    Jan 2006
    Location
    Denton
    Posts
    11
    Okay, you and do it your way, and I'll do it the way I've learned from other people.

  11. #11
    semi-colon generator ChaosEngine's Avatar
    Join Date
    Sep 2005
    Location
    Chch, NZ
    Posts
    597
    Code Monkey, you're an idiot. Several people have offered you correct helpful advice and you're practically insulted them. and you're still wrong.

    For future reference, try to engage brain before mouth.
    "I saw a sign that said 'Drink Canada Dry', so I started"
    -- Brendan Behan

    Free Compiler: Visual C++ 2005 Express
    If you program in C++, you need Boost. You should also know how to use the Standard Library (STL). Want to make games? After reading this, I don't like WxWidgets anymore. Want to add some scripting to your App?

  12. #12
    Registered User Code Monkey's Avatar
    Join Date
    Jan 2006
    Location
    Denton
    Posts
    11
    I'm not going to reply to a fellow idiots post, but please tell me if I was wrong for just tring to show loopshot another way of locating a file? I didn't think this was in order but I appericate helpful advice, thank you to ZuK, CornedBee for pointing that issue out of my code.

  13. #13
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    You weren't wrong for trying to show another way, but you were wrong for posting an incorrect way, and then not acknowledging the corrections we made.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  14. #14
    Registered User Code Monkey's Avatar
    Join Date
    Jan 2006
    Location
    Denton
    Posts
    11
    Sorry CornedBee

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File Writing Problem
    By polskash in forum C Programming
    Replies: 3
    Last Post: 02-13-2009, 09:47 AM
  2. To find the memory leaks without using any tools
    By asadullah in forum C Programming
    Replies: 2
    Last Post: 05-12-2008, 07:54 AM
  3. Replies: 3
    Last Post: 03-04-2005, 01:46 PM
  4. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 09:46 AM
  5. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM

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