File I/O Tutorial Not Compiling

This is a discussion on File I/O Tutorial Not Compiling within the C++ Programming forums, part of the General Programming Boards category; the file i/o tut seems to be wrong or sumthin. on dev c++ bloodshed, the example program wont compile. i ...

  1. #1
    Registered User
    Join Date
    Jul 2003
    Posts
    97

    File I/O Tutorial Not Compiling

    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

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    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.

  3. #3
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    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:

    Code:
    #include <fstream>
    #include <iostream>
    
    using namespace std;
    
    ....
    ... and then read up on namespaces in the FAQ.

    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]

  4. #4
    Registered User
    Join Date
    Jul 2003
    Posts
    97
    even coding as simple as this:

    Code:
    #include <iostream>
    #include <fstream>
    using namespace std;
    
    int main()
    {
    
    ofstream ("help.txt", ios::trunc);
    a_file >> "wassup!";
    a_file.close();
    }
    
    system("pause");
    }
    and yes, i have the file help.txt in the same folder as the program. heres the compiling errors i get in bloodshed:

    /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
    -------------------------------

  5. #5
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    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 08:17 PM.

  6. #6
    Registered User
    Join Date
    Jul 2003
    Posts
    97
    hmm.....that file i/o tut must be wrong or sumthin. there for C++ right? thx so much, ill go try that

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,448
    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;
    }
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    Feb 2004
    Posts
    29
    i am using vc++
    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;
    }
    and errors:
    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
    any idea???
    br

  9. #9
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    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.

  10. #10
    Registered User
    Join Date
    Feb 2004
    Posts
    29
    1000x thanks

  11. #11
    Registered User
    Join Date
    Jul 2003
    Posts
    97
    it works now. thx alot

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Subtle(?) File I/O Problem
    By cecomp64 in forum C Programming
    Replies: 9
    Last Post: 07-16-2008, 11:39 AM
  2. Question about file I/O from a newbie
    By henrik in forum C Programming
    Replies: 4
    Last Post: 11-12-2007, 11:48 PM
  3. Inventory records
    By jsbeckton in forum C Programming
    Replies: 23
    Last Post: 06-28-2007, 04:14 AM
  4. File I/O Question
    By Achy in forum C Programming
    Replies: 2
    Last Post: 11-17-2005, 11:09 PM
  5. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 01:58 AM

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