Connecting Tech Pros Worldwide Help | Site Map

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

 
LinkBack Thread Tools Search this Thread
  #1  
Old July 19th, 2005, 05:30 PM
Christopher Reeve
Guest
 
Posts: n/a
Default HELP! using filehandle.get("filename") writes to file when it reads!?

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;
}
================================================== ============

  #2  
Old July 19th, 2005, 05:30 PM
Kevin Goodsell
Guest
 
Posts: n/a
Default Re: HELP! using filehandle.get("filename") writes to file when itreads!?

Christopher Reeve wrote:
[color=blue]
> 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.[/color]

I don't understand what you said. But neither of those functions should
write to the file.
[color=blue]
> 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>[/color]

I suggest adding <string>
[color=blue]
>
> #define FSETTINGS "settings.dat"[/color]

It would be better if you used

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

I changed the case because it is no longer a macro.
[color=blue]
>
> using namespace std;
>
> int main () {
> char buffer[256];[/color]

I suggest making this

string buffer;
[color=blue]
> //fstream examplefile;
> fstream examplefile (FSETTINGS);
> //examplefile.seekg(9,ios::cur);
> examplefile.get (buffer,100);[/color]

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.
[color=blue]
> cout << buffer << endl;
>
> system ("PAUSE");
> return 0;
> }
> ================================================== ============[/color]

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.

 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,989 network members.