How to copy strings from file into array of pointers
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 17

Thread: How to copy strings from file into array of pointers

  1. #1
    Join Date
    Aug 2013
    Posts
    4

    How to copy strings from file into array of pointers

    I am trying to copy string Line BY Line from text file into array of pointers. lets say file has only two lines for example.

    This is an apple.
    This is another apple.

    I want to copy both of these lines from file to array of pointers. Below is the code which i am trying to use for this. I know it has many mistake could you please assist me. Thanks
    Code:
    #include <iostream>
    #include <stdlib.h>
    #include <stdio.h>
    #include <fstream>
    #include <string.h>
    
    using namespace std;
    
    int main()
    {
        char const *filePath = "test.txt";
    
        char *getLines[2];
        char chPerLine[20];
        char *ptrString;
        fstream myFile;
        myFile.open(filePath, ios::in | ios::out);
        myFile.getline(chPerLine, 20, '\n');
        ptrString = chPerLine;
        strcpy(*getLines, ptrString);
        *(ptrString + 1);
    
        myFile.getline(chPerLine, 20, '\n');
        ptrString = chPerLine;
        strcpy(*getLines, ptrString);
        myFile.close();
    
        cout << getLines[0] << getLines[1];
        return 0;
    }

  2. #2
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,328

    Re: How to copy strings from file into array of pointers

    I am trying to copy string Line BY Line from text file into array of pointers.
    Why? Is this an assignment? If you want to read some lines from a file use the string class for each line and a vector of string to contain the lines.
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  3. #3
    Join Date
    Aug 2013
    Posts
    4

    Re: How to copy strings from file into array of pointers

    Quote Originally Posted by 2kaud View Post
    Why? Is this an assignment? If you want to read some lines from a file use the string class for each line and a vector of string to contain the lines.
    Yes its an assignment until now we did not cover vectors in school. Getline can get whole line and pointer to char can hold string so i want to use array of pointers to hold all the strings in a file and each string end with new line.

    Thanks for reply.

  4. #4
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Posts
    12,068

    Re: How to copy strings from file into array of pointers

    Pointers need to point to things. getLines is an array of two uninitialized pointers.

    This line doesn't do anything.
    *(ptrString + 1);

  5. #5
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,328

    Re: How to copy strings from file into array of pointers

    Code:
    char *getLines[2];
    getLines is an array of 2 elements of type pointer to char. ie each element should contain a valid memory address of an area of memory of the required size that contains chars. These memory addresses need to be obtained and assigned to the array elements. If valid memory addreses are not assigned to the array elements, these elements will contain unknown data as they haven't been initialised.

    So before you perform the strcpy() you need to initialise the appropriate getLines array element to a valid memory address of the appropriate size. Have you covered dynamic memory (eg new, delete)?

    Does your assignment actually specify using an array of pointers? What happens if you want to read more than the first two lines - or does the assignment specify only the first two?
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  6. #6
    Join Date
    Apr 1999
    Posts
    27,426

    Re: How to copy strings from file into array of pointers

    Quote Originally Posted by fazee6 View Post
    Yes its an assignment until now we did not cover vectors in school. Getline can get whole line and pointer to char can hold string
    Unless your teacher isn't teaching you correctly or you misheard him/her, that is not what a pointer is. A pointer to char does not hold a string. A char pointer is nothing more than an address -- you don't open up a pointer and magically there is a string inside of it.

    What happens is that the pointer to char will eventually point to a character that denotes the start of a buffer. That buffer is contiguous, so you can use the pointer to traverse this buffer to get the characters. The issue is that you have to create the buffer first, and then point the char pointer to the first character in the buffer.

    So did you learn operator new[]? That is one way to create this buffer. If you didn't learn it, then what did you learn as to the ways to create a character buffer at runtime?

    Regards,

    Paul McKenzie

  7. #7
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    3,845

    Re: How to copy strings from file into array of pointers

    WHY, WHY, WHY, would any half decent C++ instructor teach their pupils about pointers (and memory management) BEFORE instructing them about containers.

    IT

    MAKES

    NO

    SENSE

    AT

    ALL !!!!






    pointers and memory management is "semi-advanced" stuff, using containers is kids play.
    you don't even have to explain how containers actually work internally for making it understandable for a novice what they do.

    I'm currently mentoring a 7 year old into C++. He's already writing simple and actually usable programs with std::list and std::vector and std::map and is starting to get a grasp on writing templates, and he doesn't even know what a pointer or new/delete is yet.

  8. #8
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,328

    Re: How to copy strings from file into array of pointers

    Answers on a postcard!

    Because they seem to have the mindset of teaching the 'c' part first, then classes, templates and if lucky a bit on the STL. To be slightly fair to these instructors, in the past a lot of c++ books have followed this order. Hopefully this is now changing. The outdated reasoning for this is so that you don't use as far as possible what hasn't been taught. So to use the STL containers you need to have been taught classes and templates first etc. But as is pointed out in post #7, a 7 year old can use containers there're so easy!

    I've just made the 'discovery' that universities (http://forums.codeguru.com/showthrea...ue-with-a-book) in parts of Asia are still using Turbo c++ and teaching DOS programming! I despair.
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  9. #9
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Posts
    12,068

    Re: How to copy strings from file into array of pointers

    Quote Originally Posted by OReubens View Post
    WHY, WHY, WHY, would any half decent C++ instructor teach their pupils about pointers (and memory management) BEFORE instructing them about containers.

    IT

    MAKES

    NO

    SENSE

    AT

    ALL !!!!






    pointers and memory management is "semi-advanced" stuff, using containers is kids play.
    you don't even have to explain how containers actually work internally for making it understandable for a novice what they do.

    I'm currently mentoring a 7 year old into C++. He's already writing simple and actually usable programs with std::list and std::vector and std::map and is starting to get a grasp on writing templates, and he doesn't even know what a pointer or new/delete is yet.
    I think it makes sense. It's kind of like a music teacher teaching scales before songs. I think it's useful to understand what's happening under the hood.

  10. #10
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,266

    Re: How to copy strings from file into array of pointers

    Quote Originally Posted by GCDEF
    I think it makes sense. It's kind of like a music teacher teaching scales before songs. I think it's useful to understand what's happening under the hood.
    I would say that that analogy fails because the actions involved in playing scales develops the skill to play songs, whereas the knowledge involved in learning about pointers and memory management develops the skill to implement containers, but does not necessarily develop the skill to understand how to use them effectively to solve problems in general, yet the latter tends to be more important for a beginner. If the beginner already knows how to use containers effectively to solve problems, then learning about what is under the hood can increase their understanding on when to use what container (especially when associated operations have similiar time/space complexity) since they already have the basic understanding to proceed to making such choices independently.

    You may find Stroustrup's article on Learning Standard C++ as a New Language (PDF) to be an interesting read concerning this topic.
    Last edited by laserlight; January 14th, 2014 at 09:12 PM.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  11. #11
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    3,845

    Re: How to copy strings from file into array of pointers

    Quote Originally Posted by GCDEF View Post
    I think it makes sense. It's kind of like a music teacher teaching scales before songs. I think it's useful to understand what's happening under the hood.
    By that reasoning, you would find it ok/reasonable that anyone driving a car, needs to have a full understanding of all the mechanics and electronics that are in the car, as well as knowing everything about how combustion engines work, fuel to air ratio's, ...
    Sure, all that knowledge can maybe make you a better car driver, but they are not at all needed to drive a car.

    You can teach a 3year old kid how to sing a song. ANd it doesn't need to know what notes are.


    This kid I'm mentoring isn't a super genious he's about average in his class. He just wanted to write a couple games of his own which is why I started him on C++, I'll be getting into simple graphics next and even that's before he needs to know what pointers (or new/delete) is.

    If you do C++, then somewhere down the line, you'll HAVE to learn what pointers are, what new/delete is and how to properly use them, but they're by no means so elementary that you need to force feed this onto a novice before other stuff that can get them to produce useful and fun programs that keep them motivated and exciting.

    Introducing them to a pretty complex feature like pointers too early will confuse them and make them hate the language/programming. An unmotivated pupil isn't going to get very far.


    As a general rule, I find that it's much easier (and more fun for the pupil) to teach someone by example and experimentation, and explain the theory behind it later. There's this "bad" idea in some teachers heads that teaching the dry theory only is the way to go, there's no time/room/money for the fun aspect of learning by experiment. And then those same teachers wonder why classes are filled with bored and unmotivated children.
    Last edited by OReubens; January 15th, 2014 at 06:40 AM.

  12. #12
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Posts
    12,068

    Re: How to copy strings from file into array of pointers

    I'm not talking just about containers, although understanding how they work helps understand which is appropriate. Just about any C++ program or any size or complexity is going to involve the use of pointers. If you're going to use the Windows API, or even most APIs in general, you're going to need to know what they are and how they work. They're an essential concept in C++.

  13. #13
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,328

    Re: How to copy strings from file into array of pointers

    The issue for teachers is not if they teach about pointers, memory etc but when and in what order do they introduce concepts like pointers, containers, classes, templates etc. There's probably no absolutely right answer to this as it will depend somewhat upon the existing knowledge of the students, to whom the course is aimed etc etc but where appropriate I would favour early introduction to containers etc before introducing pointers. With c, pointers have to be introduced early but those reasons don't hold up for early introduction in c++.
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  14. #14
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    3,845

    Re: How to copy strings from file into array of pointers

    Quote Originally Posted by GCDEF View Post
    I'm not talking just about containers, although understanding how they work helps understand which is appropriate.
    Well no. you don't need to understand HOW they work.
    You need to understand what they DO, how they behave. I have proof that it's easy to make someone understand what a vector is, how it differs from a list and a map and when one is better than another without even ever explaining the "magic" behind how they work.

    How does a blender work: you throw in fruit and some fluid (milk or water), press the button. SMOOTHY.
    you don't need to know anything about electricity, how an electromotor works, how the electricity gets into your house, ....

    Just about any C++ program or any size or complexity is going to involve the use of pointers.
    Yes, and I said so, somewhere down the line, you'll have to learn it. But it's a falacy that you can't make actually usefull programs without using them directly (yes, they're there, cleverly hidden behind well constructed/tested classes that hide the nasty pointers).


    If you're going to use the Windows API, or even most APIs in general, you're going to need to know what they are and how they work.
    If you're going to be interfacing with something "outside of C++/STL", then yes, maybe. But even here: use a library that hides all the messy pointers.

    They're an essential concept in C++.
    If you're going to be making libraries, your own containers, interfacing with legacy API's, COM etc...
    YES, you're goign to need it eventually.
    That makes them an (semi) "advanced" concept in C++.

    I have proof that it is entirely possible to make fun, actually useful APPLICATIONS that don't need any pointers at all. You just need a couple well written libraries/classes that hide the messy pointers.


    I wouldn't call them "essential", unless you count in the fact that they're essential to compile the libraries/classes you're using.

  15. #15
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    3,845

    Re: How to copy strings from file into array of pointers

    Quote Originally Posted by 2kaud View Post
    With c, pointers have to be introduced early but those reasons don't hold up for early introduction in c++.
    This isn't any different for C than it is for C++ other than the fact that C by default lacks dynamically sized containers like C++ does.
    You can still teach someone C without ever touching the concept of pointers, but granted, it'll be a tad more difficult or you'll need to introduce them to premade (non standard) libraries earlier.

    If you're anywhere near following microcontroller development such as ARDUINO. This is even proof in that. THere are tons and tons of Arduino projects (called "sketches" but this basically just a C program compiled with GCC) out there that don't use pointers at all. THe only "odd" thing here is that the main() is implemented inside the microcontroller core library, and you need to provide a loop() and setup() function yourself (instead of a main()).

    There are also libraries of arduino functions that internally may use pointers but hide those from their accessible interfaces, so the code the endusers write never need to use pointers.

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center