I am trying to store all of the tokens in an array. But after it store racecar in the array i get an access violation,Unhandled exception at 0x6515f8e0 (msvcr90d.dll) in Assignment2.exe: 0xC0000005: Access violation reading location 0x00000000.

Here is the file im reading from:
Bob went to town in a racecar.
Hannah plays with her mom outside.
Jill likes to play battleship with a radar.
Dad had his life saved by a reviver.
The rotator makes a helicopter blade spin.
We went down the river in a kayak.
My new car is a honda civic.
Nan took a peep at the rotor and he made a toot.

and here is my code

char displayFile(char txt[])
{
		
	
	char filename[100];
	char *toks;
	int i=0;
	int j=0;
	char strArr[100][100];

	FILE *rfPtr;
	FILE *cfPtr;

	printf("Please enter a file to read for palindromes.\n");
	scanf("%s",filename);

rfPtr=fopen(filename,"r");
cfPtr=fopen("d:\\output.txt","w");

	if (rfPtr==NULL||cfPtr==NULL)
	{
			printf("File cannot be opened!\n");
	}
	else
	{
		//while(!feof(rfPtr)){

			
	while(fgets(txt,200,rfPtr)!=NULL){
		toks=strtok(txt," .\n");
		while(toks!=NULL){
			fprintf(cfPtr,"%s\n",toks);
			
			toks=strtok(NULL," .\n");
			sscanf(toks, "%s", strArr[i]);
			

			i++;


		}
	}
			
		
	for(j=0;j<i;j++){
			printf("%s",strArr[j]);
	}
		
		
		
		
	
	}
	fclose(rfPtr);
	fclose(cfPtr);

	



	return txt;



}

Any help would be appreciated!

Your main problem is that you need to do the sscanf before you do the strtok(NULL,"..."). Couple other problems: no use passing in txt to use as local variable; return type doesn't match what you're trying to return; renamed a couple variables. And I changed sscanf to strcpy; but sscanf was not the problem.

#include <stdio.h>
#include <string.h>

int displayFile (const char* filename)
{
	char strArr[100][100];
	char txt[200];
	char *toks;
	int size = 0;
	int i;
	FILE *rfPtr;

	rfPtr=fopen(filename,"r");

	if (rfPtr==NULL)
	{
		printf("File cannot be opened!\n");
		return -1;
	}

	while(fgets(txt,200,rfPtr)){
		toks=strtok(txt," .\n");
		while(toks){
			strcpy (strArr[size++], toks);
			toks=strtok(NULL," .\n");
		}
	}

	for(i=0;i<size;i++) printf("[%s]\n",strArr[i]);

	fclose(rfPtr);
	return 0;
}

int main() {
	displayFile ("DisplayFile.txt");
}

cfPtr=fopen("d:\\output.txt","w");

What I found is that you should append like this if you want it to write to the output.txt file.

cfPtr=fopen("d:\\output.txt","a");

cfPtr=fopen("d:\\output.txt","w");

What I found is that you should append like this ...

Wrong,

You write when you want to write, append when you want to append.

in any event, it has nothing to do with access violation exceptions, as it only changes where the file pointer is located after the handle is opened.

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.