reading a line in a file and setting a pointer to the value

Good afternoon, how are you all doing? I am not sure what is going on, but what I am trying to do is read a file get the string I read and set that value of the string to a pointer that is being passed to another function. For ex.
data* z;
getline(file, "string in line read")

func1( z) z be pointing to the string line read. How do I do that I keep passing  a empty value? What could I possibly be doing wrong? Thanks for the help.
LVL 1
Corey_819Asked:
Who is Participating?
 
itsmeandnobodyelseCommented:

   ptr z;  
   z = new Data;
   if (getline(file, z->city_name))
       RB_Insert(Root, z);

That seems to be correct. I assume 'Root' is the tree where you want to insert a new Data 'node'. Note, that you have to delete the pointer z when deleting the tree or you would get memory leaks.

>> z = new data or ptr?

Only   z = new Data; is correct as   z = new ptr; would create a pointer to ptr, that is of type Data**.

Regards, Alex



 



0
 
van_dyCommented:
use something like

std::string z;
... open your file..
getline(file, z);
func1(z);

the downside is that you will have to modify the func1() as something like

func1(std::string& z)
{
  ....
0
 
Jaime OlivaresSoftware ArchitectCommented:
There are different ways to acomplish what you are trying to do.
If you want to return a pointer, you must first do allocation:

data *z;
// some actions
z = func1();

inside func1() you must allocate space related to the pointer, this way:
z = new data;
// fill data with proper values
return z;

You have the possibility to return NULL if something fails.
You must use delete operator to delete pointed data before used to avoid memory leaks.
0
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

 
Corey_819Author Commented:
Then only thing is z  is a pointer to the line I am reading. Maybe I am missing something in my getline.

data* z;
This is my syntax getline(file, temp(name of struct).city_name(string);

I want that line above to go to this functions
func1(z);

Am I missing something? Thanks for the help
0
 
van_dyCommented:
well you can use

z be declared something like
char z[MAX_LINE];

file.getline(z, MAX_LINE, '\n');
0
 
Corey_819Author Commented:
Jaime I am not trying to fill a vector or array just displaying a file by pointers? Would I still need to return z? Thank you.
0
 
Jaime OlivaresSoftware ArchitectCommented:
You can't have a pointer to a file.
All methods will force to read a file portion into a buffer, then you can point the buffer.
Buffer (and pointer) could be the same every time you read a line, or can be different every time you read a line.
z is a pointer, so I don't see where is the problem to return it by the "normal way", unless you have a more sophisticated need, or a specific homework requirement.
0
 
Corey_819Author Commented:
I am sorry maybe I am not being clear. I don't really want a pointer to a file. I have a vairable getting filed with the string and then inserting that string in a tree using pointers. Does that clear up what I am asking?  z is supposed to be my new pointer to the postion in the tree. I can't figure out how to point z to the line I read in the file it is sending me a null or nothing there value being pointed too.
0
 
Jaime OlivaresSoftware ArchitectCommented:
If you are building a tree, then you need dynamic allocation as I have suggested, you need to use the 'new' operator to allocate every string you read from file.
Solution could include an STL string as suggested by van_dy, maybe something like this:

std::string *s;
zs = new std::string;
getline(file, *s);
func1(z, s);   // atach string s to node z

If you don't do dynamic allocation, string will be destroyed after callinc func1(z)

Since you will need to dynamically allocate z too (similar to s), then you can make string s as a member of your node

class data {
     // some members
     string s;
}
0
 
itsmeandnobodyelseCommented:
I am assuming the name of your struct is 'data'. Right? (if not please post definitions of all variables, structs, trees and pointers you are using here).

Then, maybe you need this:

data* func()
{
  data* z = new data;           // create a new data object
  getline(file, z->city_name); // read city name
  return z;
}

Regards, Alex
 

0
 
Corey_819Author Commented:
Data is my struct. I am doing a typedef Data* ptr so I don't have to forget the * everytime. I define a pointer. So would I just use this code to do the

ptr z;
z = new data or ptr?

getline(file, z->city_name);
RB_Insert(Root, z)? Correct? Thanks for all your help
0
 
Corey_819Author Commented:
Thank you that is what I was missing. Thanks for the help itsmeandnobodyelse
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.