Link to home
Start Free TrialLog in
Avatar of Corey_819
Corey_819

asked on

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.
Avatar of van_dy
van_dy

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)
{
  ....
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.
Avatar of Corey_819

ASKER

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
well you can use

z be declared something like
char z[MAX_LINE];

file.getline(z, MAX_LINE, '\n');
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.
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.
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.
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;
}
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
 

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
ASKER CERTIFIED SOLUTION
Avatar of itsmeandnobodyelse
itsmeandnobodyelse
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank you that is what I was missing. Thanks for the help itsmeandnobodyelse