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.

detraveller
  • 285
  • 3
  • 17
  • 8
    You should really consider [reading a good book or two](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) - from your question, it looks like you don't have the fundamentals quite lined up yet. – Mat May 07 '13 at 11:18
  • One way to do this is to return the variable from the `read()` function and pass it to the `write()` function as an argument. However, the exact mechanism you choose will depend on your architecture. – ChrisF May 07 '13 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? – Peter Wood May 07 '13 at 11:22
  • 1
    @PeterWood - True. I'll edit the comment. – ChrisF May 07 '13 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. – quetzalcoatl May 07 '13 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 (http://www.msi.umn.edu/~esevre/amira/dev/compmodule1.html) – quetzalcoatl May 07 '13 at 11:34
  • Yup it's Amira. And sorry my question about the class definition was stupid, i just got confused by the HX_INIT thing. @quetzalcoatl – detraveller May 07 '13 at 11:41
  • No worries.. but let's return to the main thing. Are those read()/write() functions or methods? Do they reside in some class, ie. that HxCluster? If so, are they `static` or are they normal methods? Can you modify the structure of that class, i.e. can you add new fields to it? If they are normal methods and if you can modify the class, then just "merge" my example with your code: add a "line_of_text" field to your class and use it inside your read/write to hold the temporary line-of-text – quetzalcoatl May 07 '13 at 12:24
  • @quetzalcoatl As i can see in HxCluster.h file, They are `functions`. `read()` is `static int` but `write()` is only `int`. I have created a new char array `header` and tried `header = buffer`(in the read function) where buffer contains the desired line i want to copy and print. I get the error `invalid use of member HxCluster::header in static member function static int HxCluster::readIMD(const char*)` – detraveller May 08 '13 at 09:09
  • All I want to do is copy the header in the `read()` function and then print it using `fprintf` when i am in the `write()` function. – detraveller May 08 '13 at 09:14
  • I understand what you want to do, but I cannot guess/tell anything more until you say a few things more. The fact that read/write are in HxCluster is important. It is very strange that read/write differ in their "static-ness". I do not know the Amira library, but any normal layout would make such pair of functions/methods symmetric. Are you 100% sure that this 'static int read' was the only read() method in this class? Maybe there's some another non-static read() there, for example, inherited from base class? – quetzalcoatl May 08 '13 at 10:03
  • If one or both (of read/write) are static, then all fields/variables added to the class will have to be static too. If one is static and the other is not, you will have to be **very careful** and you will have to check **how many** such HxCluster exist at a time, or else you may accidentially start mixing data from one HxC object with data of another HxC object. If both are nonstatic, then new fields would be also nonstatic and no danger of mixing woudl exist. That's why it is very important to check that `read` is really static.. – quetzalcoatl May 08 '13 at 10:08
  • Another thing - is HxCluster.h/.cpp a your file at all? Googling around it looks like it is a component provided by the Amira library. Do you know whether it is intended/designed to be modified by you? If it is not, it might be better to leave it alone unmodified and find another way to pass the data, or to rethink the problem (I mean, do you really need to modify the read/write methods? can you achieve your **final effect** in any other ways?). If it's the only option, well, Amira's code is still a code, so modifying read/write, static or not, is surely doable ;) – quetzalcoatl May 08 '13 at 10:11
  • Last thing: the error `invalid use of ...` probably comes from static/nonstatic mismatch. Check if your `buffer` was marked as static. The error claims that the `readIMD` was static, so it can touch only static things, so if the buffer were not such - error would be risen. – quetzalcoatl May 08 '13 at 10:13

5 Answers5

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.

quetzalcoatl
  • 32,194
  • 8
  • 68
  • 107
  • 2
    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. – quetzalcoatl May 07 '13 at 11:27
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.

john
  • 7,897
  • 29
  • 27
  • 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. – detraveller May 07 '13 at 11:36
  • Well in that case it seems that something along the lines of quetzalcoatl's answer is best. – john May 07 '13 at 11:49
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

Thomja
  • 259
  • 5
  • 15
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.

mwerschy
  • 1,698
  • 1
  • 15
  • 26
  • 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 – detraveller May 07 '13 at 11:21
  • 1
    -1 for suggesting using global variables. That's almost always a **very bad** idea. – Spook May 07 '13 at 11:24
  • 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. – mwerschy May 07 '13 at 11:26
  • 2
    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. – Spook May 07 '13 at 11:28
-3

Sure, Use pointers!

void main(){
  char* line = malloc(100*sizeof(char));
  read_function (line);
  write_function (line);
}

void read_function(char* line){
  .... read a line
  strcpy (line, the_line_you_read_from_file);
}

void write_function (char* line){
  fprintf (fp,"%s", line);
}
Aus
  • 1,183
  • 11
  • 27
  • 1
    aside from `malloc` and not using `string`, also note that even in C you can RETURN things like pointers or even structures and that you usually don't want to blindly assume that a line will not be longer than X characters. – quetzalcoatl May 07 '13 at 11:30
  • @Spook As long as the solution with globals doesn't leak memory (like this one does), I would even prefer a solution with globals. – Constantin May 07 '13 at 12:52