'fclose()' causing problem in closing a .txt file opened in "w+" mode.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/*
I am scanning the words free of vowels from a dictionary file, and writing
them to a new file 'vowels.txt'. Everything works fine. BUT When it comes
to closing the NewStream (pointer to vowels.txt) by fclose(NewStream);
then a Heap Corruption error occurs. Can anybody tell me, WHY is it
happening?
or
Should I return from the function without 'fclose', as it will automatically close(and that works)?
*/
/*CONTENT OF CODE*/
void Fread_to_srch_words_w_o_VOWELS(FILE **stream)
{
	if (*stream == NULL)
	{
		cout<<"File pointer is not passed correctly"<<endl;
		system("pause");
		return;
	}
	else cout<<"Ready to GO"<<endl;
/* Initializations ------------------------------*/
	char vowels [] = {"AEIOU"};
	char *word_read = new char[];
	bool wrt_2_file = false;
	bool CONTINUE = true;
	char user_opt = '\0';
	int word_found = 0;
	int numread= 0;
	int vwl_read = 0;
	char *pvwl_read = NULL;
	int word_read_len = 0;
	FILE * NewStream = NULL;
/*	----------------------------------------------*/
	wrt_2_file = Is_Write(&NewStream);
	while(!feof(*stream))
	{
	//	system("cls");
		if(ferror(*stream))
		{
			cout<<"Read Error";
			clearerr(*stream);
			break;
		}
		else
		{
			if(!feof(*stream))
			{
		/*		cout<<"Reading File"<<endl; ...................*/
				fgets (word_read, 50 , *stream);
				if (word_read != NULL )
				{
					numread++;
					word_read_len = strlen(word_read) - 2;
					strupr(word_read);
					pvwl_read = strpbrk (word_read, vowels);
		/* ................ If NO VOWEL found .................................*/
					if(pvwl_read == NULL)
					{
						switch (wrt_2_file)
						{
						case true:
		/*	WRITING THESE WORDS TO NEW FILE, 'ONLY' on User's DEMAND ----------*/
							fwrite (word_read , sizeof(char), strlen(word_read), NewStream);
							cout<<"Word Found # "<<++word_found<<": \t"<<word_read<<endl;
							if (word_found % 10 == 0)	// 10 at a time	
							{
								CONTINUE = Is_Continue();
							}
							break;
						default:
		/*	Display Words with order number ------------------------*/
							cout<<"Word Found # "<<++word_found<<": \t"<<word_read<<endl;
							if (word_found % 10 == 0)	// 10 at a time	
							{
								CONTINUE = Is_Continue();
							}
							break;
						}
					}			
			*word_read = NULL; //	cout<<"Word pointer CLEARED, NOW it is = "<<word_read<<endl;
				}
			}
		}
		if (!CONTINUE)
		{
			break;
		}
	}
	if (wrt_2_file )
	{
	 fclose (NewStream); /* This line CRASHES */
	}
	system("pause");
}
Last edited on
It would help if you formatted your code.

Now that I've formatted it, you don't show all the relevant code. So there's not much I can do with what you've posted.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/*
  I am scanning the words free of vowels from a dictionary file, and writing them to a new file 'vowels.txt'.
  Everything works fine. BUT When it comes to closing the NewStream (pointer to vowels.txt) by fclose(NewStream);
  then a Heap Corruption error occurs. Can anybody tell me, WHY is it happening?
*/

/*CONTENT OF CODE*/
void Fread_to_srch_words_w_o_VOWELS(FILE **stream)
{
	if (*stream == NULL)
	{
		cout<<"File pointer is not passed correctly"<<endl;
		system("pause");
		return;
	}
	else cout<<"Ready to GO"<<endl;

	/* Initializations ------------------------------*/
	char vowels [] = {"AEIOU"};
	char *word_read = new char[];
	bool wrt_2_file = false;
	bool CONTINUE = true;
	char user_opt = '\0';
	int word_found = 0;
	int numread= 0;
	int vwl_read = 0;
	char *pvwl_read = NULL;
	int word_read_len = 0;
	FILE * NewStream = NULL;

	/* ----------------------------------------------*/
	wrt_2_file = Is_Write(&NewStream);
	while (!feof(*stream))
	{
		// system("cls");
		if(ferror(*stream))
		{
			cout<<"Read Error";
			clearerr(*stream);
			break;
		}
		else
		{
			if(!feof(*stream))
			{
				/* cout<<"Reading File"<<endl; ...................*/
				fgets (word_read, 50 , *stream);
				if (word_read != NULL )
				{
					numread++;
					word_read_len = strlen(word_read) - 2;
					strupr(word_read);
					pvwl_read = strpbrk (word_read, vowels);
					/* ................ If NO VOWEL found .................................*/
					if(pvwl_read == NULL)
					{
						switch (wrt_2_file)
						{
						case true:
							/* WRITING THESE WORDS TO NEW FILE, 'ONLY' on User's DEMAND ----------*/
							fwrite (word_read , sizeof(char), strlen(word_read), NewStream);
							cout<<"Word Found # "<<++word_found<<": \t"<<word_read<<endl;
							if (word_found % 10 == 0) // 10 at a time
							{
								CONTINUE = Is_Continue();
							}
						break;
						default:
							/* Display Words with order number ------------------------*/
							cout<<"Word Found # "<<++word_found<<": \t"<<word_read<<endl;
							if (word_found % 10 == 0) // 10 at a time
							{
							CONTINUE = Is_Continue();
							}
							break;
						}
					}
					*word_read = NULL; // cout<<"Word pointer CLEARED, NOW it is = "<<word_read<<endl;
				}
			}
		}
		if (!CONTINUE)
		{
			break;
		}
	}
	if (wrt_2_file)
	{
		fclose (NewStream); /* This line CRASHES */
	}
	system("pause");
}
Check NewStream variable for NULL before calling fclose on it.
 
	char *word_read = new char[];

This allocates ... a zero-length array? And this:
 
fgets (word_read, 50 , *stream);

reads 50 characters into it. So you're corrupting the heap.

It looks like you're expecting that new char[] will allocate a variable length array that will expand automatically when you read characters into it. fgets() doesn't work that way. The buffer must contain enough space for the maximum number of characters that can be read.
@modoran
Thanks, But I have checked NewStream for NULL.
@dhayden
Thanks a lot, it really helped.
Topic archived. No new replies allowed.