ok well, i still cant write to a file and or read from one. i did some reading and i dont know if i am reading the wrong stuff or if i am just dense. i found an example code but i cant get it to work. could some one please tell me what is wrong with it, or post a code that will work. EXAMPLE CODE Code: #include<iostream> #include<sys/stat.h> #include<sys/types.h> #include<fstream.h> using namespace std; main() { { ofstream write ("home/p/code/gettester/test.txt");//writing to a file if (write.is_open()) { write << "This is a line."<<endl; write << "This is another line."<<endl; write.close(); } else cout << "Unable to open file"; } string line; ifstream read ("pathname/file.txt");//reading a file if (read.is_open()) { while (! read.eof() ) { getline (read,line); cout<<line<<endl; } read.close(); } else cout << "Unable to open file"; } } thanks, locksmith.
What is in the file home/p/code/gettester/test.txt (NB: relative path, maybe try absolute paths until you get it working, anyway if the program is run in /usr/cpulocksmith then the file it looks for will be /usr/cpulocksmith/home/p/code/gettester/test.txt) after the program has run, and what is in pathname/file.txt (NB1: relative path; NB2: is the directory really called "pathname"?) before the program is run, and what is displayed by the program? Be sure to copy and paste exactly what is on the screen and in the files.
ok well, there is nothing in text.txt it is just a blank text file. also i think that #include<ifstream> is wrong. i think maybe it should be #include<fstream>. also just for the hell of it i shortened... [censored] god damn it.i just now got it... right this vary moment... let me see if it will work... yup... god damn it, im dumb... i am logged onto p account(dont ask about the name) for some reason i was writing the directory to the file right from the start of the file system, when all i need to do is start at my account... instead of home/p/code/test.txt all i needed was code/test.txt. man im dumb...
Not at all - you got it, so well done! Programming seems to be one of those odd things where no matter how good you are you keep on making basic mistakes and then have to spend hours hunting that mistake down. I once spent two weeks looking for a bug that turned out to be a missing semicolon or something daft like that, or maybe it was two indented lines of code after an if, WITHOUT braces to contain them.
Anyway as a result of this you'll now have a little alarm bell that rings in your head whenever you see/use a relative path...remember "NB: relative path"? - that's my alarm bell Here's an idea that you might find useful. As you find errors, don't just forget them and move on, write them down in a little notebook and review it from time to time; this will keep the errors you made, how you solved them and importantly how to avoid making them in the first place fresh in your mind.