Welcome to Dream.In.Code
Become a C++ Expert!

Join 149,445 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,225 people online right now. Registration is fast and FREE... Join Now!




File and Stream Manipulation

 
Reply to this topicStart new topic

File and Stream Manipulation

spazzedout
29 Oct, 2007 - 06:10 PM
Post #1

New D.I.C Head
*

Joined: 3 Oct, 2007
Posts: 41


My Contributions
CODE


#include <string>
#include <iostream>
#include <cstdlib>
#include <fstream>
using namespace std;

char transferFile(ifstream &in_stream);

void main()
{
    ifstream in_stream;
    

    in_stream.open("FILEIOLabDATAFILE.txt",ios_base::in);
        if(in_stream.fail())
        {
            cout << " failed to open the file!!!" << endl;
            exit(1);
        }
    
        
    transferFile(in_stream);
        

}
char transferFile(ifstream &in_stream)
{
    ofstream out_stream;

    out_stream.open("FileIOLAb2.txt");

        if(in_stream.fail())
        {
            cout << " failed to open the file!!!" << endl;
            exit(1);
        }

out_stream << in_stream;

return 0;
}


i created this code. the purpose i have for this program is to read the file on the main function and write the data in the file to another file in another function, but i think i made some mistake, when i debug the program.. it runs... however... it did not gave me the data input to another file i desired. i had a feeling the problem is located at the part where i say "out_stream << in stream;" the word documents i have the program read is more than 500 word and the output to the second file when i run this program is only like 6???? blink.gif blink.gif help me please. thank you smile.gif ok ok... so i said i know where the problem is ... i do not know how to fix it, i tried everything i know... !!! HELPP!!!

This post has been edited by spazzedout: 29 Oct, 2007 - 06:38 PM
User is offlineProfile CardPM
+Quote Post

Martyr2
RE: File And Stream Manipulation
29 Oct, 2007 - 07:37 PM
Post #2

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 5,655



Thanked: 313 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
Why don't you try something along this line...

CODE

char transferFile(ifstream &in_stream)
{
    ofstream out_stream;
    char line[100];

    out_stream.open("FileIOLAb2.txt");

    // Check the outstream failed, which is what I think you meant.
    if(out_stream.fail())
    {
        cout << " failed to open the file!!!" << endl;
        exit(1);
    }

    // Loop through the instream and pull each line into a variable first
    while (in_stream.getline(line,100)) {

        // Then dump the line to the output stream
        out_stream << line << endl;
    }

    // Don't forget to close your file after you are done transferring.
    out_stream.close();

    return 0;
}


In this example I added one intermediate step, dump the line to a variable and then onto the output stream. Put it in a loop and it will transfer the entire file line by line like I think you meant it too.

Give this a try and I think you will see it works pretty nicely. Oh and don't forget to close your file streams. We wouldn't want any locked up resources.

Enjoy!

"At DIC we be coding ninjas!" decap.gif

This post has been edited by Martyr2: 29 Oct, 2007 - 07:38 PM
User is offlineProfile CardPM
+Quote Post

spazzedout
RE: File And Stream Manipulation
30 Oct, 2007 - 06:53 AM
Post #3

New D.I.C Head
*

Joined: 3 Oct, 2007
Posts: 41


My Contributions
thanks, its was so easy and it took me forever... bananaman.gif
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 1/7/09 12:45PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month