Thread: Files

  1. #1
    Registered User
    Join Date
    Sep 2006
    Location
    vancouver wa
    Posts
    221

    Files

    I am trying to read in a previously made file from user input.
    I have googled and thumbed through a couple of books and i can see whats wrong. the file is placed in the same location as if i would have made a file from the console. I get cannot open file
    Code:
    char FileName[20];
    ifstream infile;
    
      
        cout << "Enter the name of the file you want to open: ";
        cin >> FileName;
        
        infile.open(FileName, ios::in);
        infile >> FileName;
    
        cout << "\file contents: " << FileName;
        
        if(!infile) {
    	cout << "cannot open file";
         }

  2. #2
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    How are you running your program? Inside an IDE?

  3. #3
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Try specifying the full path of the file. The problem is most likely that your working directory isn't what you think it is.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  4. #4
    Registered User
    Join Date
    Sep 2006
    Location
    vancouver wa
    Posts
    221
    I am using VS 2005
    I would like to ask the user for input file..
    what is the working directory?

  5. #5
    Registered User
    Join Date
    Sep 2006
    Location
    vancouver wa
    Posts
    221
    i also tried
    Code:
    ifstream infile;
    string myfile;
    
    char process[200];
    
    ifstream inputFile;
    
    cout << "enter file name";
    cin >> myfile;
    
    //inputFile.open (inputFileName, ios::in);
    inputFile.open (myfile.c_str(), ios::in);
    
    if(!inputFile)
     {
      cerr << "Can't open input file " << myfile << endl;
      cin >> reply;
      exit(1);
    }
    i get cant open file.
    I can create a file by specifying and reading it but not user input.
    please help!

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Show us the actual input and output.

    Also, your code won't handle filenames with spaces, so typing in a full path of something like
    c:\documents and settings\me\....
    won't work for your current code.
    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.

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    The possibilities seem to be
    1. The file does not exist in the current working directory (which for VS2005 is, IIRC, the base directory for the project if you're running from the IDE) or a full path was not specified.
    2. The file name contains spaces, since >> will quite simply not read spaces.

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    The second can be fixed by using std::getline.
    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 2006
    Location
    vancouver wa
    Posts
    221
    I am not using any spaces in the filename. The file is called scores, and I put the file in all directories of the project just to make sure. I will display the output at the bottom. I realize I have not cloded it yet.

    Code:
    #include "stdafx.h"
    #include <iostream>
    #include <fstream>
    #include <string>
    
    using namespace std;
    
    int _tmain(int argc, _TCHAR* argv[])
    {
    
     char reply;
    ifstream infile;
    string myfile;
    
    char process[200];
    
    ifstream inputFile;
    
    cout << "enter file name: ";
    cin >> myfile;
    
    //inputFile.open (inputFileName, ios::in);
    inputFile.open (myfile.c_str(), ios::in);
    
    if(!inputFile)
     {
      cerr << "Can't open input file " << myfile << endl;
      cin >> reply;
      exit(1);
    }
    while(inputFile)
    {
    
    inputFile.getline(process,200);
    
    
    cout << "Contents of file are:" << process;
    
    }
    
    
     cin >> reply;
      return 0;
    }


    Running the program:
    Code:
    enter file name: scores
    Can't open input file scores

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Are you sure that's what it's called?

    I mean, do you have the "helpful" explorer option of "hide known extensions" turned on?
    Another piece of brain-rot from MS. You read the (incomplete) filename from exploiter, and then wonder why you can't find the file.

    Sure, why not, I've got a nice fun.jpg.exe file to send to you 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.

  11. #11
    Registered User
    Join Date
    Sep 2006
    Location
    vancouver wa
    Posts
    221
    It was on hide extensions, but i unchecked it and it still does the same thing.
    the file is scores.txt

    Now I got it to open when I did scores.txt
    is there a way to do it without having to specify the extension?
    thank you for your help salem
    Last edited by mrsirpoopsalot; 01-19-2009 at 12:13 PM.

  12. #12
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by mrsirpoopsalot View Post
    It was on hide extensions, but i unchecked it and it still does the same thing.
    the file is scores.txt

    Now I got it to open when I did scores.txt
    is there a way to do it without having to specify the extension?
    thank you for your help salem
    You CAN have a file with no extension, but generally we prefer to have an extension on files so that we can tell what (type of content) is in the file. The name, generally tell us what the cotnent is about, but not what sort of program we need to open the file.

    Another option is that you add an extension if it doesn't open in the first place - add ".txt" to the filename and try opening that. If that also doesn't work, say can't open the file.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  13. #13
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    In Windows, it is pretty much taboo to not have an extension. In Linux, not so much, but it makes it annoying for Windows users.
    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.

  14. #14
    Registered User
    Join Date
    Sep 2006
    Location
    vancouver wa
    Posts
    221
    What if i ask them to specify where the file is along with the name?
    could they type in c\:scores.txt and have it work if the file is in that directory?

  15. #15
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by mrsirpoopsalot View Post
    What if i ask them to specify where the file is along with the name?
    could they type in c\:scores.txt and have it work if the file is in that directory?
    Have you tried that yourself?

    Assuming that you have an input method that accepts the filename, and the filename is valid, then yes, you can input a filename and use a file from anywhere that your computer can access.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Ressources files
    By mikahell in forum Windows Programming
    Replies: 4
    Last Post: 06-19-2006, 06:50 AM
  2. add source files to embedded VC 4.0
    By George2 in forum C++ Programming
    Replies: 4
    Last Post: 06-13-2006, 03:28 AM
  3. *.cpp and *.h files understanding
    By ElastoManiac in forum C++ Programming
    Replies: 4
    Last Post: 06-11-2006, 04:45 AM
  4. Linking header files, Source files and main program(Accel. C++)
    By Daniel Primed in forum C++ Programming
    Replies: 3
    Last Post: 01-17-2006, 11:46 AM
  5. Multiple Cpp Files
    By w4ck0z in forum C++ Programming
    Replies: 5
    Last Post: 11-14-2005, 02:41 PM