Thread: Read/Write Method

  1. #1
    Registered User
    Join Date
    Oct 2004
    Posts
    13

    Read/Write Method

    This is my first post on a problem thats driving me slowly mad so here goes. What im trying to do is to write a method that takes 2 char parameters called filenameIn and filenameOut, so that i can read from an input file and copy the contents into an output file. The code i have written so far compiles and links fine but doesnt do anything-i have programmed before but only in java so this problem is one that i want to try and solve as its holding me up finishing the program. This is the code i have so far:

    Code:
    #include <iostream>
    #include "textfilecopy.h"
    
    
    using namespace std;
    
    /*
    * Partially completed program
    * The program should copy a text file.
    *
    */
    
    int main(int argc, char **argv) {
    	if (argc !=3) {
    		cerr << "Usage: " << argv[0] << " <input filename> <output filename>" << endl;
    		int keypress; cin >> keypress;
    		return -1;
    	}
    	textfilecopy tfc;
    
    	int keypress; cin >> keypress;
    }
    That is my main.cpp, then there's textfilecopy.h:

    Code:
    #pragma once
    #include <fstream>
    #include <iostream>
    #include <string>
    using namespace std;
    
    class textfilecopy
    {
    public:
    	textfilecopy();
    	~textfilecopy();
    	
    	Copy ( char* filenameIn, char* filenameOut)
    {
    	
    	ifstream infile ("C:\\Documents and Settings\\Paul McKenzie\\Programming Work\\Parser\\Debug\\input.txt");
    
    	if (!infile) {
      cerr << "Can't open input file " << filenameIn << endl;
      exit(1);
    }
    	
    	ofstream outfile("C:\\Documents and Settings\\Paul McKenzie\\Programming Work\\Parser\\Debug\\output.txt");
    
    	if (!outfile) {
      cerr << "Can't create output file " << filenameOut << endl;
      exit(1);
    	}
    
    	string s;
    	while (infile >> s) {
    		outfile << s << endl;
    	}
    	infile.close();
    	outfile.close();
    
    }
    };
    and finally textfilecopy.cpp

    Code:
    #include ".\textfilecopy.h"
    
    
    
    textfilecopy::textfilecopy()
    {
    }
    
    textfilecopy::~textfilecopy()
    {
    }
    If anyone could help me out id be so grateful, as i say im new to C++ so it wouldnt suprise me if i missed something basic, or put things in the wrong place lol

    Thanks a lot,

    Paul
    Last edited by bigpmc; 10-18-2004 at 09:19 AM.

  2. #2
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    before any of you attack this dude for code tags, let me point out that he tried....

    use the forward slash to close the tags /code

    ...i'll look at the code in a bit
    i seem to have GCC 3.3.4
    But how do i start it?
    I dont have a menu for it or anything.

  3. #3
    Registered User
    Join Date
    Oct 2004
    Posts
    13
    ha thanks-i edited the message now-got a bit confused, hope the format is ok for you guys now

  4. #4
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    hey, uh, dude.....you don't tell it to do anything.....you basically have this...

    int main()
    {
    textfilecopy tfc; //tfc(void)
    }

    textfilecopy::textfilecopy(void){ //nothing at all }


    i might not be seing it, but you need to call copy()
    tfc.copy()
    i seem to have GCC 3.3.4
    But how do i start it?
    I dont have a menu for it or anything.

  5. #5
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    i think in textfile.h instead of '#pragma once' that you want:
    Code:
    #ifndef TEXTFILECOPY_H
    #define TEXTFILECOPY_H
    
    class textfilecopy
    {};
    
    #endif
    what are you trying to do with '#pragma once'?

    also, if you #includes are in the same directory, it's ok to leave of the ./ ....actually, it's probabl;y better to not use ./
    i seem to have GCC 3.3.4
    But how do i start it?
    I dont have a menu for it or anything.

  6. #6
    Registered User
    Join Date
    Aug 2003
    Posts
    1,218
    The code looks ok to me, with two exceptions:
    This is a possible bug:
    Code:
    while (infile >> s) {
    		outfile << s << endl;
    	}
    Because what you do is you take the first word on the line, and print it to outfile and you then insert a new row. This will print one word on each line, maybe that is how you wanted it but if it isnt I suggest changing to this:
    Code:
    while (getline(infile,s)) {
    		outfile << s << endl;
    	}
    The last one is that you actually need to call the function, here is a small example how to call a function within a class:
    Code:
    #include <iostream>
    using namespace std;
    
    class Foo
    {
    public:
    	void Bar() 
    	{ 
    		cout << "You have called Bar in class Foo" << endl;
    	}
    };
    
    int main()
    {	
    	Foo foo;
    	foo.Bar();
    }

  7. #7
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    another thing....what compiler do you use....

    i don't think that should have compiled.....
    i seem to have GCC 3.3.4
    But how do i start it?
    I dont have a menu for it or anything.

  8. #8
    Registered User
    Join Date
    Aug 2003
    Posts
    1,218
    You are correct misplaced, Copy function needs a returntype.

  9. #9
    Registered User
    Join Date
    Oct 2004
    Posts
    13
    Thanks for all your replies, im using Visual Studio . net 2003, so as i thought its a simple thing-i havent actually called the method lol
    Thanks a lot and ill let you know if your suggestions work.

    Paul

  10. #10
    Registered User
    Join Date
    Oct 2004
    Posts
    13
    Oh one more thing, bear with me im new to C++ , when i want to call the function, do i do it from my main.cpp or from the place where i wrote the Copy method?

  11. #11
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    >>when i want to call the function, do i do it from my main.cpp or from the place where i wrote the Copy method?


    You call it when you need to call it. Depends on your intent. Usually it will be in one of the two places you mentioned, but it need not be.
    Last edited by elad; 10-18-2004 at 03:29 PM.

  12. #12
    Registered User
    Join Date
    Oct 2004
    Posts
    13
    Sorted it all out now thanks to all your help and a bit of experimenting. Thanks a lot for all your input, and i will try to return the favour to others if i can.

    Thank you all so much,

    Paul

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. on method pointers and inheritance
    By BrownB in forum C++ Programming
    Replies: 2
    Last Post: 03-02-2009, 07:50 PM
  2. stuck on display method
    By shintaro in forum C++ Programming
    Replies: 2
    Last Post: 02-01-2009, 05:17 PM
  3. Best communication method to thousand childs?
    By Ironic in forum C Programming
    Replies: 8
    Last Post: 11-08-2008, 12:30 AM
  4. C# method
    By siten0308 in forum C# Programming
    Replies: 6
    Last Post: 07-15-2008, 07:01 AM
  5. Replies: 2
    Last Post: 01-22-2008, 04:22 PM