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)

Well, the code you just gave is not C# ;). It's C/C++...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.