Im missing something (infile)

This is a discussion on Im missing something (infile) within the C++ Programming forums, part of the General Programming Boards category; here the code first off: Code: #include "stdafx.h" #include <iostream> #include <fstream> #include <string> using namespace std; int main(int argc, ...

  1. #1
    RoD
    RoD is offline
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331

    Im missing something (infile)

    here the code first off:

    Code:
    #include "stdafx.h"
    #include <iostream>
    #include <fstream>
    #include <string>
    
    using namespace std;
    
    int main(int argc, char* argv[])
    {
    	string FileLocation;
    	string Extension = ".cpp";
    	bool Quit = false;
    	ofstream	outfile;
    	ifstream	infile;
    
    	cout << "Enter path to file (no ext please): ";
    	getline(cin,FileLocation);
    	FileLocation += Extension;
    
    	infile.open("FileLocation");
    
    	return 0;
    }
    It never loads the file even if its entered correctly. I did a test cout so i know its the infile line. My theory is that the FileLocation is taken as a literal not a variable because of the quotes. Problem is i cant built it without quotes, so how can i accomplish this? I know its a lame question but never tried it before.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    That's exactly right. Why are you using quotes? That makes it a string literal, not a variable name.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    RoD
    RoD is offline
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    Well i didnt use them at first but it produced this error, how do i get around it? Different function for infile?

    --------------------Configuration: Lazy Commenter - Win32 Debug--------------------
    Compiling...
    Lazy Commenter.cpp
    C:\Program Files\Microsoft Visual Studio\MyProjects\Lazy Commenter\Lazy Commenter.cpp(23) : error C2664: 'void __thiscall std::basic_ifstream<char,struct std::char_traits<char> >:pen(const char *,int)' : cannot convert parameter 1 from 'class std:
    :basic_string<char,struct std::char_traits<char>,class std::allocator<char> >' to 'const char *'
    No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
    Error executing cl.exe.

    Lazy Commenter.obj - 1 error(s), 0 warning(s)

  4. #4
    Veni Vidi Vice
    Join Date
    Aug 2001
    Posts
    343
    Open accepts a const char*

    Try this
    Code:
    infile.open(FileLocation.c_str());

  5. #5
    RoD
    RoD is offline
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    no errors but it apparently isnt opening cuz this while i added under it doesnt execute:

    Code:
    while(!infile.fail())
    	{
    		cout << "cool" << endl;
    	}

  6. #6
    RoD
    RoD is offline
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    fixed, thnx for all the help.

  7. #7
    RoD
    RoD is offline
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    ok new question, i searched but couldnt find what i wanted.

    After i open the file i create a directory called "CPP BACKUP" on the c:\ drive. How can i make a copy of the file i opened, an exact copy, and move it to the backup folder?

  8. #8
    RoD
    RoD is offline
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    i know i could read in all the data from the file then make a new one, but surely i can copy and place the copy in my folder?

  9. #9
    RoD
    RoD is offline
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    I tried searching i cant find my answer anywhere

  10. #10
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    One way if you are running Windows:

    CopyFile("oldfilename","\\CPP BACKUP\\newfilename",TRUE);

  11. #11
    RoD
    RoD is offline
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    Yea that will do it, thank you much. Last question, how can i check to see if a folder alrdy exists? Like

    if (! this directory exists)
    {
    create it here
    }
    Last edited by RoD; 05-01-2003 at 09:25 PM.

  12. #12
    RoD
    RoD is offline
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    Nothing shows up in the folder snoop.

  13. #13
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    If your compiler has the stat() function, you could use this to see if a file or directory exists.

    Or in windows, you could use:
    GetFileAttributes("dir");

    Or:
    FindFirstFile("dir",...);

  14. #14
    RoD
    RoD is offline
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    i'll give that try

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Errors including <windows.h>
    By jw232 in forum Windows Programming
    Replies: 4
    Last Post: 07-29-2008, 01:29 PM
  2. failure to import external C libraries in C++ project
    By nocturna_gr in forum C++ Programming
    Replies: 3
    Last Post: 12-02-2007, 02:49 PM
  3. more then 100errors in header
    By hallo007 in forum Windows Programming
    Replies: 20
    Last Post: 05-13-2007, 08:26 AM
  4. ras.h errors
    By Trent_Easton in forum Windows Programming
    Replies: 8
    Last Post: 07-15-2005, 10:52 PM
  5. pointer to array of objects of struct
    By undisputed007 in forum C++ Programming
    Replies: 12
    Last Post: 03-02-2004, 03:49 AM

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