the file i/o tut seems to be wrong or sumthin. on dev c++ bloodshed, the example program wont compile. i even have a .txt file with the same name as the the program says, and it just wont compile! whats the proper coding for it to work? thx
the file i/o tut seems to be wrong or sumthin. on dev c++ bloodshed, the example program wont compile. i even have a .txt file with the same name as the the program says, and it just wont compile! whats the proper coding for it to work? thx
Post the exact code that you are using and the errors that your compiler throws at you. "It just won't compile" is not helpful at all.
My best code is written with the delete key.
Taking a guess, I'd suppose it could be this code:
http://www.cprogramming.com/tutorial/lesson10.html
If so, this compiles and runs OK for me, but it's using old style headers. You might want to change to:
... and then read up on namespaces in the FAQ.Code:#include <fstream> #include <iostream> using namespace std; ....
If that's not your problem, then my guess is worthless.
When all else fails, read the instructions.
If you're posting code, use code tags: [code] /* insert code here */ [/code]
even coding as simple as this:
and yes, i have the file help.txt in the same folder as the program. heres the compiling errors i get in bloodshed:Code:#include <iostream> #include <fstream> using namespace std; int main() { ofstream ("help.txt", ios::trunc); a_file >> "wassup!"; a_file.close(); } system("pause"); }
/My Documents/C++ Stuff/Un-Important Working Programs/fileediting.cpp C:\My Documents\C++ Stuff\Un-Important Working Programs\C
In function `int main()':
10 C:\My Documents\C++ Stuff\Un-Important Working Programs\fileediting.cpp
` a_file' undeclared (first use this function)
(Each undeclared identifier is reported only once for each function it appears
/My Documents/C++ Stuff/Un-Important Working Programs/fileediting.cpp C:\My Documents\C++ Stuff\Un-Important Working Programs\C
At global scope:
14 C:\My Documents\C++ Stuff\Un-Important Working Programs\fileediting.cpp
ISO C++ forbids declaration of `system' with no type
14 C:\My Documents\C++ Stuff\Un-Important Working Programs\fileediting.cpp
` int system' redeclared as different kind of symbol
373 C:\Dev-Cpp\include\stdlib.h
previous declaration of `int system(const char*)'
14 C:\My Documents\C++ Stuff\Un-Important Working Programs\fileediting.cpp
invalid conversion from `const char*' to `int'
15 C:\My Documents\C++ Stuff\Un-Important Working Programs\fileediting.cpp
parse error before `}' token
-------------------------------
Code:#include <iostream> #include <fstream> #include <cstdlib> using namespace std; int main() { ofstream a_file("help.txt", ios::trunc); a_file << "wassup!"; a_file.close(); //} -- Should not be here. system("pause"); }
Last edited by Zach L.; 03-06-2004 at 09:17 PM.
hmm.....that file i/o tut must be wrong or sumthin. there for C++ right? thx so much, ill go try that
The example wont compile because it is old.
For example, it doesnt take into account namespaces, and uses the deprecated headers.
This updated example compiles fine on Dev-C++ 4.9.8.7:
Code:#include <fstream> #include <iostream> using std::ofstream; using std::ifstream; using std::cout; int main() { char str[10]; //Used later //Creates an instance of ofstream, and opens example.txt ofstream a_file("example.txt"); //Outputs to example.txt through a_file a_file<<"This text will now be inside of example.txt"; a_file.close(); //Closes up the file ifstream b_file("example.txt"); //Opens for reading the file b_file>>str; //Reads one string from the file cout<<str; //Should output 'this' b_file.close(); //Do not forget this! return 0; }
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
i am using vc++
and errors:Code:#include <iostream.h> #include <fstream.h> int main() { char str[10]; //read in later ofstream a_file(example.txt); a_file<<"set content of example.txt"; a_file.close(); ifstream b_file(example.txt); b_file>>str; cout<<str; b_file.close(); return 0; }
any idea???Code:D:\Programming\c\files\Cpp1.cpp(8) : error C2065: 'example' : undeclared identifier D:\Programming\c\files\Cpp1.cpp(8) : error C2228: left of '.txt' must have class/struct/union type D:\Programming\c\files\Cpp1.cpp(12) : error C2228: left of '.txt' must have class/struct/union type
br
example.txt needs to be in quotes like this:
"example.txt"
when used as a parameter for a constructor of an object from the fstream/ofstream/ifstream classes.
1000x thanks
it works now. thx alot