Menu

ifstream, ofstream ...

2006-05-28
2012-09-26
  • Nobody/Anonymous

    Here is my code:

    include <cstdlib>

    include <iostream>

    include <fstream> // not fstream.h because it said depricated header..

    using namespace std;

    int main(int argc, char *argv[])
    {
    ofstream output("data.dat");
    output << "omg it worked" << endl;
    output.close();

    system("PAUSE");
    return 0;
    }

    Now from my c++ class, ofsteam should create the file if it does now exist, and it should be in the same directory as the program that is calling it. I compiled this without any errors and ran in, but nothing happened, no file created, no nothing. I decided maybe I needed to help it out a little bit and I created data.dat for it, same thing, nothing happened. I tried to do it with data.txt and still no results. My version is 4.9.9.2

     
    • Wayne Keen

      Wayne Keen - 2006-05-28

      Your code ran, created the file in c:\mycstuff, and it has what is expected there. I ran it with Dev 4.9.9.2, on XP Pro.

      I know this is a runtime concern, but could you please post your full compile log?

      Also, did you do a Windows Search for your file?

      Wayne

       
    • Nobody/Anonymous

      Hmm, I am calling it with command line perameters. It dosent seem to work when I do that, but when I run it normally it works. I dont see that there should a be a differecnce.. I also want to create a dll that can do this. I wrote the dll with the same code as the application that has the code I posted. Application works without command line perameters and dll just dosent work period.

       
      • Wayne Keen

        Wayne Keen - 2006-05-28

        "Hmm, I am calling it with command line perameters."

        How exactly are you doing that? (Remember the best way to get help is to provide information that enables others to duplicate what you are seeing)

        Your compile log I asked for?

        Wayne

         
    • Nobody/Anonymous

      What's your CWD when running the program?
      Have you actually searched the HD for the corresponding files?
      You may want to consider using absolute rather than relative paths, to ensure that your data actually ends up in a known place!

       
    • Nobody/Anonymous

      Here is my code for the .exe, normally I call it with the name, then the value 1 then another number..

      include <cstdlib>

      include <iostream>

      include <windows.h>

      define DLL "CopyMem.dll"

      define GAME "notepad"

      include <fstream>

      using namespace std;

      void Inject(DWORD pId, char* strDll );

      HANDLE hProcess;
      DWORD pId;

      int main(int argc, char *argv[])
      {
      ofstream output("data.dat");
      output << "Zomg it worked" << endl;
      cout<<"Zomg?" << endl;
      output.close();

      double argument = atof(argv[1]);
      double argument2 = atof(argv[2]);

      pId = argument2;

      if((argument == 1) && (argument2 != 0)){
      Inject(pId, DLL);
      cout<< "DLL injected :) ...or attempted at least "<<endl;
      }

      It works ok when I run it, but when i call it from a dos prompt, somthing like c:\injector 1 1234, it runs everything fine, except no file i/o. It clearly runs the code because when i put cout's and messagebox's through the code i want run, they appear...

      Compile Log:
      Compiler: Default compiler
      Building Makefile: "C:\Dev-Cpp\Final\Makefile.win"
      Executing make...
      make.exe -f "C:\Dev-Cpp\Final\Makefile.win" all
      make.exe: Nothing to be done for `all'.

      Execution terminated
      Compilation successful

      Now for the dll:

      Note that i use dll main to run my code as soon as the dll enters the address space of the program:

      / Replace "dll.h" with the name of your header /

      include "dll.h"

      include <windows.h>

      include <fstream>

      include <cstdlib>

      include <iostream>

      using namespace std;

      DllClass::DllClass()
      {

      }

      DllClass::~DllClass ()
      {

      }

      ifdef __cplusplus

      extern "C"

      endif

      BOOL APIENTRY DllMain(HANDLE hModule, DWORD reason, LPVOID lpReserved)
      {
      MessageBox(NULL, "Works", ":)", NULL);
      ofstream outfile("out.txt");
      outfile << "Test" << endl;
      outfile.close();

      }

      and compile log:

      Compiler: Default compiler
      Building Makefile: "C:\Dev-Cpp\Final\Makefile.win"
      Executing make...
      make.exe -f "C:\Dev-Cpp\Final\Makefile.win" all
      g++.exe -c dllmain.cpp -o dllmain.o -I"C:/Dev-Cpp/lib/gcc/mingw32/3.4.2/include" -I"C:/Dev-Cpp/include/c++/3.4.2/backward" -I"C:/Dev-Cpp/include/c++/3.4.2/mingw32" -I"C:/Dev-Cpp/include/c++/3.4.2" -I"C:/Dev-Cpp/include" -DBUILDING_DLL=1

      dllmain.cpp: In function BOOL DllMain(void*, DWORD, void*)': dllmain.cpp:30: warning: passing NULL used for non-pointer converting 4 ofint MessageBoxA(HWND__, const CHAR, const CHAR*, UINT)'

      dllwrap.exe --output-def libCopyMem.def --driver-name c++ --implib libCopyMem.a dllmain.o -L"C:/Dev-Cpp/lib" --no-export-all-symbols --add-stdcall-alias -o CopyMem.dll

      dllwrap.exe: no export definition file provided.
      Creating one, but that may not be what you want

      Execution terminated
      Compilation successful

       
    • Nobody/Anonymous

      Yes, I found the files that i wanted to generate by command line argument in a folder I did not expect. I used outfile.fail() and found that there is a problem in openeing a file with code from a .dll. Any ideas?

       
    • Anonymous

      Anonymous - 2006-05-28

      >> and it should be in the same directory as the program that is calling it.

      Not exactly, it will be in the current working directory. If for example your program is c:\project\myprog.exe, and from the console in another folder you enter this:

      c:\anotherfolder> c:\project\myprog.exe

      the file will be created in c:\anotherfolder.

      Have you tried searching your hard drive for data.dat?

      Have you tried checking the output streams status for errors? (use ofstream::is_open() or ofstream::fail() for example).

      If you want to check what your programs working directory is put the following function in the code:

      system( "cd" ) ;

      Clifford

      Clifford

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.