-
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
-
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
-
ha thanks-i edited the message now-got a bit confused, hope the format is ok for you guys now
-
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 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 ./
-
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();
}
-
another thing....what compiler do you use....
i don't think that should have compiled.....
-
You are correct misplaced, Copy function needs a returntype.
-
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
-
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?
-
>>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.
-
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