Thread: Loading a File

  1. #1
    Registered User sean345's Avatar
    Join Date
    Mar 2002
    Posts
    346

    Loading a File

    I want to load a file into a char** array. I am trying to make it so that it can load a file with any sized lines and legnth. I am not sure how to begin. If I use fgets I have to specify a size. Is there another function I can use?

    Also if I create a double dimensioned array using malloc and want to make it larger, is there a way to add more memory onto this array without deleting the contents?

    Thanks

    - Sean
    If cities were built like software is built, the first woodpecker to come along would level civilization.
    Black Frog Studios

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >I am trying to make it so that it can load a file with any sized lines and legnth.
    fgets will read up to the size of the buffer or a newline, if you fill the buffer but don't get the newline, assign what you have to a place in the array and then call fgets repeatedly until you reach the newline. After each call you append the input onto the same string in the array.

    >is there a way to add more memory onto this array without deleting the contents?
    Yes, using realloc you can change the size of the array by creating another one and transferring the contents accordingly.

    -Prelude
    My best code is written with the delete key.

  3. #3
    Registered User lliero's Avatar
    Join Date
    Oct 2001
    Posts
    59

    ya right

    ya. prelude. your right again
    " programming is 1% syntax and 99% logic "

  4. #4
    Registered User sean345's Avatar
    Join Date
    Mar 2002
    Posts
    346
    Ok, I made some code and I get a Windows error when I run the program now. I know it is happening within these functions that load the file.

    Code:
    int LoadFile(char Name[]){
    	int Line = 0;
    	int FileDone = FALSE;
    	FILE *infile = fopen(Name, "r");
    
    	Compiler.Line = Memory(2*sizeof(char*));
    
    	while(FileDone==FALSE){
    		FileDone = GetLine(infile, Line);
    		CleanString(Compiler.Line[Line]);
    		printf("%s\n", Compiler.Line[Line]);
    		Line++;
    		Compiler.Line = GlobalReAlloc(Compiler.Line, (Line+2)*sizeof(char*), 0);
    	}
    
    	Compiler.TotalLines = Line + 1;
    
    	fclose(infile);
    	return SUCCESS;
    }
    
    int static GetLine(FILE *infile, int Line){
    	int Flag = FALSE;
    	int Len = MAX_LINE;
    	char *Temp;
    	char *Base;
    
    	Temp = Memory(Len*sizeof(char));
    
    	while(Flag==FALSE){
    		fgets(Temp, MAX_LINE, infile);
    
    		if(Compiler.Line[Line]!=NULL){
    			Base = Compiler.Line[Line];
    		}
    		else{
    			Base = Memory(MAX_NAME);
    			strcpy(Base, "");
    		}
    
    		Compiler.Line[Line] = Memory((strlen(Temp)+1+Len)*sizeof(char));
    		snprintf(Compiler.Line[Line], strlen(Temp)+Len+1, "%s%s", Base, Temp);
    
    		GlobalFree(Base);
    		Base = NULL;
    
    		if(Temp[strlen(Temp)-1]=='\n'||Temp[strlen(Temp)-1]=='\0'){
    			break;
    		}
    		Len = Len + MAX_LINE;
    		GlobalFree(Temp);
    		Temp = Memory((Len+1)*sizeof(char));
    	}
    
    	GlobalFree(Temp);
    
    	if(feof(infile)!=0){
    		return TRUE;
    	}
    
    	return Flag;
    }
    I think the error is something happening with the snprintf, but I am not sure. Anyone know what is wrong with this?

    - Sean
    If cities were built like software is built, the first woodpecker to come along would level civilization.
    Black Frog Studios

  5. #5
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    A couple of things....

    >FILE *infile = fopen(Name, "r");
    You must check that the file opened correctly before using it. Maybe it didn't and you passed a NULL file pointer to the fgets() call.

    To enable you to see where it's failing, how about doing something simple, like putting in a few printf("Checkpoint 1 reached\n"); type statements. That way you can track the bug better, and pin point the line causing the crash.

    >Compiler.Line = Memory(2 * sizeof(char *));
    What is Compiler.Line? Here you've alloc'ed (I presume) memory for 2 char pointers, is that what you intended?
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  6. #6
    Registered User sean345's Avatar
    Join Date
    Mar 2002
    Posts
    346
    Compiler.Line is a char**. Sorry for not specifying that. I will try the printf's to see how it works. Thanks.

    - Sean
    If cities were built like software is built, the first woodpecker to come along would level civilization.
    Black Frog Studios

  7. #7
    Registered User sean345's Avatar
    Join Date
    Mar 2002
    Posts
    346
    I found the problem. What is happening is that when I use the reallocate command:
    Code:
    Compiler.Line = realloc(Compiler.Line, (Line+1)*sizeof(char*));
    //or
    Compiler.Line = GlobalReAlloc(Compiler.Line, (Line+1)*sizeof(char*), 0);
    it seems to allocate the memory, but when I try to access that part of the char** array I get an illegal operation error. For some reason the array is not realloc correctly. Is this because it is a double pointer? Anyone know what is happening?

    Does anyone have a good example on how to reallocate a char** because it seems to cause an error when I use it.

    - Sean
    Last edited by sean345; 05-09-2002 at 08:21 PM.
    If cities were built like software is built, the first woodpecker to come along would level civilization.
    Black Frog Studios

  8. #8
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Dunno if this will help, but are you doing any error checking on realloc() ? For example:
    Code:
    new_buffer = realloc( buffer, 100 );    
    if( new_buffer == NULL ) 
    {
          /* not able to allocate larger buffer */    
    } 
    else {buffer = new_buffer;}
    You shouldn't use the same pointer as a parameter in realloc(), as you do to store it's return value. If you did, and realloc fails, the return will be NULL, and you'll loose the pointer to the original memory.

    Also, for debugging, you can use %p in printf to see where a pointer to pointing to. Eg
    printf("Pointer is pointing to address %p\", myptr);
    This helps determine if you have NULL or invalid pointers.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  9. #9
    Registered User sean345's Avatar
    Join Date
    Mar 2002
    Posts
    346
    Ok, thanks for the help. I'll try it when I get home.

    One more question, Is there a function that will count the number of occurances of a char in a string?

    - Sean
    Last edited by sean345; 05-10-2002 at 09:58 AM.

  10. #10
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by sean345

    One more question, Is there a function that will count the number of occurances of a char in a string?
    Can't think of a standard one, but it'd be easy to make one yourself. A function prototype might look like this:

    >int CharOccurences(char *arr, int arrlen, char countme);

    Then just loop through the array comparing each char to countme, and return the number found.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  11. #11
    Registered User sean345's Avatar
    Join Date
    Mar 2002
    Posts
    346
    I fixed the problem by not using realloc. What I did was loaded the entire file into a string and counted the number of new line charectors. Then I create that many char* for the line char** array.

    Now my question is how to free the array. This is what I tried but I get a heap error.
    Code:
    for(int i=0;i<TotalLines;i++){
    	GlobalFree(Line[i]);
    }
    
    GlobalFree(Line);
    Line is a char**.

    - Sean
    If cities were built like software is built, the first woodpecker to come along would level civilization.
    Black Frog Studios

  12. #12
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Or you could just do it the easy way.
    Code:
    int array[256] = {0}, c;
    while( (c=fgetc(fp)) != EOF )
    {
        array[c]++;
    }
    printf("There are %d lines in the file.", array['\n'] );
    God I love simplicity.

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. To find the memory leaks without using any tools
    By asadullah in forum C Programming
    Replies: 2
    Last Post: 05-12-2008, 07:54 AM
  2. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 03:44 PM
  3. Replies: 3
    Last Post: 03-04-2005, 02:46 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. archive format
    By Nor in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 08-05-2003, 07:01 PM