What's wrong with this strcpy and this pointer

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Nov 2006
Posts: 22
Reputation: earlyriser is an unknown quantity at this point 
Solved Threads: 0
earlyriser's Avatar
earlyriser earlyriser is offline Offline
Newbie Poster

What's wrong with this strcpy and this pointer

 
0
  #1
Feb 18th, 2007
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.


  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. }
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,264
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

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

 
0
  #2
Feb 18th, 2007
Last edited by iamthwee; Feb 18th, 2007 at 4:47 pm.
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Nov 2006
Posts: 22
Reputation: earlyriser is an unknown quantity at this point 
Solved Threads: 0
earlyriser's Avatar
earlyriser earlyriser is offline Offline
Newbie Poster

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

 
0
  #3
Feb 18th, 2007
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)

  1. strcpy (PCourant->nom, line);
Last edited by earlyriser; Feb 18th, 2007 at 4:56 pm.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,362
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1464
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

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

 
0
  #4
Feb 18th, 2007
>> 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.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,264
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

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

 
0
  #5
Feb 18th, 2007
yeah and don't forget to clean up after with delete[], avoid void main and EOF blah blah blah.
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Nov 2006
Posts: 22
Reputation: earlyriser is an unknown quantity at this point 
Solved Threads: 0
earlyriser's Avatar
earlyriser earlyriser is offline Offline
Newbie Poster

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

 
0
  #6
Feb 18th, 2007
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.
Reply With Quote Quick reply to this message  
Join Date: Nov 2006
Posts: 22
Reputation: earlyriser is an unknown quantity at this point 
Solved Threads: 0
earlyriser's Avatar
earlyriser earlyriser is offline Offline
Newbie Poster

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

 
0
  #7
Feb 18th, 2007
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;
}
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 5,051
Reputation: John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold 
Solved Threads: 332
Team Colleague
John A's Avatar
John A John A is offline Offline
Vampirical Lurker

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

 
0
  #8
Feb 18th, 2007
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.

  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.
"Technological progress is like an axe in the hands of a pathological criminal."
Reply With Quote Quick reply to this message  
Join Date: Nov 2006
Posts: 22
Reputation: earlyriser is an unknown quantity at this point 
Solved Threads: 0
earlyriser's Avatar
earlyriser earlyriser is offline Offline
Newbie Poster

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

 
0
  #9
Feb 18th, 2007
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++.
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 5,051
Reputation: John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold 
Solved Threads: 332
Team Colleague
John A's Avatar
John A John A is offline Offline
Vampirical Lurker

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

 
0
  #10
Feb 18th, 2007
Originally Posted by earlyriser View Post
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.
"Technological progress is like an axe in the hands of a pathological criminal."
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC