Thread: file i/o problems

  1. #1
    Registered User
    Join Date
    May 2005
    Posts
    3

    Question file i/o problems

    i'm new to c++ and i'm trying to get a hang of file i/o. this code is making errors when i try to compile it (main() is yet to be filled):

    Code:
    #include <string>
    #include <iostream>
    #include <fstream>
    
    using namespace std;
    
    void lfuncWrite(string lsFiletowrite, string lsContentstowrite)
    {
    	ofstream File(lsFiletowrite); //load for output (writing) and delete contents.
    	File << lsContentstowrite;    //write.
    	cout << "Done.";
    }
    
    
    void lfuncRead(string lsFiletoread)
    {
    	string lsFilecontents;
    
    	ifstream File(lsFiletoread);
    	File >> lsFilecontents;
    	cout << lsFilecontents;
    }
    
    
    int main()
    {
    
    
    	return 0;
    }
    i'm using visual c++ pro by the way.

    thanks.

  2. #2
    He's trying.
    Join Date
    Apr 2005
    Location
    Missouri, US
    Posts
    70
    I was under the impression you could not use a string to name a file to read (or dynamically select a file in any way). I may be wrong, but I've tried it myself and failed miserably.
    I used Perl instead - it worked a lot better.

  3. #3
    *this
    Join Date
    Mar 2005
    Posts
    498
    Read up on fstream

    you need to change the string to a c style string
    like this:
    Code:
    void lfuncWrite(string lsFiletowrite, string lsContentstowrite)
    {
            ofstream File;
            char* fn = lsFiletowrite.c_str();
    
    	File.open(fn); //load for output (writing) and delete contents.
    	File << lsContentstowrite;    //write.
            File.close();  //close the file when done
    	cout << "Done.";
    }
    Last edited by JoshR; 05-22-2005 at 10:49 PM.

  4. #4
    Registered User
    Join Date
    May 2005
    Posts
    3
    is there a way to let the user input a file name in c++?

  5. #5
    *this
    Join Date
    Mar 2005
    Posts
    498
    ya
    you can just get input to the string, then run the string through your function.

    for example:
    Code:
    string fileNameOutput, content;
    
    cout << "Enter file name for output:";
    cin >> fileNameOutput;
    cin.get();
    cout << "Enter content:";
    getline(cin, content);
    
    void lfuncWrite(fileNameOutput, content);

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Quote Originally Posted by JoshR
    Read up on fstream

    you need to change the string to a c style string
    like this:
    Code:
     
    void lfuncWrite(string lsFiletowrite, string lsContentstowrite)
    {
    	ofstream File;
    	char* fn = lsFiletowrite.c_str();
     
    	File.open(fn); //load for output (writing) and delete contents.
    	File << lsContentstowrite; //write.
    	File.close(); //close the file when done
    	cout << "Done.";
    }
    JoshR, the changes you made are not necessary, I assume/hope you only did them to show what is going on.

    Code:
    void lfuncWrite(string lsFiletowrite, string lsContentstowrite)
    {
    	ofstream File(lsFiletowrite.c_str()); //load for output (writing) and delete contents.
    	File << lsContentstowrite;	//write.
    	cout << "Done.";
    }
    iamthejake2000, The code in red is all you need.

  7. #7
    *this
    Join Date
    Mar 2005
    Posts
    498
    ya just showing how you have a pointer to assign a string to it (in style) because thats what it has to be to pass through the open command

    you could obviously substitute lsFiletowrite.c_str() instead of fn in the open command

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    In addition, you don't need the explicit calls to open and close, and you should be using a const char* since that is what c_str() returns.

  9. #9
    *this
    Join Date
    Mar 2005
    Posts
    498
    True, but you can open and close it at different times, for example if your reading from a file and you encounter a specific character you can close it before finishing through all the characters.

    I used it to show exactly whats going on.

    Closing a file sets the eof marker so if you were using it then you wanted to open it to read from it, you should close it before re-opening it.

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. Need Help Fixing My C Program. Deals with File I/O
    By Matus in forum C Programming
    Replies: 7
    Last Post: 04-29-2008, 07:51 PM
  3. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 04:54 PM
  4. Simple File encryption
    By caroundw5h in forum C Programming
    Replies: 2
    Last Post: 10-13-2004, 10:51 PM
  5. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 02:58 AM