Input File to Output File

This is a discussion on Input File to Output File within the C++ Programming forums, part of the General Programming Boards category; I am making a program that reads from a .txt file and outputs the names that are in my .txt ...

  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
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,272
    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, 11:39 PM
  2. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 09:39 AM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 07: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, 04:28 PM

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