Results 1 to 14 of 14

Thread: how to open files

  1. #1

    Thread Starter
    New Member
    Join Date
    Sep 2002
    Posts
    6

    Talking how to open files

    Hello,

    I'm trying to open a sound file in c++ and it just doesn't work!, if anyone knows the code for that then can they please send it.
    Also is the code for opening and playing music files (wav) different from the code to open exe files or zip files.

    Thanks Ilia

  2. #2
    Frenzied Member
    Join Date
    Jul 2002
    Posts
    1,370
    Code:
    i=PlaySound ("C:\\WINDOWS\\MEDIA\\TADA.WAV", 0L,SND_FILENAME | SND_ASYNC);
    be sure to include windows.h

  3. #3

    Thread Starter
    New Member
    Join Date
    Sep 2002
    Posts
    6
    Thanks for the code, but I'm new to c++ and get 4 error messages, saying undeclared indentifier.
    Would I need to declare them as variables, if so how

    Thanks
    Ilia

  4. #4
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Did you #include <windows.h> ?
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  5. #5
    Lively Member
    Join Date
    Sep 2002
    Posts
    78
    Your four errors are probably: i, PlaySound, SND_FILENAME, and SND_ASYNC. Here is the complete code that will do this:
    PHP Code:
    #include <iostream.h>
    #include <windows.h>

    int main()
    {
          
    bool i;
          
    i=PlaySound("C:\\WINDOWS\\MEDIA\\TADA.WAV"0L,SND_FILENAME SND_ASYNC);

    That should work. Be sure to set your project as a console app.

  6. #6
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Two things...

    1. iostreams are unnecessary in that program, since it doesn't produce any output.

    2. It's <iostream> not <iostream.h>, you've picked the old non-standard header.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  7. #7
    Lively Member
    Join Date
    Sep 2002
    Posts
    78
    Apologies...

  8. #8
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    No need
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  9. #9

    Thread Starter
    New Member
    Join Date
    Sep 2002
    Posts
    6
    The code works better but I still get this error message:

    --------------------Configuration: sound1 - Win32 Debug--------------------
    Compiling...
    sound1.cpp
    E:\C++\Day5\sound1\sound1.cpp(16) : error C2731: 'main' : function cannot be overloaded
    E:\C++\Day5\sound1\sound1.cpp(15) : see declaration of 'main'
    E:\C++\Day5\sound1\sound1.cpp(18) : warning C4129: 'W' : unrecognized character escape sequence
    E:\C++\Day5\sound1\sound1.cpp(18) : warning C4129: 'M' : unrecognized character escape sequence
    E:\C++\Day5\sound1\sound1.cpp(18) : warning C4129: 'T' : unrecognized character escape sequence
    E:\C++\Day5\sound1\sound1.cpp(18) : warning C4800: 'int' : forcing value to bool 'true' or 'false' (performance warning)
    E:\C++\Day5\sound1\sound1.cpp(19) : warning C4508: 'main' : function should return a value; 'void' return type assumed
    Error executing cl.exe.

    sound1.exe - 1 error(s), 5 warning(s)
    Last edited by ilia-c++; Sep 6th, 2002 at 03:14 PM.

  10. #10
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Can you post your entire code, please?
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  11. #11

    Thread Starter
    New Member
    Join Date
    Sep 2002
    Posts
    6
    // sound.cpp : Defines the entry point for the console application.
    //

    #include "stdafx.h"


    int main(int argc, char* argv[])
    {
    return 0;
    }

    #include <windows.h>

    int main()
    {
    bool i;
    i=PlaySound("C:\WINDOWS\MEDIA\TADA.WAV", 0L,SND_FILENAME | SND_ASYNC);
    }

  12. #12
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    You're not allowed two versions of main(). Use just the second half (from #include <windows.h>).
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  13. #13
    Frenzied Member
    Join Date
    Aug 2000
    Location
    Birmingham, AL
    Posts
    1,276
    You also need to use double backslashes to avoid the character escape sequence errors.

    ...and 0L is redundant.

    Code:
    #include <windows.h>
    
    void main(){
    
        bool i;
        i = PlaySound("C:\\WINDOWS\\MEDIA\\TADA.WAV",  0, SND_FILENAME | SND_ASYNC);
    
    }

  14. #14
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Also, PlaySound returns a BOOL which is just a synonym for int and not the same as bool.
    And in this case, you can simply forget the return value of PlaySound.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width