943,683 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 7509
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Feb 18th, 2007
0

What's wrong with this strcpy and this pointer

Expand Post »
Hello
I'm trying to assign a value from a string via some pointer, but I got an error and I cannot figure how to solve it.
Do you have an idea? I would really help me. The problematic line is marked between *********
Thanks.


C++ Syntax (Toggle Plain Text)
  1. // reading a text file
  2. #include <iostream>
  3. #include <fstream>
  4. #include <string>
  5. using namespace std;
  6.  
  7.  
  8. struct Cours
  9. {
  10. char *sigle;
  11. Cours *suivant;
  12. };
  13.  
  14. struct Etudiant
  15. {
  16. char *nom;
  17. Etudiant *apres;
  18. };
  19.  
  20. struct Professeur
  21. {
  22. char nom [20];
  23. Professeur *suivant;
  24. };
  25.  
  26.  
  27. class DossierProfesseur
  28. {
  29. public:
  30. Professeur *tete;
  31. DossierProfesseur();
  32. ~DossierProfesseur();
  33. void affichage();
  34. };
  35.  
  36.  
  37. DossierProfesseur::DossierProfesseur()
  38. {
  39. Professeur *PCourant, *PTete;
  40. string line;
  41. ifstream myfile ("FP.txt");
  42. if (myfile.is_open())
  43. {
  44. while (! myfile.eof() )
  45. {
  46. getline (myfile,line);
  47. cout << line << endl;
  48. PCourant = new Professeur;
  49. /*****************************************************/
  50. strcpy (PCourant->nom, line);
  51. /*****************************************************/
  52. }
  53. myfile.close();
  54. }
  55.  
  56. else cout << "Unable to open file";
  57. }
  58.  
  59.  
  60. void main () {
  61.  
  62.  
  63. DossierProfesseur *dossierProfesseur;
  64.  
  65. dossierProfesseur= new DossierProfesseur;
  66. }
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
earlyriser is offline Offline
22 posts
since Nov 2006
Feb 18th, 2007
0

Re: What's wrong with this strcpy and this pointer

Last edited by iamthwee; Feb 18th, 2007 at 4:47 pm.
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Feb 18th, 2007
0

Re: What's wrong with this strcpy and this pointer

Thanks iamthwee, but it is not helping me a lot, I was in that page before writing this post and I did what I understood:

strcpy (where-the-data-will-be, where-there-are-now)

C++ Syntax (Toggle Plain Text)
  1. strcpy (PCourant->nom, line);
Last edited by earlyriser; Feb 18th, 2007 at 4:56 pm.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
earlyriser is offline Offline
22 posts
since Nov 2006
Feb 18th, 2007
0

Re: What's wrong with this strcpy and this pointer

>> strcpy (PCourant->nom, line);

line is a c++ class, strcpy() wants a c style pointer. You have to use std::string's c_str method to get the pointer, like this
strcpy (PCourant->nom, line.c_str());

  1. struct Professeur
  2. {
  3. char nom [20];
  4. Professeur *suivant;
  5. };
Since you are writing a c++ program you should use c++ std::string class, not character arrays, unless your teacher requires you to use character arrays. Using std::string will make your life a lot easier and eleminate several problems with using strcpy() and other similar standard c string handling functions. The main problem with those functions is they will allow you to scribble all over memory by copying strings beyond the allocation of the destination buffer. Example: copy a 25 character string into that buffer that has only room for 19 characters plus null terminator. After that happens your program will likely crash at some point and you will spend hours trying to find the problem.
Last edited by Ancient Dragon; Feb 18th, 2007 at 5:03 pm.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is online now Online
21,949 posts
since Aug 2005
Feb 18th, 2007
0

Re: What's wrong with this strcpy and this pointer

yeah and don't forget to clean up after with delete[], avoid void main and EOF blah blah blah.
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Feb 18th, 2007
0

Re: What's wrong with this strcpy and this pointer

Thanks Ancient Dragon!
It works, well, the program doesnt, but at least the strcpy works.
My teacher wants char arrays.

Thanks iamthwee!
Last edited by earlyriser; Feb 18th, 2007 at 5:34 pm.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
earlyriser is offline Offline
22 posts
since Nov 2006
Feb 18th, 2007
0

Re: What's wrong with this strcpy and this pointer

After reading a line in a text file I need to read another line which contains an INT. I thought that the bold line in this code will be ok to retreive the data, but it doesnt.

            
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
using namespace std;

struct Professeur
{
    char nom[20];
    int ancien;
};


class DossierProfesseur
{
   
        
    public:
        Professeur *tete;
        DossierProfesseur();
        ~DossierProfesseur();
};


DossierProfesseur::DossierProfesseur()
{
    Professeur *PCourant, *PTete=NULL;
    string line;
    ifstream myfile ("FP.txt");
    if (myfile.is_open())
    {
        while (! myfile.eof() )
        {
            getline (myfile,line);
            PCourant = new Professeur;
            strcpy (PCourant->nom, line.c_str());
            cout << PCourant->nom;
            getline (myfile, PCourant->ancien);
        }
        myfile.close();
    }

    else cout << "Unable to open file";
}


void main () {
 

    DossierProfesseur *dossierProfesseur;
    
    dossierProfesseur= new DossierProfesseur;
}
Reputation Points: 10
Solved Threads: 0
Newbie Poster
earlyriser is offline Offline
22 posts
since Nov 2006
Feb 18th, 2007
0

Re: What's wrong with this strcpy and this pointer

You're trying to use getline() to read a string into an integer. It just ain't going to work. Read it into a string, and take it out with atoi() or use a stringstream.

C++ Syntax (Toggle Plain Text)
  1. std::string line;
  2. std::stringstream lineStream;
  3. getline(myFile, line);
  4. lineStream << line;
  5. lineStream >> PCourant->ancien;

Your code is driving me crazy! If you're going to use char arrays, you might as well be using C. And no, I know it's not your fault, so I blame your teacher for that.
Team Colleague
Reputation Points: 2240
Solved Threads: 338
Vampirical Lurker
John A is offline Offline
5,055 posts
since Apr 2006
Feb 18th, 2007
0

Re: What's wrong with this strcpy and this pointer

Thanks joeprogrammer!
I will try that and give the feedback tomorrow, (I have been doing this homework all the day).
I didn't know that C is better for char arrays than C++.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
earlyriser is offline Offline
22 posts
since Nov 2006
Feb 18th, 2007
0

Re: What's wrong with this strcpy and this pointer

Click to Expand / Collapse  Quote originally posted by earlyriser ...
Thanks joeprogrammer!
I will try that and give the feedback tomorrow, (I have been doing this homework all the day).
I didn't know that C is better for char arrays than C++.
It's better because C functions were designed for char arrays. A lot of C++ functions were designed for std::string. You could always use the old C functions to manipulate the mix of C and C++ strings, but you usually shouldn't mix C and C++ functions unless it's absolutely necessary.
Team Colleague
Reputation Points: 2240
Solved Threads: 338
Vampirical Lurker
John A is offline Offline
5,055 posts
since Apr 2006

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: IO design problem
Next Thread in C++ Forum Timeline: Need some help on my program!!!!!!





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC