Copy a word at a time from txt file

Discussion in 'C' started by jefcoatv, Nov 15, 2010.

  1. jefcoatv

    jefcoatv New Member

    Joined:
    Oct 1, 2010
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    I am trying to copy a word at a time from text file to an array. I am using malloc to allocate memory to a dynamic array. Then copying the file to the Dynamic array.

    Then copying the Dynamic array into a structure of arrays to hold the individual words.

    The dynamic array is giving me a some type of line feed at the bottom and my word count function shuts down when it encounters these.

    I get an error:

    Debug Assertion Failed

    Expression: (unsigned)(c+1)<=256

    Here is my Code:

    Code:
    #include <stdio.h>
    #include<stdlib.h>
    #include <ctype.h>
    #include <string.h>
    
    #define MAXWORDS 1000
    
    typedef struct
    {
    	char Word[88];
    }UniqueWords;
    
    int BytesInLocation(char *FilePath);
    char *GetWord(char *Source, char *Dest);
    
    
    int main(void)
    {
    	char *Location = "C:\\Users\\svjefcoat\\Documents\\Visual Studio 2010\\Projects\\assignment 10\\getty.txt";
    	int BytesInLocation=BytesInFile(Location);
    	UniqueWords Word[MAXWORDS];
    	char *pDArray;
    	char *Read;
    	UniqueWords *pList;
    	FILE *pStor1;
    
    
    
    
    	pDArray = malloc(BytesInLocation +1);
    
    	pStor1 = fopen(Location, "r");
    			
    	fread(pDArray, sizeof(char), BytesInLocation, pStor1);
    	pDArray[BytesInLocation] = '\0';
    
    	fclose(pStor1);
    
    
    		
    	Read = pDArray;
    
    	pList = Word;
    	for(;;)
    	{
    		Read = GetWord(Read, pList->Word);
    		
    			pList++;
    	}
    
    	free(pDArray);
    	
    			
    	getchar();
    
    	return 0;
    
    
    }
    
    
    
    int BytesInFile(char *FilePath)
    {
    	FILE *pStor;
    	int Mark;
    
    	pStor = fopen(FilePath, "r");
    
    	fseek(pStor, 0, SEEK_END);
    	Mark = ftell(pStor);
    
    	fclose(pStor);
    
    	return Mark;
    }
    
    char *GetWord(char *Source, char *Dest)
    {
    	while(*Source)
    	{
    		if((isspace(*Source))||(ispunct(*Source)))
    		{
    			Source++;
    			continue;
    		}
    		else
    			break;
    	}
    
    	while(*Source)
    	{
    		if(isspace(*Source)||(ispunct(*Source)))
    		{
    			*Dest = '\0';
    			return Source;
    		}
    		*Dest++ = *Source++;
    	}
    	*Dest = '\0';
    	return Source;
    }
    
    Any suggestions on dealing with these double lines/line feed?
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice