Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

NewbJ

File Streams

This topic is 5194 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I''m having a problem with out file streams in a program I''m writing, specifically that when I try to create a new file to write to, it isn''t created. I used the bad() and fail() functions to see if there was some sort of error, and sure enough, there was. I tried lots of different things, including changing the extension on the file being created, using different ios:: flags, and using plain fstream rather than ofstream, but none of them seem to help. Here''s the code:
#include <fstream>
#include <iostream>

#include "CPU.h"

using namespace std;

void CPU::doMethod1()
{
  char character;

  ifstream inFile; //file to read from

  inFile.open (filePath);

  ofstream outFile; //file to output to

  outFile.open ("output.dat"); //creating a new file


  while ( inFile.get(character) )
  {
    cout << character;
    outFile << character;
    outFile << '' '';
  }

  inFile.close();
  outFile.close();
} 
As you can probably tell, I''m just copying the contents of one file to another, character-by-character. The other file, inFile, opens fine (filePath is a character array in the class CPU). Thanks!

Share this post


Link to post
Share on other sites
Advertisement
I don''t see the error in creating the output file... However, here''s a cleaner way to copy the contents of a file:
#include <iostream>
#include <iterator>
 
int main()
{
ifstream fin("file.in");
ofstream fout("file.out");
istream_iterator<char> isi(fin);
ostream_iterator<char> osi(fout, "");
copy(isi.begin(), isi.end(), osi);
return 0;
}

Share this post


Link to post
Share on other sites
Hmm...well, let me try again.

Ok, it sort of works, but not really. I guess my concern, more specifically, was...I'm telling it to create the file, and I can get the contents of the file (now), but when I look for it in the Windows file browser in the directory of the project, it's not there. So, where are files created, by default? I thought it was in the project directory, but apparently not.

EDIT: I did the obvious, and used the Windows search function to find the file, and it was in the Dev-C++ directory. Is there any way to make it automatically go to the project directory rather than the compiler directory? (besides putting the project directory path directly in)

[edited by - NewbJ on March 6, 2004 8:32:39 PM]

[edited by - NewbJ on March 6, 2004 8:33:56 PM]

Share this post


Link to post
Share on other sites
quote:
Original post by NewbJ
So, where are files created, by default? I thought it was in the project directory, but apparently not.
Not the project directory. The directory of the binary. By default, that is.

quote:
EDIT: I did the obvious, and used the Windows search function to find the file, and it was in the Dev-C++ directory. Is there any way to make it automatically go to the project directory rather than the compiler directory?
First off, that''s some very weird behavior (another reason for me not to like Dev-C++ - Yay!) There might be some options that set the application working directory to the location of the binary. Search the IDE documentation.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!