Editing TXT files in C++

This is a discussion on Editing TXT files in C++ within the C++ Programming forums, part of the General Programming Boards category; I have been recently going through the tutorials that your site offers. I decided to expand on one of the ...

  1. #1
    C++ and Flash Programmer
    Join Date
    Jul 2004
    Posts
    7

    Editing TXT files in C++

    I have been recently going through the tutorials that your site offers. I decided to expand on one of the tutorials. This one to be precise, http://www.cprogramming.com/tutorial/lesson10.html and created the following program from this code.


    Code:
    #include<iostream>
    #include <fstream>
    
    using namespace std;
    
    int main()
    {
        cout<<"This is a simple C++ program that writes text to a .txt file.\n Choose one of the following tasks to perform:\n";
        
        int selection;   
        char str[10];
        char filetext;             
        
        cout<<"1. Create blank text file or delete replace current file\n";
        cout<<"2. Write some preprogrammed text into the text file.\n";
        cout<<"3. Write your own text into the text file\n";
        cout<<"Enter your option: \n";
        
        cin>>selection;
        cin.ignore();
        
        if (selection == 1)
        {
            // load function for task 1
            cout<<"You chose option 1\n";
            ofstream a_file ( "example.txt" );
            a_file.close();
        }
        else if(selection == 2)
        {
            // load function for task 2
            cout<<"You chose option 2\n";
            ofstream a_file ( "example.txt" );
            a_file<<"Here is some predesigned text. Pretty cool huh!";
            a_file.close();
        }
        else if(selection == 3)
        {
            // load function for task 3
            cout<<"You chose option 3\n";
            ofstream a_file ( "example.txt" );
            cout<<"Please enter some text: ";
            cin>> filetext;
            a_file<<filetext;
            a_file.close();
            cin.ignore();
        }
        cin.get();
    }
    It's a relatively simple program but in the last if statement when I try to make it possible for users to be able to enter text of there own there is a small problem. They can only seem to enter words such as "c_programming" and "nexus" any text with spaces or captials cuts out after the first letter.

    Can anyone help me make it so that all text will work and is it possible to make it change the name of the file as well.

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Quote Originally Posted by Nexus
    It's a relatively simple program but in the last if statement when I try to make it possible for users to be able to enter text of there own there is a small problem. They can only seem to enter words such as "c_programming" and "nexus" any text with spaces or captials cuts out after the first letter.
    cin is whitespace delimited, getline isn't.
    Last edited by Dave_Sinkula; 03-22-2005 at 09:22 PM.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,803
    It is cause you are only reading in a char. So your output will only be one character long, you can something like this instead.
    Code:
    char filetext[80];
    Woop?

  4. #4
    C++ and Flash Programmer
    Join Date
    Jul 2004
    Posts
    7
    Thanks for the help.

    How would I be able to make a user change the title of the document?

  5. #5
    Registered User
    Join Date
    Sep 2004
    Posts
    80
    When you say title, do you mean the filename? From "example.txt" to WhateverIwant.txt?

    It could look like this:
    Code:
    char filename[50];
    cout << "Enter filename: ";
    cin >> filename;
    ofstream a_file(filename);
    If that's what you're looking for....
    Last edited by antex; 03-24-2005 at 04:40 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. comparing 2 txt files
    By norhos in forum C Programming
    Replies: 9
    Last Post: 03-13-2008, 06:18 AM
  2. Help with loading files into rich text box
    By blueparukia in forum C# Programming
    Replies: 3
    Last Post: 10-19-2007, 12:59 AM
  3. added start menu crashes game
    By avgprogamerjoe in forum Game Programming
    Replies: 6
    Last Post: 08-29-2007, 01:30 PM
  4. Communication with txt files (continuing)
    By Gordon in forum Windows Programming
    Replies: 9
    Last Post: 08-13-2007, 08:49 PM
  5. printing txt files
    By baphomet in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 03-10-2002, 08:53 PM

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