HELP! using filehandle.get("filename") writes to file when it reads!?

Discussion in 'C++' started by Christopher Reeve, Sep 14, 2003.

  1. Hi, I wonder if anyone could help me. I am moving accross from C to
    C++ and am writing a program that reads and writes data into a file. I
    am aware of the old C ways of doing it but am trying to use the C++
    functions which don't seem to be working propperly.

    I need to open the file for reading and writing because I want to
    search for a possition in a file called settings.dat and then write a
    number after a certain chatacter. However when I use
    examplefile.get(buffer,100); or examplefile.getline(buffer,100), in
    addition to reading to the file it also modifies it by inserting the
    first three characters of what ot read back into the place it started
    reading from. Does anyone know why this might be happening or how I
    can get arround this? I am using Dev-C++.


    I'll give this test program as an example, it requires any old file
    called settings.dat

    ================================================
    #include <iostream>
    #include <fstream>




    #define FSETTINGS "settings.dat"

    using namespace std;

    int main () {
    char buffer[256];
    //fstream examplefile;
    fstream examplefile (FSETTINGS);
    //examplefile.seekg(9,ios::cur);
    examplefile.get (buffer,100);
    cout << buffer << endl;





    system ("PAUSE");
    return 0;
    }
    ==============================================================
     
    Christopher Reeve, Sep 14, 2003
    #1
    1. Advertising

  2. Re: HELP! using filehandle.get("filename") writes to file when itreads!?

    Christopher Reeve wrote:

    > Hi, I wonder if anyone could help me. I am moving accross from C to
    > C++ and am writing a program that reads and writes data into a file. I
    > am aware of the old C ways of doing it but am trying to use the C++
    > functions which don't seem to be working propperly.
    >
    > I need to open the file for reading and writing because I want to
    > search for a possition in a file called settings.dat and then write a
    > number after a certain chatacter. However when I use
    > examplefile.get(buffer,100); or examplefile.getline(buffer,100), in
    > addition to reading to the file it also modifies it by inserting the
    > first three characters of what ot read back into the place it started
    > reading from.


    I don't understand what you said. But neither of those functions should
    write to the file.

    > Does anyone know why this might be happening or how I
    > can get arround this? I am using Dev-C++.
    >
    >
    > I'll give this test program as an example, it requires any old file
    > called settings.dat
    >
    > ================================================
    > #include <iostream>
    > #include <fstream>


    I suggest adding <string>

    >
    > #define FSETTINGS "settings.dat"


    It would be better if you used

    const char *const fsettings = "settings.dat";

    I changed the case because it is no longer a macro.

    >
    > using namespace std;
    >
    > int main () {
    > char buffer[256];


    I suggest making this

    string buffer;

    > //fstream examplefile;
    > fstream examplefile (FSETTINGS);
    > //examplefile.seekg(9,ios::cur);
    > examplefile.get (buffer,100);


    I suggest using

    getline(examplefile, buffer);

    Where buffer is a string. Note that this is not exactly the same,
    because it extracts the newline character from the stream (and discards
    it) rather than leaving it there.

    > cout << buffer << endl;
    >
    > system ("PAUSE");
    > return 0;
    > }
    > ==============================================================


    I can't see an obvious reason for any serious problems. Your code worked
    for me. You might need to give us more information, and the contents of
    the input file you used for testing.

    -Kevin
    --
    My email address is valid, but changes periodically.
    To contact me please use the address from a recent posting.
     
    Kevin Goodsell, Sep 14, 2003
    #2
    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. =?Utf-8?B?Sm9l?=

    Extract filename from a filename typed by user

    =?Utf-8?B?Sm9l?=, Aug 23, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    1,069
    Travis Murray
    Aug 24, 2004
  2. Replies:
    1
    Views:
    1,607
    Roland de Ruiter
    Jun 15, 2006
  3. Terry Carroll
    Replies:
    10
    Views:
    650
    Fuzzyman
    Jan 23, 2006
  4. davidcalvin

    Multiple reads and writes with popen?

    davidcalvin, Jul 9, 2008, in forum: C Programming
    Replies:
    0
    Views:
    1,151
    davidcalvin
    Jul 9, 2008
  5. Stefano Crocco
    Replies:
    0
    Views:
    158
    Stefano Crocco
    Nov 14, 2008
Loading...

Share This Page