text


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 8 of 8

Thread: text

  1. #1
    Join Date
    Nov 2005
    Posts
    68

    text

    well i have seen the other program that is for that writes text to a txt document. but i didnt understand exactly how he didnt. i messed around with the code but didnt get it. can some one show me really quick.

    i just want the user to be able to enter text, whatever text the user wants and then the program makes a file containing that text.

  2. #2
    Join Date
    Nov 2005
    Posts
    68
    anyone?

  3. #3
    Join Date
    Nov 2003
    Posts
    4,118
    Look at the following article. It explains how to use the <fstream> library. http://www.devx.com/DevX/LegacyLink/9397 I don't know if your teacher used this library, but it should work.
    Danny Kalev

  4. #4
    Join Date
    Nov 2005
    Posts
    68

    ok i wrote some code

    this program allows the user to name the file and then write text to a .txt file. the only problem is that the program limits how many characters the user can enter. how can i fix this?
    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    using namespace std;
    
    int main () {
      ofstream myfile;
      char text[300];
      char text2[20];
      cout << "name your file (example.txt)" << endl;
      cin.getline(text2,20);
      cout << "enter your text" << endl;
      cin.getline(text,300);
      myfile.open (text2);
      myfile << text;
      myfile.close();
      system ("pause");
      return 0;
    }

  5. #5
    Join Date
    Dec 2005
    Location
    New Jersey
    Posts
    290
    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    
    using namespace std;
    
    int main () {
    	ofstream file;
    	string fileName;
    	string text;
    	
    	cout << "Enter the name of the file: ";
    	cin >> fileName;
    	cout << "Enter the text to write to " << fileName << ": ";
    	cin >> text;
    	
    	file.open(fileName.c_str());
    	file << text;
    	file.close();
    
    	return 0;
    }

  6. #6
    Join Date
    Nov 2005
    Posts
    68
    it still stops after a certain amount of characters can some one post a way to make it so that you can enter more words/characters :confused: ? should i add more strings to contain text or is there another way.

  7. #7
    Join Date
    Nov 2005
    Posts
    68
    wait i answered my own question. just so i can better my knowledge what does this code do: .c_str()

  8. #8
    Join Date
    Nov 2003
    Posts
    4,118
    c_str() returns a const char * representation of the string object. You need to use it when a function call expects a const char * argument, and you're using a std::string object. A classic example is opening a file whose name is stored in a string object. fstream::open accepts onlt char/wchar_t * names, not strings.
    Danny Kalev

Similar Threads

  1. Importing text file using schema.ini
    By Kevin in forum VB Classic
    Replies: 3
    Last Post: 12-05-2005, 07:25 PM
  2. Text
    By Mus in forum Java
    Replies: 2
    Last Post: 04-05-2002, 02:39 AM
  3. Top fixed screen
    By Cheng in forum Web
    Replies: 1
    Last Post: 11-06-2001, 10:43 AM
  4. Script for scrolling
    By Mark in forum Web
    Replies: 3
    Last Post: 08-30-2001, 12:45 PM
  5. Double Text 1.0
    By George Gilbert in forum vb.announcements
    Replies: 0
    Last Post: 08-19-2001, 12:34 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center