Please can someone help me

This is a discussion on Please can someone help me within the C++ Programming forums, part of the General Programming Boards category; i made a really basic program to make a text file then transfer it to a dat file and in ...

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    27

    Please can someone help me

    i made a really basic program to make a text file then transfer it to a dat file and in tried to make it read the program back to me when i run the program here is the code

    Code:
    #include<iostream>
    #include <windows.h>
    
    using namespace std;
    
    int main()
    {
        system("rename thing.txt thing.dat");
        system ("\"C:\\WINDOWS\\Desktop\\thing.dat\"")
    ;}
    thanks

    EDIT
    i use dev-C++

  2. #2
    Registered User
    Join Date
    May 2004
    Posts
    73
    Usually, when you open a .dat file, a little message will pop up, saying "you are about to open a .dat file. you sure?" or something like that.

    Instead of opening it with the system command, try opening it with an ifstream.

  3. #3
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    You're also using double quotes inside a string- you'll need to escape them so that the compiler interprets those as double quotes and not the end or beginnings of strings. If you want to use double quotes inside string, precede the quotes with a backslash: \"

    You specify that main returns an int, but don't return anything. I believe Dev-C++ will take care of that for you, but it's very bad practice - I'd advise against that.

    In future, you might also consider adding more information to your post, like what exactly is going wrong with your program, what you expected it to do, and any error message you're recieving. A more descript subject line also wouldn't hurt.

  4. #4
    Registered User
    Join Date
    Apr 2005
    Posts
    27
    i tried it and it didnt do what i want(the ifstream).it compiles and runs but it dosnt do any thing it just goes up and dose nothing.
    i want it to become part of the program...like being able to put a program in the batch or text or any type of file then load it into the program that i made...do u get what i mean
    Last edited by aaroroge; 04-30-2005 at 11:17 AM.
    compiler:dev-C++
    asr

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,592
    Probably because your OS knows nothing about what to do with .dat files.

    Open up a console, and type in
    C:\WINDOWS\Desktop\thing.dat

    It's basically the same as typing
    notepad.exe

    But the OS knows what to do with a .exe file (run it). It's utterly clueless as to what to do with a .dat file, and will probably give you some error message.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    Registered User
    Join Date
    Apr 2005
    Posts
    27
    i tried making it an exe and it still wouldnt run it would do nothing it compiles all right but dosnt do anything when you run ill post what ive done so far please help me

    Code:
    #include<iostream>
    #include <windows.h>
    #include<fstream>
    
    using namespace std;
    
    int main()
    {
        //Create and open an input stream
    	ifstream in_stream;
    	in_stream.open("test1.exe", ios::in);
    	//waits for user input
    	cin.get();
    ;}
    and here is test1.exe

    Code:
    #include<iostream>
    
    using namespace std;
    
    int main()
    {
      cout<<"hi";
      cin.get();
    }
    thanks
    compiler:dev-C++
    asr

  7. #7
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,592
    Lemme get this straight, you want your first program to run another program called test.exe ?

    So do you want to see "hi" on the screen, or are you trying to run test.exe in such a way that your other program can read "hi" into some variable or other?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  8. #8
    Registered User
    Join Date
    Apr 2005
    Posts
    27
    yes yes
    compiler:dev-C++
    asr

  9. #9
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Quote Originally Posted by sean_mackrory
    You specify that main returns an int, but don't return anything. I believe Dev-C++ will take care of that for you, but it's very bad practice - I'd advise against that.
    Actually, the automatic success return from main is specified in the C++ standard, perfectly valid and not at all bad practice if it's only the default path that makes use of it.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  10. #10
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,592
    > yes yes
    I guess the simplest way of doing this would be
    Code:
    #include<iostream>
    #include <windows.h>
    #include<fstream>
    
    using namespace std;
    
    int main()
    {
        //Create and open an input stream
        system( "test1.exe > test1-results.txt" );
        ifstream in_stream;
        in_stream.open("test1-results.txt", ios::in);
        //waits for user input
        cin.get();
    }
    But you should remove the "wait for a key" at the end of your test1.exe program to make it run smoothly.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21