Thread: File IO - Tutorial is wrong?

  1. #1
    Registered User
    Join Date
    May 2003
    Posts
    6

    File IO - Tutorial is wrong?

    I was checking through the tutorials (again), and I got to lesson #10 where it says:

    Code:
    #include <fstream.h>
    #include <iostream.h>
    int main()
    {
      char str[10];  
             //Used later
      ofstream a_file("example.txt");
    //Creates an instance of ofstream, and opens example.txt
      a_file<<"This text will now be inside of example.txt"; 
    //Outputs to example.txt through a_file
      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! 
    }
    The thing is that this doesn't create a file called "example.txt" anywhere...
    Is it just my comp, or what?

  2. #2
    Registered User
    Join Date
    May 2003
    Posts
    12
    It worked for me. The file is in the same folder as the
    executable. If you want the file elsewhere put in the full path
    in the code.

  3. #3
    Registered User
    Join Date
    May 2003
    Posts
    6
    Aargh! It doesn't seem to work...

    I am using Dev-C++ on a WinXP comp.

    ---------------------------------------------
    Wait, so if I wanted to create a file inside another folder, I use:
    Code:
    ofstream some_file("C:\damn\foo\bar.txt");
    Last edited by NetPsycho; 06-01-2003 at 10:58 AM.

  4. #4
    Registered User
    Join Date
    May 2003
    Posts
    6
    I used this code:
    Code:
    #include <fstream.h>
    #include <iostream.h>
    #include <string.h>
    #include <errno.h>
    #include <stdlib.h>
    
    int main()
    {
      char str[10]; 
      //Used later
      ofstream a_file("example.txt");
      if ( !a_file ) {
        cerr << "oops " << strerror(errno) << endl;
        system("pause");
        return 1;
      }
      else
      {
       cout << "Foo" << endl;
       system("pause");
       return 0;
      }
    }
    And it output: Foo

    Ok, I forgot the escapes, but I was not really being very specific.
    Last edited by NetPsycho; 06-01-2003 at 01:01 PM.

  5. #5
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    instead of:
    Code:
    ofstream a_file("example.txt");
    try:
    Code:
    ofstream a_file;
    a_file.open("example.txt",ios::trunc);
    ...
    a_file.close();
    it shouldn't make any difference, but maybe your compiler is playing tricks with your head... maybe closing the file will help...
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  6. #6
    Registered User
    Join Date
    Nov 2002
    Posts
    126
    I doubt this is the problem, but with your compiler it might be necessary for you to explicitly enable output to the file by setting the ios::out bit when opening the file. Try this code:

    Code:
    #include <iostream>
    #include <fstream>
    
    using namespace std;
    
    int main(int argc, char** argv) {
    
    	ofstream MyFile;
    
    	//Open file
    	MyFile.open("example.txt", ostream::out);
    
    	if(MyFile.fail()) {
    		cerr <<"Sorry, I can't open that file for output\n";
    		return 0;
    	}
    
    	//Write to file
    	MyFile <<"This should be inside of example.txt" <<endl;
    
    	//Close file
    	MyFile.close();
    }
    That should create the file and write to it. If it doesn't, it should say it couldn't open the file. Hope that helps

  7. #7
    Registered User
    Join Date
    Feb 2003
    Posts
    596
    You will probably find example.txt in your Dev-C++ directory ... i.e. C:\program files\Dev-C++

    To specify a complete path type it this way:

    ofstream a_file("c:/cfiles/scrap/example.txt"); // note - forward slashes

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie homework help
    By fossage in forum C Programming
    Replies: 3
    Last Post: 04-30-2009, 04:27 PM
  2. file io
    By Abda92 in forum C Programming
    Replies: 17
    Last Post: 09-18-2006, 11:23 PM
  3. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 10:46 AM
  4. File IO with .Net SDK and platform SDK
    By AtomRiot in forum Windows Programming
    Replies: 5
    Last Post: 12-14-2004, 10:18 AM
  5. archive format
    By Nor in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 08-05-2003, 07:01 PM