please explain it to me as i am very new to c++.
Which part do you mean? Well for my string thing say they entered the first part of "example.txt" and the input typed was example then you would use:
So basically you add teh file extension to the end.Code:stringx += ".txt
And for progbman:
Code:ofstream a_file ( stringx.c_str() );
Last edited by bumfluff; 04-28-2006 at 02:37 PM.
Maybe you should post your code.
Code:#include <iostream> #include <fstream> #include <iomanip> #include <string> using namespace std; int main() { string a; string b; string str; string x; cout<<"Please verify what the file name should be: "; cin>>x; cin.ignore(); ofstream a_file ( string x.c_str()ios::app); cout<<"Please input some text.\n"; cout<<">"; getline(cin,a); cin.ignore(); a_file<< a; a_file.close(); ifstream b_file("str.txt"); cout<<"File opened successfully.\n"; cout<<"Reading Data from File."<<"\n"; cout<<setfill('-')<<setw(80)<<"-"<<endl; b_file.close(); cin.get(); }
What are you trying to do as there isn't really any point to that code.
Now im just having probs getting it to read the file.Any ideas?
Have you made sure that the file is opne...checkout the tutorial and are you usiong << instead of >>
1."Have you made sure the file is open".
A. Yes.
2."are you using << instead of >>".
A. I am using >> it just closes as soon as it gets to that point in the prog.
I ave made some progress but as soon as it gets to whitespace it stops and will only print the first word. It should be printing "This is a test file" but its just printing "This" i am using getline, so whats up?
+ i cant now append to the end of the text if the user defines the name of the file, i just get an error message.
post any new code up
Code:#include <iostream> #include <fstream> #include <iomanip> #include <string> using namespace std; int main() { cout<<"Please enter a filename: "; getline(cin,x); ofstream a_file (x.c_str()); cout<<"Please input some text.\n"; cout<<">"; getline(cin,a); cin.ignore(); a_file<< a; a_file.close(); ifstream b_file( x.c_str()); cout<<"File opened successfully.\n"; cout<<"Reading Data from File."<<"\n"; cout<<setfill('-')<<setw(80)<<"-"<<endl; getline(cin, b); cin.ignore(); b_file>> b; cout<< b <<"\n"; b_file.close(); cin.get(); }
I made a few changes to it and it works now so no worrys.Thanx though.