Run-Time Check Failure #2 - Stack around the variable 't' was corrupted.

I have a char variable, and I am attempting to make it into an int. After searching online, people mentioned I should do the following:
char t;
int time = t - '0';
After doing this I get a Run-Time Check failure. What is the reason for this?
Thanks in advance.
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
  int main() {

	//VARIABLE DECLARATION
	char mode;
	char t; 
	
	FILE *fp; 

	if ((fp = fopen("recording.wav", "r"))==NULL) {
		printf("NO RECORDING EXISTS...\nI WILL RECORD YOUR AUDIO FIRST!\n");
		mode = '1';
	}
	else {
		do {
			printf("Please indicate if you would like to: \nRecord [1]\nPlayback [2]\nUSER CHOICE: ");
			scanf("%s", &mode);
			fclose(fp);
		} while (mode != '1' && mode != '2'); 
	}

	if (mode == '1') {
		do {
			printf("Please enter the duration of the recording [SECONDS]: ");
			scanf("%s", &t);
		} while (isalpha(t)); 
	}
	else {
		do {
			printf("Please indicate the duration of the playback [SECONDS]: ");
			scanf("%s", &t);
		} while (isalpha(t)); 
	}
	
	int time = t - '0'; 

	int size = (FREQUENCY * (int)time); 
	char *data = malloc(sizeof(char)*size);			//allocating memory

	if (mode == '1') {
		printf("Recording..."); 
		
		int user_input = 0; 

		Audio_Recording(data, size); 

		printf("Processing...\r"); 

		printf("Saving...\r"); 
		FILE *fp = fopen("recording.wav", "w"); 
		fwrite(data, sizeof(char), size, fp); 

		fclose(fp); 
	}
	else {
		printf("Playing...\r"); 
		FILE *fp = fopen("recording.wav", "r"); 
		fread(data, sizeof(char), size, fp); 

		fclose(fp); 

		Playback_Recording(data, size); 
	}
	free(data); 

	system("PAUSE"); 
	return 0; 
}
t is a char. It can hold one single character. How many characters are you typing?
@Moschops even when I enter just one character I still get the Run-Time failure.
When you enter one character, two characters then get written into the memory, because when you write to a memory location using scanf and the %s format specifier, a terminating zero value is added. You are trying to store two characters in a single char variable. This is bad.

The bad solution here is for you to make t an array of char, rather than a single char.

The better solution is to use C++ strings and C++ IO (e.g. cin ).
These look highly suspicious. scanf("%s", &mode); scanf("%s", &t);
mode and t are char but you try to read a string into it.
One more thing, for C Programmers

The scanf family of functions returns a value, so one should check it, just to see that it has worked.

C11 has scanf_s which is supposed to be safer (one has to specify the size)

http://en.cppreference.com/w/c/io/fscanf

edit;

Another thing, please don't start a new topic about the same subject and double posting questions. You could be reported and your account restricted. The reason for this is that it is ultimately a time waster for us, the same advice is given for both topics.
Last edited on
In above code, the file is opened using fopen() in text mode.
For a wav file you should open it in binary mode.


In order to open a file as a binary file, a "b" character has to be included in the mode string. This additional "b" character can either be appended at the end of the string (thus making the following compound modes: "rb", "wb"

http://www.cplusplus.com/reference/cstdio/fopen/


Topic archived. No new replies allowed.