Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2004-11-02
12
Medium Priority
?
215 Views
Last Modified: 2010-08-05
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.
0
Comment
Question by:Corey_819
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
  • 2
  • +1
12 Comments
 
LVL 5

Expert Comment

by:van_dy
ID: 12475815
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
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 12475897
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
 
LVL 1

Author Comment

by:Corey_819
ID: 12475916
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 5

Expert Comment

by:van_dy
ID: 12475967
well you can use

z be declared something like
char z[MAX_LINE];

file.getline(z, MAX_LINE, '\n');
0
 
LVL 1

Author Comment

by:Corey_819
ID: 12475986
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
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 12476089
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
 
LVL 1

Author Comment

by:Corey_819
ID: 12476134
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
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 12476240
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
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 12481658
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
 
LVL 1

Author Comment

by:Corey_819
ID: 12484532
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
 
LVL 39

Accepted Solution

by:
itsmeandnobodyelse earned 160 total points
ID: 12485406

   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
 
LVL 1

Author Comment

by:Corey_819
ID: 12508175
Thank you that is what I was missing. Thanks for the help itsmeandnobodyelse
0

Featured Post

Tech or Treat! - Giveaway

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

604 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question