ofstream a_file Help

This is a discussion on ofstream a_file Help within the C++ Programming forums, part of the General Programming Boards category; How can i get the file name to be the id number? I have tried ofstream a_file(id".txt"); but got an ...

  1. #1
    Registered User
    Join Date
    Apr 2003
    Posts
    33

    ofstream a_file Help

    How can i get the file name to be the id number? I have tried ofstream a_file(id".txt"); but got an error. Thanks.

    #include <iostream.h>
    #include <fstream.h>

    int main()
    {
    int id=0;

    id++;

    ofstream a_file("example.txt");
    a_file<<"hello:
    a_file.close();

    return 0;
    }

  2. #2
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Code:
    string s;
    s = id + ".txt";
    ofstream a_file(s.c_str( ));
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  3. #3
    Registered User slaveofthenet's Avatar
    Join Date
    Apr 2003
    Posts
    80
    You can't just add the int onto the string unless you want the ascii value of that int. For instance:
    int i = 65;
    string s = "";
    s += i;
    cout << s;
    will output 'A' not 65. You'll want to convert the int to a string first, try this: http://faq.cprogramming.com/cgi-bin/...&id=1043284385

  4. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    33
    Can you explain how this works alittle? Sorry I am a newb.

  5. #5
    Registered User
    Join Date
    Apr 2003
    Posts
    33
    This is what i have in my program:

    Code:
    string s = "";
    s += id;
    
    ofstream a_file(s+".txt");
    I get these errors:

    Error : undefined identifier 'string'
    hello.cpp line 151 string s = "";

    Error : undefined identifier 's'
    hello.cpp line 152 s += id;

    Error : undefined identifier 's'
    hello.cpp line 154 ofstream a_file(s+".txt");

  6. #6
    Registered User slaveofthenet's Avatar
    Join Date
    Apr 2003
    Posts
    80
    You must include the string library.
    #include <string>
    using namespace std;
    Add these 2 lines to the top of your source file and those 3 errors should go away.

  7. #7
    Registered User
    Join Date
    Apr 2003
    Posts
    33
    Here is my code and the error i get now is at the bottom.

    Code:
    #include <iostream.h>
    #include <iomanip.h>
    #include <fstream.h> 
    #include <sstream.h>
    #include <string>
    using namespace std;
    
    int main()
    {
    
    	char address[70];
    	char name[50];
    	char phone[20];
    	char description[200];
    	int zip;
    	int correct=2;
    	int id=0000;
    	int id2;
    	int option;
    	int quanf=1;
    	int quan46=1;
    	int qaun57=1;
    	int quan810=1;
    	int quanf57=1;
    	double pricef;
    	double price46;
    	double price57;
    	double price810;
    	double pricef57;
    	double product[10];
    	double cash_tend=0.00;
    	double change;
    	double total;
    ;
    	
    cout<<"Welcome to my first Register Program\n";
    cout<<"*******Good Luck Today*******\n";
    
    for (int i=0;i<100;i++)
    {
    cout<<"Please chose an option:\n";
    cout<<"(1)New Customer\n(2)Previos Customer\n(3)Exit\n";
    cin>>option;
    
    switch(option)
    {
    	case 1:
    		while(correct==2)
    		{
    			id++;
    			
    			cin.ignore();
    			cout<<"Please enter name:\n";
    			cin.getline(name, 50, '\n');
    			
    			
    			cout<<"Please enter address:\n";
    			cin.getline(address, 70, '\n');
    			 
    			
    			cout<<"Please enter zip code:\n";
    			cin>>zip;
    			 
    			cin.ignore();
    			cout<<"Please enter phone number:\n";
    			cin.getline(phone, 20, '\n');
    			 
    			
    			cout<<"Please enter description:\n";
    			cin.getline(description, 200, '\n');
    			
    			
    			cout<<endl<<"Name: "<<name<<endl;
    			cout<<"Address: "<<address<<endl;
    			cout<<"Zip Code: "<<zip<<endl;
    			cout<<"Phone: "<<phone<<endl;
    			cout<<"Description: "<<description<<endl;
    		
    		cout<<"\nIs this information correct?\n(1)Yes\n(2)No\n";
    		cin>>correct;
    		}
    		
    		product[0]=10.00;	//Product one: 4X6
    		product[1]=10.00;	//Product two: Frame
    		product[2]=15.00;	//Product three: 5X7
    		product[3]=15.00;	//Product four: Frame with 5X7
    		product[4]=20.00;	//Product five: 8X10
    		product[5]=0.00;	//Product six: Other
    		
    		correct=2;
    		while(correct==2)
    		{
    			cout<<"How many 4X6 were purchase:\n";
    			cin>>quan46;
    			
    			cout<<"How many 5X7 were purchase:\n";
    			cin>>qaun57;
    			
    			cout<<"How many 8X10 were purchase:\n";
    			cin>>quan810;
    			
    			cout<<"How many FRAMES were purchase:\n";
    			cin>>quanf;
    			
    			cout<<"How many FRAMES W/ 5X7 were purchase:\n";
    			cin>>quanf57;
    			
    			cout<<"4X6: "<<quan46<<endl;
    			cout<<"5X7: "<<qaun57<<endl;
    			cout<<"8X10: "<<quan810<<endl;
    			cout<<"Frame(s): "<<quanf<<endl;
    			cout<<"Frames w/ 5X7: "<<quanf57<<endl;
    			
    			cout<<"\nIs this Correct?\n(1)Yes\n(2)No\n";
    			cin>>correct;	
    		}
    		
    		price46=quan46*product[0];
    		price57=qaun57*product[2];
    		price810=quan810*product[4];
    		pricef=quanf*product[1];
    		pricef57=quanf57*product[3];
    		
    		total=price46+price57+price810+pricef+pricef57;
    		
    		cout<<"\nTotal Cost: "<<total<<endl;
    		cout<<"Cash Tended: \n";
    		cin>>cash_tend;
    		change=cash_tend-total;
    		cout<<"Change: "<<change<<endl;
    		
    
    		break;	
    			
    	case 2:
    			cout<<"Please enter there ID number\n";
    			cin>>id2;
    			//Will then search for the id number in the output file.
    		break;
    
    	case 3:
    			cout<<"This is just a test case 3\n";
    		break;
    		
    	default:
    		cout<<"Your entered an Invalid choice\n";
    };
    
    
    cout<<endl<<(char)id<<endl;
    
    string s = "";
    s += id;
    
    ofstream a_file(s+".txt");
    a_file<<id<<endl<<name<<endl<<address<<endl<<zip<<endl<<phone<<endl<<description<<endl<<quan46<<" 4X6"<<endl<<qaun57<<" 5X7"<<endl<<quan810<<" 8X10"<<endl<<quanf<<" Frames"<<endl<<quanf57<<" Frames w/ 5X7"<<endl<<"Total: "<<total<<endl<<"Cash Tended: "<<cash_tend<<endl<<"Change: "<<change<<endl<<endl<<endl;
    a_file.close();
    
    correct=2;
    }
    
    
     return 0;
    }
    Error : function call 'basic_ofstream(std::basic_string<char, std::char_traits<char>, std::allocator<char>>)' does not match
    'std::basic_ofstream<char, std::char_traits<char>>::basic_ofstream()'
    'std::basic_ofstream<char, std::char_traits<char>>::basic_ofstream(const char *, std::ios_base:penmode)'
    'std::basic_ofstream<char, std::char_traits<char>>::basic_ofstream(const std::basic_ofstream<char, std::char_traits<char>> &)'
    hello.cpp line 155 ofstream a_file(s+".txt");

  8. #8
    Registered User
    Join Date
    Nov 2002
    Posts
    1,109
    Originally posted by XSquared
    Code:
    string s;
    s = id + ".txt";
    ofstream a_file(s.c_str( ));

  9. #9
    Registered User
    Join Date
    Apr 2003
    Posts
    33
    Ok i use the code:
    Code:
    string s;
    s = id + ".txt";
    ofstream a_file(s.c_str( ));
    but the file name that it outputs to is just txt then the next file is xt and then just t and then it stops. Why? and How can i fix it?

  10. #10
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Code:
    #include <sstream>
    
    //...
    
    ostringstream ss;
    
    for( whatever ) {
    
        ss.flush( );
        ss<<id<<".txt";
        ofstream a_file( ss.str( ).c_str( ) );
    
    }
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. ofstream and FILE behaviour
    By MrLucky in forum C++ Programming
    Replies: 7
    Last Post: 06-21-2007, 05:45 PM
  2. Ofstream... broke as a result of ifstream fix
    By tms43 in forum C++ Programming
    Replies: 3
    Last Post: 11-12-2006, 10:40 AM
  3. Capturing key presses
    By cgod in forum Windows Programming
    Replies: 6
    Last Post: 11-25-2004, 12:10 PM
  4. Using ofstream in private of a class-errors
    By loobian in forum C++ Programming
    Replies: 3
    Last Post: 12-13-2003, 09:06 PM
  5. ofstream and ifstream for searching and writing
    By johnnyd in forum C++ Programming
    Replies: 6
    Last Post: 03-17-2003, 07:34 AM

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