CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Mar 2007
    Posts
    238

    Writing to a file

    Hi There,
    I need some help in writing files using CFile .
    This is my requirement.

    Open the file.
    Write the header
    Write the content.
    WHen it reaches 10000 lines
    write the footer
    create a new file
    write the header

    and continue with the getting the data to write the body.

    CFile myfile;
    myfile.Open("file1.xml", mode:create|mode:write);
    myfile.write(the header)

    here there a for loop for getting the body of the file
    myfile.write(body);
    ncounter++;
    if(ncounter==10000)
    myfile.write(footer);
    myfile.close();
    CFile myfile2;
    myfile2.Open("file2.xml", mode:create|mode:write);
    myfile2.write(header);
    ncounter=0;
    }

    I should be able to close the myfile outside if it never reaches 10000 lines and also I should be able to close the myfile2 outside the loop if the 2nd or nth file doesn't reach the 10000 lines.

    I think I am missign some logic here as there should be a easy way to solve this. Any help is appreciated.
    Thanks
    T
    Last edited by tarunk; May 20th, 2011 at 03:57 AM.

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: Writing to a file

    Sorry, but I can't understand what exactly your problem(s) is(are).
    Can't you use for or while loop?
    Can't you use CFile::Close?

    PS: BTW, the CFile file is closed automatically on destruction, so if you put the CFile into the {...} block you shouldn't care about closing it after going out this block.
    Victor Nijegorodov

  3. #3
    Join Date
    Mar 2007
    Posts
    238

    Re: Writing to a file

    Thank you for the reply.

    CFile myfile;
    CFile myfile2; // for consecutive files
    myfile.Open("file1.xml", modecreate|modewrite);
    myfile.write(the header);

    a while loop to go through the directorystructure to list the files
    myfile.write(body); /// this should be myfile or myfile two accordingly
    ncounter++;
    if(ncounter==10000)
    myfile.write(footer);
    myfile.close();

    myfile2.Open("file2.xml", mode:create|mode:write);
    myfile2.write(header);
    ncounter=0;
    }
    myfile.close(); //myfile or myfile2 ......

    The problem is if the code does reaches the if condition myfile2 needs to be written and closed outside the if condition and if it doesn't myfile needs to be closed. How do I do that? There is a while loop created to get the body of the files anyway, so I don't have to worry about that.

  4. #4
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: Writing to a file

    Dear tarunk,
    after being here in CG since more than four years, since posting 236 posts... don't you know yet that Code tags (and proper indentations of course!) make the code snippets much more readable/understandable?
    Victor Nijegorodov

  5. #5
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: Writing to a file

    Your question isn't really about writing to a file, it's about your loop, which you didn't show. The best thing to do would be test in your loop if you're written 10000 lines, and if you have, close and reopen the same CFile object with the new name and keep going. Don't use two CFile objects, and just close the one you're using at the end of the loop.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured