:-) thx again!
so, I just had some time and thought "hey, let's begin with the second method (i.e. read_file()
I made some mistakes, now it works that it does recognize when there is no file that could be opend AND it opens a file, and prints out the first word. Why only the first word? Shouldn't it print out the whole content of the file?
Code:void read_file(){ std::string readfile; // this is the object where the filename goes into std::string readtext; // this is the object where the read in text goes into std::cout << "Please enter the filename of the file you want to open: \n"; std::getline(std::cin,readfile); // get the user to input the filename std::ifstream userfile (readfile.c_str()); if(!userfile.is_open()){ // check if the file could be opend std::cout<<"The file you tried to open could not be opened, sorry!"; }else{ std::cout<<"You loaded the file " << readfile << "! Content below\n"; userfile>>readtext; std::cout<<readtext; } }
thanks, that did the trick :-)
the tutorial used the >> operator with a char text[] and it worked with more than one word, so I thought I could use it the same way with the ifstream-struct, but that was wrong as it seams.
Now it works!
changed the line from userfile>>readtext; to getline(userfile,readtext);
Edit says: wait a minute. I just switched to eclipse for writing my files, and it gave me an error once (not now) saying ifstream was a struct. In the c++.reference i just read it says ifstream is a class?! Why would eclipse call it a struct?
Last edited by twilight; 08-08-2009 at 02:39 AM.
So, after I got a little confused with how the tutorial on this page explains things, I found myself another tutorial, that suits me "needs" way better i.e. describes things how I need to hear them to understand. So now I am following 2 tutorials and using them complementary.
Anyhow, I tried to get on with my little program, so I had at least one thing finished ;-) Here is my new void appendto_file() - function. When I compile it, I get the following errors, what do I wrong? Thanks again for any help!! :-)
Code:../util.cpp: In function 'void appendto_file()': ../util.cpp:76: error: no match for 'operator==' in 'isfile == 'y'' ../util.cpp:76: error: no match for 'operator==' in 'isfile == 'Y'' ../util.cpp:81: error: 'ios' has not been declared ../util.cpp:81: error: 'app' was not declared in this scopeCode:void appendto_file() { std::cout << "Please enter the filename of the file you want to append text to: "; std::string filename; std::getline(std::cin,filename); std::cout << "\n"; std::ifstream appendfile (filename.c_str()); if(!appendfile.is_open()) { std::cout<<"The file you tried to open could not be opened, sorry!\n"; } else { std::cout<<"Is this the file, you wanted to open?\n\n"; std::string readtext; getline(appendfile,readtext); std::cout<<readtext; std::cout<<"\n\n"; std::cout<<"Enter y for yes and n for no: "; appendfile.close(); std::string isfile; // now we want to check if that was the file or not. std::getline(std::cin,isfile); if(isfile == 'y' || isfile == 'Y') { std::cout<<"Please enter the text you want to append: \n"; std::string appendtext; std::getline(std::cin,appendtext); std::fstream appendfile (filename, ios::app); appendfile<<appendtext; appendfile.close(); } else { std::cout<<"Sorry, please try again!\n"; } } }
Last edited by twilight; 08-12-2009 at 04:22 PM.
You cannot check whether a string equals a character, because it can't. Perhaps you want to check whether the string equals the string "Y".
Also, it's std::ios::app.
thx, I changed ios::app into std::ios::app.
Ok, so check a string against a character is not possible. Makes sense I guess. :-D But what when I use the string.c_str()-function. So to say:
When I do that, the compiler complains about:Code:if(isfile.c_str() == 'y' || isfile.c_str() == 'Y')
But I seem to fail to see where I use a pointer.Code:../util.cpp:76: error: ISO C++ forbids comparison between pointer and integer
Of course in this case, you can just drop the c_str()Code:if(isfile.c_str()[0] == 'y' || isfile.c_str()[0] == 'Y')
Code:if(isfile[0] == 'y' || isfile[0]== 'Y')
bit∙hub [bit-huhb] n. A source and destination for information.
thx, the error is now gone.
just for my understanding: isfile[0] looks like I am accessing the first char of an array of chars, but isfile is a string, or more precisely: an object. What exactly does the [0] do with an object?
Also, the following line now gets an error it did not get before :-(
error:Code:std::fstream appendfile (filename, std::ios::app);Code:../util.cpp: In function 'void appendto_file()': ../util.cpp:86: error: no matching function for call to 'std::basic_fstream<char, std::char_traits<char> >::basic_fstream(std::string&, const std::_Ios_Openmode&)' /usr/include/c++/4.0.0/fstream:704: note: candidates are: std::basic_fstream<_CharT, _Traits> ::basic_fstream(const char*, std::_Ios_Openmode) [with _CharT = char, _Traits = std::char_traits<char>] /usr/include/c++/4.0.0/fstream:690: note: std::basic_fstream<_CharT, _Traits>::basic_fstream() [with _CharT = char, _Traits = std::char_traits<char>] /usr/include/c++/4.0.0/iosfwd:95: note: std::basic_fstream<char, std::char_traits<char> > ::basic_fstream(const std::basic_fstream<char, std::char_traits<char> >&) make: *** [util.o] Error 1
The [] operator can be overloaded by any object. std::string overloads it to return the character at the passed index.
The fstream constructor takes a C style string, not a std::string. Add a .c_str() to the end of filename.
bit∙hub [bit-huhb] n. A source and destination for information.
oh, man. I had that once before :-( thx for the hint again!!
now it works. At least, it compiles. my appendto_file() function now asks for the filename, prints an error if it can't be opened, prints the content of the file if it can be opened.
the question if this is the right file works, too. y and Y gives the expected result, n or anything else too.
But what happens when the user presses y and then inputs his text is another question. The user can input text, but that text does not get appended :-( So something in the following code-snippet must be wrong:
Code:std::cout<<"Please enter the text you want to append: \n"; std::string appendtext; std::getline(std::cin,appendtext); std::fstream appendfiletwo; appendfiletwo.open (filename.c_str(), std::ios::app); appendfiletwo<<appendtext.c_str(); appendfiletwo.close();
Last edited by twilight; 08-12-2009 at 05:03 PM.
oh, tanks, that clears that issue :-)
what about my code-snippet from the most recent post? Any hint you have for me there?
I just thought a further check would be nice to see if the file-opening for the fstream worked out, so I changed the snippet from:
into:Code:std::cout<<"Please enter the text you want to append: \n"; std::string appendtext; std::getline(std::cin,appendtext); std::fstream appendfiletwo; appendfiletwo.open (filename.c_str(), std::ios::app); appendfiletwo<<appendtext.c_str(); appendfiletwo.close();
And guess what I am getting when I try to append something?Code:std::cout<<"Please enter the text you want to append: \n"; std::string appendtext; std::getline(std::cin,appendtext); std::fstream appendfiletwo; appendfiletwo.open (filename.c_str(), std::ios::app); if(!appendfiletwo.is_open()) { std::cout<<"Your file could not be opend for appending, sorry.\n"; } else { appendfiletwo<<appendtext.c_str(); appendfiletwo.close(); std::cout<<"Your data has been appended!\n"; }
Your file could not be opend for appending. So something is not working with my fstream. But what? I can't figure it out right now :-(
Edit: a string does not lose it's content once I used it by string.c_str() or does it? If it does it would be clear why fstream could not open, because my string is empty. But if it does not, then I have a problem I really can't see :-(
Is the name what you think it is?
(Since this is called filetwo....) Is the file already open?