file ouput using fstream problem

This is a discussion on file ouput using fstream problem within the C++ Programming forums, part of the General Programming Boards category; I have a problem with outputing info to a file... my code is: while (int i = 0; i < ...

  1. #1
    Registered User
    Join Date
    Feb 2002
    Posts
    93

    Exclamation file ouput using fstream problem

    I have a problem with outputing info to a file... my code is:

    while (int i = 0; i < 5; i++)
    {
    outfile << "Blah" << endl;
    }

    it outputs this to a file


    blah
    blah
    blah
    blah
    blah
    <------- and it creates a blank line here....

    my question is how do i not create a blank line there..

  2. #2
    Registered User
    Join Date
    Jan 2002
    Posts
    381
    Have a very close look at your loop.
    The last iteration adds it in.
    Try something like this:

    outfile << "Blah";
    while (int i = 0; i < 4; i++)
    {
    outfile << endl << "Blah";
    }

    or

    while (int i = 0; i < 4; i++)
    {
    outfile << "Blah" << endl;
    }
    outfile << "Blah";

  3. #3
    Registered User
    Join Date
    Feb 2002
    Posts
    93

    oops

    oops i guess sending 1 piece of data to a file isn't a good example.. its easy to get around.. i'm actually passing 4 different variables to a file.. i can't think of a way to send data to a file without getting that last line... heres my real code


    void Tmain_form::write_to_file(int x)
    {
    file_name = saveas_dialog->FileName;

    ofstream file;

    x = 0;

    file.open(file_name.c_str());

    while ( x < title_list->Count )
    {
    file << title_list->Strings[x].c_str() << endl;
    file << type_list->Strings[x].c_str() << endl;
    file << location_list->Strings[x].c_str()<< endl;
    file << size_list->Strings[x].c_str()<< endl;

    x = x + 1;
    }

    file.close();
    }


    my resulting file would look like this

    title1
    type1
    location1
    size1
    title2
    type2
    location2
    size2
    <--------------blank line would be here...

    is it possible to change my code alittle to still get all the data to the file properly without having that last endline at the end..
    i need aline of code just outside the while loop that can delete the last line of my output file.. anyone know how to do this?

  4. #4
    Registered User
    Join Date
    Jan 2002
    Posts
    381
    Its pretty similar:

    x = 1;

    file.open(file_name.c_str());

    if (title_list->Count > 0)
    {
    file << title_list->Strings[x].c_str() << endl;
    file << type_list->Strings[x].c_str() << endl;
    file << location_list->Strings[x].c_str()<< endl;
    file << size_list->Strings[x].c_str();
    }

    while ( x < title_list->Count )
    {
    file << endl << title_list->Strings[x].c_str();
    file << endl << type_list->Strings[x].c_str();
    file << endl << location_list->Strings[x].c_str();
    file << endl << size_list->Strings[x].c_str();

    x = x + 1;
    }

    Its either that method or putting an if statment inside the loop.

  5. #5
    Registered User
    Join Date
    Feb 2002
    Posts
    93

    ooh la la

    oh yeah thats the ticket... i didn't think of doing that... thnx m8

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A development process
    By Noir in forum C Programming
    Replies: 37
    Last Post: 07-10-2011, 10:39 PM
  2. Newbie homework help
    By fossage in forum C Programming
    Replies: 3
    Last Post: 04-30-2009, 04:27 PM
  3. read from file problem
    By Martin Kovac in forum C Programming
    Replies: 1
    Last Post: 04-13-2009, 08:33 AM
  4. Subtle(?) File I/O Problem
    By cecomp64 in forum C Programming
    Replies: 9
    Last Post: 07-16-2008, 11:39 AM
  5. Problem with file and array
    By paok in forum C Programming
    Replies: 5
    Last Post: 05-01-2008, 04:19 AM

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