Solved

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

Posted on 2004-11-02
209 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
Question by:Corey_819
    12 Comments
     
    LVL 5

    Expert Comment

    by: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)
    {
      ....
    0
     
    LVL 55

    Expert Comment

    by:Jaime Olivares
    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
    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
     
    LVL 5

    Expert Comment

    by:van_dy
    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
    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
    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
    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
    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
    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
    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:

       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
    Thank you that is what I was missing. Thanks for the help itsmeandnobodyelse
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Find Ransomware Secrets With All-Source Analysis

    Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

    Suggested Solutions

    Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
    Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
    The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
    The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

    857 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

    Need Help in Real-Time?

    Connect with top rated Experts

    9 Experts available now in Live!

    Get 1:1 Help Now