opening files

This is a discussion on opening files within the C++ Programming forums, part of the General Programming Boards category; is there any other way to open a file for append output other than: Code: ofstream afile("filename",ios::app); ??...

  1. #1
    Registered User
    Join Date
    Jul 2005
    Posts
    17

    opening files

    is there any other way to open a file for append output other than:


    Code:
    ofstream afile("filename",ios::app);
    ??

  2. #2
    Banned
    Join Date
    Jun 2005
    Posts
    594
    CreateFile() and FILE

    im sure there are some other way, if you could be more specifc in
    what you where trying to accomplish youd get more info.

  3. #3
    Registered User
    Join Date
    Jul 2005
    Posts
    17
    opening a file in append mode so I can write info into it, only reason i m asking is because ios:app is hard to remember sometimes.

    just other choices for opening a file that might be easier to remember.

    as well, later on i want to say.. write to the file the using information from a class.

    as well input from teh file will automatically place into the class' variable
    Last edited by hoangvo; 07-23-2005 at 02:34 AM.

  4. #4
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    well, you could do this:
    Code:
    #include<iostream>
    #include<fstream>
    
    int main()
    {
            std::fstream file("test.in",std::ios::out|std::ios::app);
            file<<"\nHello World";
            file.close();
            return 0;
    }
    it's essentially the same thing.

    my point: you're going to have to memorize things to learn programming. ios::app is one of the easier things to remember. just remember that you're appending things, not truncating the file.

    to demonstrate my point, here's one way of reading and writing data from a struct to a file:
    Code:
    #include<iostream>
    #include<fstream>
    
    int main()
    {
    
            struct myStruct
            {
                    int a;
                    int b;
                    char c;
            } test;
    
            test.a=50;
            test.b=60;
            test.c='A';
    
            /* open a binary file for reading and writing by truncating */
            std::fstream file("test.in",std::ios::binary|std::ios::in|std::ios::out|std::ios::trunc);
    
            /* write to the file */
            file.write(reinterpret_cast<char*>(&test),sizeof(myStruct));
    
            /* then to read from it, starting at the beginning */
            file.seekg(0,std::ios::beg);
            file.read(reinterpret_cast<char*>(&test),sizeof(test));
    
            /* close the file */
            file.close();
    
            /* output what you just read */
            std::cout<<"int a: "<<test.a<<"\nint b: "<<test.b<<"\nchar c: "<<test.c<<std::endl;
            return 0;
    }
    Last edited by major_small; 07-23-2005 at 03:11 AM.
    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

  5. #5
    Registered User
    Join Date
    Jul 2005
    Posts
    17
    Thank you,

    I got it to work via binary method.

    its a bit to remember but as i constantly type it i will eventually remember.

    its abit confusing as well cause of reinterpret_cast<char*>(&variable)

    can u explain that part?

    i know that read accepts an array of char
    how does it re-cast into a char array from class/struct type?

  6. #6
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    it basically turns the struct into an array of bytes and writes those bytes to the file as characters.
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Opening ASCII files in C?
    By Mavix in forum C Programming
    Replies: 6
    Last Post: 04-25-2007, 03:23 PM
  2. Need help opening a series of files
    By ramparts in forum C Programming
    Replies: 9
    Last Post: 11-14-2006, 05:49 PM
  3. Opening files with UNICODE file names
    By decohk in forum Linux Programming
    Replies: 2
    Last Post: 11-09-2006, 05:25 AM
  4. opening files
    By angelic79 in forum C Programming
    Replies: 3
    Last Post: 10-19-2004, 07:52 AM
  5. Opening files - Giving options?
    By wwwGazUKcom in forum C++ Programming
    Replies: 3
    Last Post: 09-18-2001, 08:06 AM

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