Thread: Input File to Output File

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    8

    Input File to Output File

    I am making a program that reads from a .txt file and outputs the names that are in my .txt file into an output file that the user specifies. My program compiles but then I get an error that says

    "int main(): Assertion `fin.is_open()' failed.
    Aborted"

    What am I doing wrong


    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    #include <cassert>
    #include <cfloat>
    using namespace std;
    
    int main()
    {
     cout << "Enter the name of the input file:";
     string inputname;
     getline(cin, inputname);
     ifstream fin;
     fin.open(inputname.data());
     assert ( fin.is_open() );
     int count=0;
     double reading,
     sum=0.0;
    
     for(;;)
      {
        fin >> reading;
        if (fin.eof()) break;
        count++;
      }
     fin.close();
    
     cout << "Enter name of output file:";
     string outputname;
     getline(cin, outputname);
     ofstream fout(outputname.data());
     assert(fout.is_open());
     fout << "There are " << count << " numbers.";
    
     fout.close();
     cout << "Finished!\n";
    }

  2. #2
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Perhaps you should use .c_str() instead of .data() when you access the contents of the string.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A development process
    By Noir in forum C Programming
    Replies: 37
    Last Post: 07-10-2011, 10:39 PM
  2. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. File Input and Output, simple.
    By Vber in forum C Programming
    Replies: 5
    Last Post: 11-17-2002, 02:57 PM
  5. Simple File Creation Algorithm
    By muffin in forum C Programming
    Replies: 13
    Last Post: 08-24-2001, 03:28 PM