Thread: Writing to files

  1. #1
    Registered User
    Join Date
    May 2005
    Location
    Toronto, Canada
    Posts
    257

    Writing to files

    I've read the C and C++ tutorials on working with files and I know how to work with binary files.
    Are FILE pointers the best way to go about outputing to a file? I don't quet understand how to do it in C++.

    The tutorial I've read and didn't understand is FAQ > How do I... (Level 2) > Work with files (C++).
    Thanks.

  2. #2
    Registered User mitakeet's Avatar
    Join Date
    Jun 2005
    Location
    Maryland, USA
    Posts
    212
    What do you want to accomplish? At least for MS C++, I believe the C++ streams are just wrappers for the underlying C calls, so it is potentially a bit faster to use C, but performance should be the last consideration for anyone getting started. IO is one of the biggest differences between C and C++, pick one and go with it or plan on a 2x learning curve.

    Free code: http://sol-biotech.com/code/.

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw

  3. #3
    Registered User
    Join Date
    May 2005
    Location
    Toronto, Canada
    Posts
    257
    I want to open a file read in some data, I know what exactly, process it, and output to another file.
    I have to work with C++, because I'm making a GUI.

  4. #4
    Registered User mitakeet's Avatar
    Join Date
    Jun 2005
    Location
    Maryland, USA
    Posts
    212
    Actually, you don't need to work with C++ with a GUI. Even if you are using, say, MFC, you can still use C IO all you like (there is even a standard C C++ header for C's <stdio.h>, it is <cstdio>). You are still not being very helpful on what you want to do, why not post some code?

    Free code: http://sol-biotech.com/code/.

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw

  5. #5
    Registered User
    Join Date
    May 2005
    Location
    Toronto, Canada
    Posts
    257
    I really can't figure out what they are trying to do in this bit of code. It's from one of the tutorials, but it won't compile on my comp.
    Code:
    #include <iostream> 
    #include <fstream> 
    #include <cstdlib> 
    
    int main ( int argc, char *argv[] )
    {
      if ( argc != 3 ) {
        std::cerr<<"Usage: %s <readfile1> <writefile2>\n";
        std::exit ( EXIT_FAILURE );
      }
    
      std::ifstream in ( argv[1] );
    
      if ( !in.is_open() ) {
        std::cerr<<"Error opening input file\n";
        std::exit ( EXIT_FAILURE );
      }
    
      std::ofstream out ( argv[2] );
    
      if ( !out.is_open() ) {
        std::cerr<<"Error opening output file\n";
        std::exit ( EXIT_FAILURE );
      }
    
      char ch;
      while ( in.get ( ch ) )
        out.put ( ch );
    }

  6. #6
    Registered User mitakeet's Avatar
    Join Date
    Jun 2005
    Location
    Maryland, USA
    Posts
    212
    What OS/compiler? What errors? It looks like perfectly good code to me.

    Free code: http://sol-biotech.com/code/.

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw

  7. #7
    Registered User
    Join Date
    May 2005
    Location
    Toronto, Canada
    Posts
    257
    I'm using MS Visual C++ on Windows 2000.
    The error I get is that <exit> is not an std object. I tried a using namespace line at the begining of the program and getting rid of all the std:: , that compiled but what is this code supposed to do?

    can I use good old fprintf and fwrite in C++ without consiquences?

  8. #8
    Registered User mitakeet's Avatar
    Join Date
    Jun 2005
    Location
    Maryland, USA
    Posts
    212
    You can use C or C++ IO in C++ programs, though you should pick one and stick with it, at least for the entirety of the program.

    The program is basically a version of copy; it takes a source file and a destination file and copies the source to the destination byte by byte. Not very efficient, but if the file size is not large, that is irrelevant.

    Free code: http://sol-biotech.com/code/.

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw

  9. #9
    Registered User
    Join Date
    May 2005
    Location
    Toronto, Canada
    Posts
    257
    Quote Originally Posted by mitakeet
    a destination file and copies the source to the destination byte by byte. Not very efficient, but if the file size is not large, that is irrelevant.
    Is there an easier way to copy the whole file into memory?

    Say I want to read a file into a huge string into memory, extract some pieces, modify otehrs and then write it to another file. Is it easy to navigate through a string this way?
    I hope this is making sense.

  10. #10
    Registered User mitakeet's Avatar
    Join Date
    Jun 2005
    Location
    Maryland, USA
    Posts
    212
    That program is NOT copying the file into memory, it is copying it from one disk location to another with (nominally) only a single byte in memory at any given instant. You can read the file into memory (presuming you have enough memory, check out memory mapped files if you don't), but if you (as the programmer, files are just strings of bits) have structure in the file, it makes better sense to read in the file into some sort of equivelent structure, modify the structure, then write it back out to file. There is nearly an infinite number of ways to do this, so you will need to supply a lot of detail for anything concrete. Why not just take a stab at writing your own program and then post back here with any problems?

    Free code: http://sol-biotech.com/code/.

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw

  11. #11
    Registered User
    Join Date
    May 2005
    Location
    Toronto, Canada
    Posts
    257
    I'll have to do some heavy thinking on how to organise the memory structure. Then I'll write a hopefully simple code of my own to sort out all the input gibrish.
    Thanks for the consultation,

    A.

  12. #12
    Registered User mrafcho001's Avatar
    Join Date
    Jan 2005
    Posts
    483
    did you forget a return 0; at the end.. lol

  13. #13
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> did you forget a return 0; at the end.. lol

    The return 0 at the end of the main function is not required. The main function is special in that it will implicitly return 0 if no return value is specified. Many people just don't put the return if they are not using it, and allow the implicit return 0 to work.

    In VC++ 6.0, it will give you a warning, not an error, but that's because VC++ 6.0 is old and not very standards compliant. To fix the error on the sample code, just remove the std:: from everything and add using namespace std; under the #includes. This is not best practice, but because the compiler is out of date it doesn't recognize that exit should be part of std.
    Last edited by Daved; 06-21-2005 at 10:28 AM.

  14. #14
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    From memory there was an error in MSVC6 whereby the functions in cstdlib were not included in namespace std. Its an easy fix just wrap the contents of the header in a namespace std block.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with writing to files
    By beanroaster in forum C++ Programming
    Replies: 10
    Last Post: 12-23-2007, 12:21 AM
  2. Reading & Writing files (Error)
    By Blackroot in forum C++ Programming
    Replies: 9
    Last Post: 01-10-2006, 11:55 AM
  3. Writing files to a CD
    By SyntaxBubble in forum Windows Programming
    Replies: 1
    Last Post: 04-16-2003, 04:43 PM
  4. *.COM Files? Writing them?
    By johnc in forum A Brief History of Cprogramming.com
    Replies: 13
    Last Post: 07-11-2002, 01:52 AM
  5. Making files, opening them, and writing to them
    By Unregistered in forum Game Programming
    Replies: 6
    Last Post: 06-18-2002, 09:57 PM