Re: File Manipulation?

Discussion in 'C++' started by Öö Tiib, May 31, 2011.

  1. Öö Tiib

    Öö Tiib Guest

    On May 31, 3:20 am, A Moose in Love <>
    wrote:
    > I need to know in a simple way, how to create files, open them, write
    > to them, save them, and close them.  I've googled and have come up
    > with information that doesn't seem to work.  For example:
    > // basic file operations
    > #include <iostream>
    > #include <fstream>
    > using namespace std;
    >
    > int main () {
    >   ofstream myfile;
    >   myfile.open ("example.txt");
    >   myfile << "Writing this to a file.\n";
    >   myfile.close();
    >   return 0;
    >
    > }
    >
    > This does not work.  I cannot find a file after running this little
    > program called 'example.txt'.
    > I'm not a programmer; I just need to do this.


    Being not a programmer is OK, but do you at least know what operating
    system and C++ compiler you are using? With that information available
    people might be able to help you to find that "example.txt" of yours.
     
    Öö Tiib, May 31, 2011
    #1
    1. Advertising

  2. Öö Tiib

    Ian Collins Guest

    On 05/31/11 02:22 PM, A Moose in Love wrote:
    >> people might be able to help you to find that "example.txt" of yours.

    >
    > I looked for the file. I opened 'Control Panel' and searched. I
    > have Windows XP.


    The file will be in the directory where you ran the programme. If you
    don't know where that is, try asking on a group or forum dedicated to
    your IDE.

    --
    Ian Collins
     
    Ian Collins, May 31, 2011
    #2
    1. Advertising

  3. Öö Tiib

    John Smith Guest

    On 5/30/2011 7:33 PM, Ian Collins wrote:
    > On 05/31/11 02:22 PM, A Moose in Love wrote:
    >>> people might be able to help you to find that "example.txt" of yours.

    >>
    >> I looked for the file. I opened 'Control Panel' and searched. I
    >> have Windows XP.

    >
    > The file will be in the directory where you ran the programme. If you
    > don't know where that is, try asking on a group or forum dedicated to
    > your IDE.
    >


    1) Open up a command prompt (emulated dos prompt.)

    The command prompt should be in you accessories directory.

    2) Change to the root of the drive with:

    cd \

    3) Enter:

    dir filename.exe /s

    Make sure you replace "filename.exe" with the name of the file you are
    looking for. Let the above command complete, you should see text
    telling you where the file is, or that file was not found. If you are
    in the root, it has scanned the whole disk and that filename.exe dos not
    exist on the drive.

    Regards,
    JS
     
    John Smith, May 31, 2011
    #3
  4. Öö Tiib

    Geoff Guest

    On Mon, 30 May 2011 19:22:58 -0700 (PDT), A Moose in Love
    <> wrote:

    >I looked for the file. I opened 'Control Panel' and searched. I
    >have Windows XP. I use for a compiler, I use Bloodshed Dev - C++
    >4.9.9.2. Here is my source code for the program in question. (if
    >that helps?)
    >This is a program to analyze pace as pertains to horse racing.


    [snip code for brevity]

    Nowhere in your code does a file get opened or written.
     
    Geoff, May 31, 2011
    #4
  5. Öö Tiib

    Geoff Guest

    On Mon, 30 May 2011 19:22:58 -0700 (PDT), A Moose in Love
    <> wrote:

    >I looked for the file. I opened 'Control Panel' and searched. I
    >have Windows XP. I use for a compiler, I use Bloodshed Dev - C++
    >4.9.9.2. Here is my source code for the program in question. (if
    >that helps?)
    >This is a program to analyze pace as pertains to horse racing.


    You never write a file.
    Using your printData function as a model, create a writeData function
    replacing all your cout with myfile and call it from your
    lengthOfRaceFunction. Now you will need to write some kind of inteface
    for getting user input for specifying the file names.

    void writeData()
    {
    ofstream myfile;
    myfile.open ("example.txt");
    cout << "Writing data file." << endl;

    myfile.precision(4);
    //write << "\n\n*************************" << endl << endl;
    myfile << "Horse Number: " << horseNumber << endl;

    myfile << "TOTAL PACE: " << TP << endl;

    myfile << "\n____________________" << endl;
    myfile << "VELOCITY" << endl;
    //myfile << "\nfirstFraction: " << firstFraction << endl;
    //myfile << "secondFraction: " << secondFraction << endl;
    //myfile << "thirdFraction: " << thirdFraction << endl;
    myfile << "EP: " << EP << endl;
    myfile << "AP: " << AP << endl;
    myfile << "SP: " << SP << endl;
    myfile << "FW: " << FW << endl;
    //myfile << "FX: " << FX << endl;
    myfile.precision(3);
    //myfile << "ELDiff: " << ELDiff * 100 << endl;
    myfile << "____________________" << endl;

    myfile.precision(4);
    myfile << "\nENERGY" << endl;
    myfile << "\ntotal energy: " << energy << endl;
    //myfile << "firstFraction: " << firstFractionEnergy << endl;
    //myfile << "secondFraction: " << secondFractionEnergy << endl;
    //myfile << "thirdFraction: " << thirdFractionEnergy << endl;
    myfile << "EP: " << EPEnergy << endl;
    myfile << "AP: " << APEnergy << endl;
    myfile << "SP: " << SPEnergy << endl;
    myfile << "FW: " << FWEnergy << endl;
    //myfile << "FX: " << FXEnergy << endl;
    myfile.precision(3);
    //myfile << "ELDiff: " << ELDiffEnergy * 100 << endl;
    myfile << "____________________" << endl;

    myfile.precision(4);
    myfile << "\nSYNTHESIS" << endl;
    //myfile << "\ntotal energy: " << energy << endl;
    //myfile << "\nfirstFraction: " << firstFractionSynthesis <<
    endl;
    //myfile << "secondFraction: " << secondFractionSynthesis <<
    endl;
    //myfile << "thirdFraction: " << thirdFractionSynthesis << endl;
    myfile << "EP: " << EPSynthesis << endl;
    myfile << "AP: " << APSynthesis << endl;
    myfile << "SP: " << SPSynthesis << endl;
    myfile << "FW: " << FWSynthesis << endl;
    //myfile << "FX: " << FXSynthesis << endl;
    myfile.precision(3);
    //myfile << "ELDiff: " << ELDiffSynthesis * 100 << endl;
    myfile << "____________________" << endl;
    //myfile << "\n\n*************************" << endl << endl;

    myfile.close();
    }
     
    Geoff, May 31, 2011
    #5
  6. Öö Tiib

    Jorgen Grahn Guest

    On Tue, 2011-05-31, A Moose in Love wrote:
    ....
    > 4.9.9.2. Here is my source code for the program in question. (if
    > that helps?)
    > This is a program to analyze pace as pertains to horse racing.
    >
    > """

    ....

    >
    > void lengthOfRaceFunction()
    > {

    ....
    > if (lengthOfRace != 5 & lengthOfRace != 5.5 & lengthOfRace != 6 &
    > lengthOfRace != 6.5 & lengthOfRace !=7 &
    > lengthOfRace != 8 & lengthOfRace != 8.2 & lengthOfRace != 8.3 &
    > lengthOfRace != 8.5 & lengthOfRace != 9)


    I haven't read all of it (it was kind of hard to read), but two
    obvious bugs:

    - you can't reliably use != on doubles
    - don't confuse the '&&' and the '&' operators

    /Jorgen

    --
    // Jorgen Grahn <grahn@ Oo o. . .
    \X/ snipabacken.se> O o .
     
    Jorgen Grahn, May 31, 2011
    #6
  7. Öö Tiib

    Miles Bader Guest

    Jorgen Grahn <> writes:
    > - you can't reliably use != on doubles


    Of course one shouldn't do so blindly, but there are certainly cases
    where it makes sense to use != or == on doubles.

    E.g., comparison of doubles holding integral values that result from an
    explicit assignment should be fine (double d = 47; .... if (d != 47) ).
    This is very useful for detecting explicitly assigned out-of-band (for
    the algorithm) values.

    [I guess some fractional values (e.g. 0.5) should in practice also be OK
    since they're exactly representable in any sane radix (2, 10, 16).]

    -Miles

    --
    Consult, v.i. To seek another's disapproval of a course already decided on.
     
    Miles Bader, Jun 1, 2011
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. JezB

    Resource File Manipulation

    JezB, Feb 25, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    323
  2. Roedy Green

    Zip file manipulation

    Roedy Green, Aug 17, 2003, in forum: Java
    Replies:
    2
    Views:
    866
    Roedy Green
    Aug 17, 2003
  3. John Harrison

    Re: file overwrite data manipulation

    John Harrison, Jun 26, 2003, in forum: C++
    Replies:
    2
    Views:
    5,963
    John Harrison
    Jun 27, 2003
  4. Chris Theis
    Replies:
    0
    Views:
    812
    Chris Theis
    Jun 26, 2003
  5. Darren

    File Manipulation

    Darren, Oct 25, 2003, in forum: C++
    Replies:
    1
    Views:
    339
    Jon Bell
    Oct 25, 2003
Loading...

Share This Page