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!

Dani AI

Generated

Most likely cause: your code advances the strtok pointer and then uses it without checking for NULL. Calling any function (sscanf, strcpy, etc.) with a NULL pointer will crash. Also the loop increments i even when no token was copied, and there are no bounds checks on strArr, so a short input can still trigger undefined behavior.

Use a defensive token-copy pattern: get the token, copy it into your storage, then advance to the next token. Always check for NULL and clamp lengths to avoid buffer overflow. Example (different from the samples already posted):

#define MAX_WORDS 100
#define MAX_WORD_LEN 99

char line[256];
char words[MAX_WORDS][MAX_WORD_LEN + 1];
int n = 0;

while (fgets(line, sizeof line, rfPtr)) {
    char *tok = strtok(line, " .\n");
    while (tok && n < MAX_WORDS) {
        size_t L = strlen(tok);
        if (L > MAX_WORD_LEN) L = MAX_WORD_LEN;
        memcpy(words[n], tok, L);
        words[n][L] = '\0';
        ++n;
        tok = strtok(NULL, " .\n");
    }
}

Practical tips:

  • Read the filename safely (use fgets and strip the newline) or use scanf("%99s", filename) to avoid overflow.
  • Check every fopen return before using the FILE*. If you open an output file, choose "w" to overwrite or "a" to append deliberately; the mode itself won’t cause an access violation, but using NULL file pointers will.
  • Don’t use feof() to control reading; while (fgets(...)) is correct.
  • Compile with warnings enabled (-Wall) and run under a debugger (or valgrind) to see the exact failing call and pointer value.

Regarding earlier replies: is on the right track about copying the token before moving on; ’s note on append vs overwrite depends on intent; is correct that fopen mode doesn’t explain the read-access crash. Guard token use, clamp lengths, and the crash should disappear.

Recommended Answers

All 3 Replies

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.