reading files

This is a discussion on reading files within the C++ Programming forums, part of the General Programming Boards category; i want to open a wav file but it does not work this way. why? #include<fstream.h> void main() { unsigned ...

  1. #1
    Registered User
    Join Date
    Aug 2001
    Posts
    89

    reading files

    i want to open a wav file but it does not work this way. why?

    #include<fstream.h>

    void main()
    {
    unsigned long x;
    ifstream file1("sourcepathstring");
    ofstream file2("targetpathstring");

    while(!file1.eof())
    {
    file1>>x;
    cout<<x<<endl;
    file2<<x<<endl;
    }
    file1.close();
    file2.close();
    }

    it is not working this way? why?
    if i have char instead of unsigned long it runs when i convert from
    char to ASCII number, but this way i am loosing data, also i want
    to know WHY it is not working.
    thanks
    behold the faithless one and his angel

  2. #2
    Registered User
    Join Date
    Aug 2001
    Posts
    89
    sorry, this is the correct prog.

    #include<fstream.h>

    void main()
    {
    unsigned long x;
    ifstream file1("sourcepathstring",ios::binary);
    ofstream file2("targetpathstring",ios::binary);

    while(!file1.eof())
    {
    file1>>x;
    cout<<x<<endl;
    file2<<x<<endl;
    }
    file1.close();
    file2.close();
    }
    behold the faithless one and his angel

  3. #3
    Registered User
    Join Date
    Aug 2001
    Posts
    89
    sorry again
    when i say it is not working i mean that i have only "0" in the
    screen.
    behold the faithless one and his angel

  4. #4
    zen
    zen is offline
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    If you want to read and write binary files you should use the read and write member functions -

    Code:
    #include <fstream>
    #include <iostream>
    
    using namespace std; 
    
    int main() 
    { 
        unsigned long x; 
        ifstream file1("sourcepathstring",ios::binary);
        ofstream file2("targetpathstring",ios::binary); 
    
        while(!file1.eof()) 
        { 
            file1.read((char*)&x,sizeof(x));
            cout << x<<endl; 
            file2.write((char*)&x,sizeof(x)); 
        } 
    
    
        file1.close(); 
        file2.close(); 
    
    return 0;
    } 

  5. #5
    Registered User
    Join Date
    Aug 2001
    Posts
    89
    Thanks a lot.
    it worked, i did not use the std but it worked
    can you explain to me why the pointer to a char is needed
    in the read and write member function?
    x is a number, not a character...shouldn't it be
    file1.read((unsigned long*)&x,sizeof(x));
    ??
    behold the faithless one and his angel

  6. #6
    zen
    zen is offline
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    The write and read member functions take a pointer to a char (byte sized variable). Unless you explicitly cast your buffer to a char*, it won't compile. It will then read/write the amount bytes into your buffer that you specify in the second parameter, which in your case is the sizeof a long.

    You wouldn't ever have to do (unsigned long*)&x, if x was an unsigned long, just &x would be sufficient otherwise you are casting a pointer to the type it already is. It would be like doing
    this -

    int a =10;
    (int)a=20; //unecessary a is already an int

  7. #7
    Registered User
    Join Date
    Aug 2001
    Posts
    89
    thanks a lot man.
    behold the faithless one and his angel

  8. #8
    zoo
    Guest
    Here's one way.

    Code:
    #include<stdio.h>
    #include<fstream.h>
    
    int main() 
    {
    unsigned long x;
    remove("targetpathstring");
    ifstream file1("sourcepathstring",ios::binary);
    ofstream file2("targetstring",ios::binary);
    
    while(!file1.eof())
    { 
      file1.read( (unsigned char *) &x,sizeof(unsigned long));
      file2.write( (unsigned char *) &x,file1.gcount());
    }
    file1.close(); 
    file2.close();
    return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading User Defined Files
    By Necrofear in forum C++ Programming
    Replies: 17
    Last Post: 06-30-2006, 01:55 AM
  2. Linking header files, Source files and main program(Accel. C++)
    By Daniel Primed in forum C++ Programming
    Replies: 3
    Last Post: 01-17-2006, 11:46 AM
  3. reading files
    By hiya in forum C++ Programming
    Replies: 7
    Last Post: 05-21-2005, 12:40 PM
  4. A little help reading from files...
    By Invincible in forum C++ Programming
    Replies: 4
    Last Post: 04-03-2002, 10:43 AM
  5. Need Advice in reading files
    By jon in forum C Programming
    Replies: 4
    Last Post: 10-07-2001, 08:27 AM

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