Thread: Copy .dat file to dynamic memory...

  1. #1
    Registered User IndioDoido's Avatar
    Join Date
    Apr 2007
    Posts
    194

    Thumbs up Copy .dat file to dynamic memory...

    hi

    i'm trying to copy the data i have in a .dat file to a program's dynamic memory.
    i've manage to do the opposite (memory to .dat), but know i can't load the prog's memory with that data.

    Code:
    void carregarDADOS(pno lista[3]) 
    {
    	//main struct
                    pno aux = NULL;
    	pno novo = NULL;
    	no temp;
    
                    //folder struct
    	pnodir auxD = NULL;
    	pnodir novoD = NULL;
    	nodir tempD;
    
                    //file struct
    	pnofich auxF = NULL;
    	pnofich novoF = NULL;
    	nofich tempF;
    
    	int i=0;
    
    	FILE *b = fopen("dataBETA.dat", "rb");
    
    	if(b!=NULL)
    	{
    		while(fread(&temp,sizeof(no),1,b) > 0)
    		{
    			novo = (pno)malloc(sizeof(no));
    			*novo = temp;
    			novo->prox = NULL;
    			if(lista[0] == NULL)
    			{
    				lista[0] = novo;
    				while(fread(&tempD,sizeof(nodir),1,b) > 0)
    				{
    					novoD = (pnodir)malloc(sizeof(nodir));
    					*novoD = tempD;
    					novoD->prox = NULL;
    					if(lista[0]->dir == NULL)
    					{
    						lista[0]->dir = novoD;
    						while(fread(&tempF,sizeof(nofich),1,b) > 0)
    						{
    							novoF = (pnofich)malloc(sizeof(nofich));
    							*novoF = tempF;
    							novoF->prox = NULL;
    							if(lista[0]->dir->fich == NULL)
    								lista[0]->dir->fich = novoF;
    							else
    							{
    								auxF=lista[0]->dir->fich;
    								while(auxF->prox!=NULL)
    									auxF=auxF->prox;
    								auxF->prox = novoF;
    							}
    
    						}
    					}
    					else
    					{
    						auxD=lista[0]->dir;
    						while(auxD->prox!=NULL)
    							auxD=auxD->prox;
    						auxD->prox = novoD;
    					}
    				}
    
    			}
    			else
    			{
    				aux=lista[0];
    				while(aux->prox!=NULL)
    					aux=aux->prox;
    				aux->prox = novo;
    				while(fread(&tempD,sizeof(nodir),1,b) > 0)
    				{
    					novoD = (pnodir)malloc(sizeof(nodir));
    					*novoD = tempD;
    					novoD->prox = NULL;
    					if(lista[0]->dir == NULL)
    					{
    						lista[0]->dir = novoD;
    						while(fread(&tempF,sizeof(nofich),1,b) > 0)
    						{
    							novoF = (pnofich)malloc(sizeof(nofich));
    							*novoF = tempF;
    							novoF->prox = NULL;
    							if(lista[0]->dir->fich == NULL)
    								lista[0]->dir->fich = novoF;
    							else
    							{
    								auxF=lista[0]->dir->fich;
    								while(auxF->prox!=NULL)
    									auxF=auxF->prox;
    								auxF->prox = novoF;
    							}
    
    						}
    					}
    					else
    					{
    						auxD=lista[0]->dir;
    						while(auxD->prox!=NULL)
    							auxD=auxD->prox;
    						auxD->prox = novoD;
    					}
    				}
    
    			}
    		}
    	}
    	fclose(b);
    }
    Any hint??
    "Artificial Intelligence usually beats natural stupidity."

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Yeah, don't have nested fread() calls, and separate the "read from a file" and "append to a list" functionality.

    This function is simply too long and too deeply nested to make any sense of.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User IndioDoido's Avatar
    Join Date
    Apr 2007
    Posts
    194
    hey Salem!

    solved the problem

    but now i have a new issue...paths.
    in the code im not using any path for the files, i just put their name ("bla.dat") and when i debug and run it works nicely.
    but, then i open the .exe file and run my prog it crashes beacause it can't find the files

    which path should i use so that the debug and the .exe file both work?

    another question, how can i calculate the size in bytes of a .dat file?
    Last edited by IndioDoido; 05-27-2007 at 04:26 PM.
    "Artificial Intelligence usually beats natural stupidity."

  4. #4
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    If you want to count the amount of bytes, move the pointer to the end of the file and determine it's position (which is an offset in bytes from the start), i.e. use fseek() and ftell()... Not really efficient, but it is portable.

    Also don't attempt to close 'b' if you didn't open it... I would suggest:

    Code:
    FILE * b;
    
    if((b = fopen("blah.dat", "rb")) == NULL)
    {
        perror("Failed to open file");
        return 1;
    }
    Also:
    * Don't cast malloc (See the FAQ)
    * Check if the memory allocation fails before proceeding, eg:

    Code:
    if((ptr = malloc(size)) == NULL)
        perror("Malloc failed");
    else{
        /* Didn't fail... */
    }

  5. #5
    Registered User IndioDoido's Avatar
    Join Date
    Apr 2007
    Posts
    194
    thx zacs7...
    i've used fseek() and ftell() and it works nice ;-)

    can you help me on this one:

    in the code im not using any path for the files, i just put their name ("bla.dat") and when i debug and run it works nicely.
    but, then i open the .exe file and run my prog it crashes beacause it can't find the files

    which path should i use so that the debug and the .exe file both work?
    "Artificial Intelligence usually beats natural stupidity."

  6. #6
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    If you don't use a path, just make sure that bla.dat is in the same directory as your executable.
    It probably shouldn't crash just because it can't find a file. If the file handle from fopen() is NULL, print an error and abort, or something.
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie homework help
    By fossage in forum C Programming
    Replies: 3
    Last Post: 04-30-2009, 04:27 PM
  2. Inventory records
    By jsbeckton in forum C Programming
    Replies: 23
    Last Post: 06-28-2007, 04:14 AM
  3. Writing input from a file into shared memory
    By RazielX in forum C Programming
    Replies: 2
    Last Post: 09-23-2004, 12:34 PM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. Manipulating the Windows Clipboard
    By Johno in forum Windows Programming
    Replies: 2
    Last Post: 10-01-2002, 09:37 AM