*strcpy and linked list

Please support our C# advertiser: Intel Parallel Studio Home
Reply

Join Date: Sep 2006
Posts: 12
Reputation: Line is an unknown quantity at this point 
Solved Threads: 0
Line's Avatar
Line Line is offline Offline
Newbie Poster

*strcpy and linked list

 
1
  #1
Sep 19th, 2006
Hey!

I'm looking for some help on what I'm doing wrong as I'm new to C#. I'd really like it if you can explain why my code isn't working and then offer some help on how to fix it without "making it easy" for me ;o) I have to understand what's happening :oP hehe...

On to the problem:

I have a linked list containing pointers to lines read from a file. What I would like to do is copy these lines into a char.

Can I use *strcpy for this? The way I use *strcpy now isn't working; I'm not getting the entire text copied. Maybe the error isn't the strcpy but some other part of my code?

Here's the code I guess is relevant:
  1. struct lenket{
  2. struct lenket *neste;
  3. char *dir;
  4. };
  5.  
  6. struct lenket *liste = NULL, *siste = NULL, *pdp, *q;

  1. int main(int argc, char **argv)
  2. {
  3. FILE *fp;
  4. char *everything;
  5. unsigned int filesize;
  6. char *p1, *p2;
  7. char *filnavn = argv[2];
  8.  
  9. if ((fp = fopen(filnavn,"r")) != NULL) {
  10. printf("Filen %s er åpnet.\n\n", filnavn);
  11. fseek(fp, 0, SEEK_END);
  12. filesize = ftell(fp);
  13. p1=p2 = everything = malloc(sizeof(char) * filesize);
  14. fseek(fp, 0, SEEK_SET);
  15. fread(everything, 1, filesize+1, fp);
  16. everything[filesize] = 0;
  17.  
  18. while(p1-everything < filesize){
  19. if(*p1 =='\n'|| p1-everything == filesize-1 ){
  20. if(*p1==0){
  21. break;
  22. }
  23.  
  24. *p1='\0';
  25. pdp=malloc(sizeof(struct lenket));
  26. pdp->neste=NULL;
  27. pdp->dir=malloc(p1-p2+1);
  28. strncpy(pdp->dir, p2, p1-p2);
  29. if(liste==NULL){
  30. liste=siste=pdp;
  31. }else{
  32. siste->neste=pdp;
  33. siste=pdp;
  34. }
  35. p2=p1+1;
  36. }else{
  37. ++p1;
  38. }
  39. }
  40. pdp=liste;

  1. char *teksten = NULL;
  2. teksten=malloc(sizeof(teksten)+1);
  3.  
  4. do {
  5. *strcpy(teksten, pdp->dir);
  6. printf("%s\n", pdp->dir);
  7. } while((pdp=pdp->neste) != NULL);

All help is appreciated! :o)
Reply With Quote Quick reply to this message  
Join Date: Feb 2002
Posts: 898
Reputation: Tekmaven is a glorious beacon of light Tekmaven is a glorious beacon of light Tekmaven is a glorious beacon of light Tekmaven is a glorious beacon of light Tekmaven is a glorious beacon of light 
Solved Threads: 28
Moderator
Tekmaven's Avatar
Tekmaven Tekmaven is offline Offline
The C# Man, Myth, Legend

Re: *strcpy and linked list

 
0
  #2
Sep 20th, 2006
Well, the code you just gave is not C# . It's C/C++...
-Ryan Hoffman

.NET Specialist / Webmaster, Extended64.com.
Please do not email or PM me with support questions. Please direct them to the forums instead.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
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