0

I have two functions read() and write(). I read a file in the read() function and store a line in the header in a variable. Now i want the write() function to write that same line to a new file. But how can i use the same variable or information from the other function? What is the way to do this?

Here is some info about the code:

After including necessary files, it says this

HX_INIT_CLASS(HxCluster,HxVertexSet);

The name of the class is HxCluster and it would be great if someone can tell me why it is not like we define classes in the simple way: class class_name {};

The I have many functions out of which two are read() and write(). They both take one argument only which is the file to be read and the file to be written to in the respective cases. I don't know if writing the code for that will help here.

14
  • 8
    You should really consider reading a good book or two - from your question, it looks like you don't have the fundamentals quite lined up yet. Commented May 7, 2013 at 11:18
  • 2
    @ChrisF Using a global variable is almost never the right answer. Why are you suggesting it to someone who obviously needs some help in the basics? Commented May 7, 2013 at 11:22
  • 1
    @PeterWood - True. I'll edit the comment. Commented May 7, 2013 at 11:23
  • 1
    @detraveller: look at your HxCluster.H or HxCluster.HPP file and you will probably find the "class HxCluster {}". I think HX_INIT_CLASS macro is used in CPP files only. Commented May 7, 2013 at 11:32
  • 1
    Also, as the HX_INIT_CLASS popped up, it would be probably worthy to say what libraries are you using. Currently for me it looks like Amira (msi.umn.edu/~esevre/amira/dev/compmodule1.html) Commented May 7, 2013 at 11:34

4 Answers 4

5

If I understood you well, this is just what in C++ the structures/classes/objects are for. For example:

class FileLineWriter
{
public:
    FileLineWriter();

    void read(istream& inputfile);
    void write(ostream& putfile);

private:
    string line_of_text;
};

void FileLineWriter::read(istream& s)
{
    // s >> this->line_of_text; // possible, but probably will not do what you think
    getline(s, this->line_of_text);
}

void FileLineWriter::read(ostream& s)
{
    s << this->line_of_text;
}

...
FileLineWriter writer;
writer.read(firstfile);
writer.write(secondfile);

note that the above is NOT a working code. It is just a sample. You will have to fix all typos, missing namespaces, headers, add stream opening/closing/error handling, etc.

Sign up to request clarification or add additional context in comments.

1 Comment

of course, I wrote everything assuming that you can alter the read/write functions to became methods of an object. If you can not do that (i.e. the read()/write() must remain "objectless" plain functions), then returning-a-line (plain return or via ref/pointer parametr) or using-somekindof-globalvariable is the only option.
1

You return the variable from read and pass it as a parameter to write. Something like this

std::string read()
{
   std::string header = ...
   return header;
}

void write(std::string header)
{
   ...
}

std::string header = read();
write(header);

Passing information between functions is a basic C++ skill to learn.

2 Comments

Yes but in my case passing arguments is out of question since the function can only take in one argument which is the name of the read/write file.
Well in that case it seems that something along the lines of quetzalcoatl's answer is best.
0

If I have understood this right then I would suggest that you save the info on the variable to a string or an int depending on what kind of info it is.

I would also recommend to always include some code for us to be able to give you some more help

Comments

0

You can either make write take an argument, void write(std::string text) or you can store the string you read as a global variable std::string text at the top of your .cpp file, text = ... in your read function (replace ... with ifstream or whatever you use) and then write text in your write funcion.

4 Comments

This is exactly what i thought should have worked. I tried that and it didn't. Let me try again and then I will come back with the code if that doesn't work. Thanks
-1 for suggesting using global variables. That's almost always a very bad idea.
It is, that's why I suggested using function arguments first. I still think he should be given all information so he can choose for himself.
I disagree. Writing bad, but working code is always an option, but shouldn't be suggested to anyone (especially a beginner) to avoid creating bad habits. In C you might have suggested using global variables. In C++ it's almost always a terrible idea.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.