| | |
What's wrong with this strcpy and this pointer
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
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.
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)
// reading a text file #include <iostream> #include <fstream> #include <string> using namespace std; struct Cours { char *sigle; Cours *suivant; }; struct Etudiant { char *nom; Etudiant *apres; }; struct Professeur { char nom [20]; Professeur *suivant; }; class DossierProfesseur { public: Professeur *tete; DossierProfesseur(); ~DossierProfesseur(); void affichage(); }; DossierProfesseur::DossierProfesseur() { Professeur *PCourant, *PTete; string line; ifstream myfile ("FP.txt"); if (myfile.is_open()) { while (! myfile.eof() ) { getline (myfile,line); cout << line << endl; PCourant = new Professeur; /*****************************************************/ strcpy (PCourant->nom, line); /*****************************************************/ } myfile.close(); } else cout << "Unable to open file"; } void main () { DossierProfesseur *dossierProfesseur; dossierProfesseur= new DossierProfesseur; }
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)
strcpy (where-the-data-will-be, where-there-are-now)
C++ Syntax (Toggle Plain Text)
strcpy (PCourant->nom, line);
Last edited by earlyriser; Feb 18th, 2007 at 4:56 pm.
>> 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
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.
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()); c Syntax (Toggle Plain Text)
struct Professeur { char nom [20]; Professeur *suivant; };
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.
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;
} 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.
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.
C++ Syntax (Toggle Plain Text)
std::string line; std::stringstream lineStream; getline(myFile, line); lineStream << line; 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."
All my posts may be freely redistributed under the terms of the MIT license.
All my posts may be freely redistributed under the terms of the MIT license.
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."
All my posts may be freely redistributed under the terms of the MIT license.
All my posts may be freely redistributed under the terms of the MIT license.
![]() |
Similar Threads
- problems with pointers (C++)
- help!! about ADT list please (C)
- Errors help (C++)
- total newb - "passing arg 2 of `strcpy' makes pointer from integer without a cast" (C++)
- Array troubles? (C++)
Other Threads in the C++ Forum
- Previous Thread: IO design problem
- Next Thread: Need some help on my program!!!!!!
| Thread Tools | Search this Thread |
Tag cloud for C++
api application array arrays based beginner binary bmp c++ c/c++ calculator char char* class classes code compile compiler console conversion convert count data delete deploy dll download dynamiccharacterarray email encryption error file format forms fstream function functions game givemetehcodez graph homeworkhelp iamthwee ifstream input int java lib library lines list loop looping loops map math matrix memory newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg search simple sorting spoonfeeding string strings struct temperature template templates text text-file tree url variable vector video visual visualstudio void win32 windows winsock wordfrequency wxwidgets






