DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   C# (http://www.daniweb.com/forums/forum61.html)
-   -   *strcpy and linked list (http://www.daniweb.com/forums/thread55572.html)

Line Sep 19th, 2006 8:10 pm
*strcpy and linked list
 
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:
struct lenket{
        struct lenket *neste;
        char *dir;
};

struct lenket *liste = NULL, *siste = NULL, *pdp, *q;

int main(int argc, char **argv)
{
                FILE *fp;
                char *everything;
                unsigned int filesize;
                char *p1, *p2;
                char *filnavn = argv[2];

                if ((fp = fopen(filnavn,"r")) != NULL) {
                        printf("Filen %s er åpnet.\n\n", filnavn);
                        fseek(fp, 0, SEEK_END);
                        filesize = ftell(fp);
                        p1=p2 = everything = malloc(sizeof(char) * filesize);
                        fseek(fp, 0, SEEK_SET);
                        fread(everything, 1, filesize+1, fp);
                        everything[filesize] = 0;
       
                        while(p1-everything < filesize){
                                if(*p1 =='\n'|| p1-everything == filesize-1 ){
                                        if(*p1==0){
                                                break;
                                        }

                                        *p1='\0';
                                        pdp=malloc(sizeof(struct lenket));
                                        pdp->neste=NULL;
                                        pdp->dir=malloc(p1-p2+1);
                                        strncpy(pdp->dir, p2, p1-p2);
                                        if(liste==NULL){
                                                liste=siste=pdp;
                                        }else{
                                                siste->neste=pdp;
                                                siste=pdp;
                                        }
                                        p2=p1+1;
                                }else{
                                        ++p1;
                                }
                        }
                        pdp=liste;

char *teksten = NULL;
                                teksten=malloc(sizeof(teksten)+1);

                                do {
                                        *strcpy(teksten, pdp->dir);
                                        printf("%s\n", pdp->dir);
                                } while((pdp=pdp->neste) != NULL);

All help is appreciated! :o)

Tekmaven Sep 20th, 2006 3:54 pm
Re: *strcpy and linked list
 
Well, the code you just gave is not C# ;). It's C/C++...


All times are GMT -4. The time now is 9:20 am.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC