File I/O help please

This is a discussion on File I/O help please within the C++ Programming forums, part of the General Programming Boards category; I coded a program to open a file show content modify, clear or append some text to it. Code: #include ...

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    18

    File I/O help please

    I coded a program to open a file show content modify, clear or append some text to it.

    Code:
    #include <iostream>
    #include <fstream>
    
    using namespace std;
    
    int main()
    {
        char name[100];
        char content[100];
        char write[200];
        int sel;
        
        cout<< "Enter the name of the file to open: ";
        cin>> name;
        cin.ignore();
        ifstream a_file ( name );
        a_file.getline ( content, 100);
        cout<< "Contents of the file: "<< content;
        a_file.close();
        cout<< "\nSelect an action from below.\n1.)Modify  2.)Clear  3.)Add some more text: ";
        cin>> sel;
        
        if (sel == 1)
        {
                ofstream b_file ( name );
                cout<< "Enter something to write to the file: ";
                cin.getline (write, 200);
                b_file<< write;
                b_file.close();
                cin.ignore();
                cout<< "File modified successfully.";
                } else if (sel == 2)
                {
                       ofstream b_file (name, ios::trunc);
                       cout<<"File cleared Successfully.";
                       b_file.close();
                       } else if ( sel == 3 )
                       {
                              ofstream b_file (name, ios::app);
                              cout<< "Enter something to add to the file: ";
                              cin.getline ( write, 200 );
                              b_file<< write;
                              cin.ignore();
                              cout<< "Text added to the file seccessfully.";
                              b_file.close();
                              }else {
                                    cout<< "Bad selection press 'Enter' to close";
                                    }
        cin.get();
    }
    I'm stuck in if codes
    Code:
     else if (sel == 2)
                {
                       ofstream b_file (name, ios::trunc);
                       cout<<"File cleared Successfully.";
                       b_file.close();
                       }
    works fine but the other twos only open and clear file but don't write to file while they have to do so any help will be welcomed.

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,996
    Code of yours is confusing to say the least. First, I'm going to clean it up and see what we can find.

    Code:
    #include <iostream>
    #include <fstream>
    
    using namespace std;
    
    int main()
    {
    	char name[100];
    	char content[100];
    	char write[200];
    	int sel;
    
    	cout << "Enter the name of the file to open: ";
    	cin >> name;
    	cin.ignore();
    	ifstream a_file(name);
    	a_file.getline(content, 100);
    	cout << "Contents of the file: " << content;
    	a_file.close();
    	cout << "\nSelect an action from below.\n1.)Modify  2.)Clear  3.)Add some more text: ";
    	cin >> sel;
    
    	if (sel == 1)
    	{
    		ofstream b_file(name);
    		cout << "Enter something to write to the file: ";
    		cin.getline(write, 200);
    		b_file << write;
    		b_file.close();
    		cin.ignore();
    		cout << "File modified successfully.";
    	}
    	else if (sel == 2)
    	{
    		ofstream b_file(name, ios::trunc);
    		cout << "File cleared Successfully.";
    		b_file.close();
    	}
    	else if (sel == 3)
    	{
    		ofstream b_file(name, ios::app);
    		cout << "Enter something to add to the file: ";
    		cin.getline(write, 200);
    		b_file << write;
    		cin.ignore();
    		cout << "Text added to the file seccessfully.";
    		b_file.close();
    	}
    	else
    	{
    		cout << "Bad selection press 'Enter' to close";
    	}
    	cin.get();
    }
    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.

  3. #3
    Registered User
    Join Date
    Mar 2008
    Posts
    18
    Ok I fixed it I had to put
    Code:
    cin.ignore();
    after
    Code:
     cin>> sel;
    but thanx for the reply.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 09:35 AM
  2. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  3. 2 questions surrounding an I/O file
    By Guti14 in forum C Programming
    Replies: 2
    Last Post: 08-31-2004, 12:21 AM
  4. File I/O problems!!! Help!!!
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 05-17-2002, 09:09 PM
  5. advice on file i/o
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 11-29-2001, 05:56 AM

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