File and String Program Not Running, in plain C lang

Hi my code is not running so I ask that someone test this for me and also review if my logic and syntax is correct.

The assignment is to write a program that statistically computes similarity of C syntax with another program; a same and a different. The one used here is in C language, it's called Battleship.cpp. The program must open a file and read line by line for keywords and then produce statistics.

The reason my code is not running is the fopen function is failing and it goes to return -1. I am using MS Visual Studio 2013 and there are no compiler errors after turning off deprecation. I do see, however, this error UMEngx86.dll'. Cannot find or open the PDB file. The file being opened is in my source folder.

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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
  #include <stdlib.h>
#include <stdio.h>
#include <string.h>

main() {
	char line[100];
	char *eof;
	const char lines, blank = " ", comment1 = "//", comment2 = "/*", ints = "int", longs = "long", floats = "float", doubles = "double", chars = "char", ifs = "if", elses = "else", fors = "for", switches = "swtich", semicolons = ";", structs = "struct", arrays = "[", blocks = "{";
	int totalBlanks, totalComments, totalComment1, totalComment2, totalInts, totalLongs, totalFloats, totalDoubles, totalChars, totalIfs, totalElses, totalFors, totalSwitches, totalSemicolons, totalStructs, totalArrays, totalBlocks;

	FILE* inFile = NULL;
	const char *p = inFile;

	printf("Opening file Battle_ship.txt");

		inFile = fopen(inFile,"Battle_ship.cpp", "r");
	if (inFile == NULL) {
		printf("Could not open file Battle_ship.cpp. \n");
		return -1;
	}
	fgets(line, 100, inFile);
	while (!feof(inFile)) {
		fgets(line, 100, inFile);
	}
	while ((p = strstr(p, blank)) != NULL) {
		p += strlen(" ");
		totalBlanks++;
	}
	while ((p = strstr(p, comment1)) != NULL) {
		p += strlen("//");
		totalComment1++;
	}
	while ((p = strstr(p, comment2)) != NULL) {
		p += strlen("/*");
		totalComment2++;
	}
	while ((inFile = strstr(p, ints)) != NULL) {
		p += strlen("int");
		totalInts++;
	}
	while ((p = strstr(p, longs)) != NULL) {
		p += strlen("long");
		totalLongs++;
	}
	while ((p = strstr(p, floats)) != NULL) {
		p += strlen("float");
		totalFloats++;
	}
	while ((p = strstr(p, doubles)) != NULL) {
		p += strlen("double");
		totalDoubles++;
	}
	while ((p = strstr(p, chars)) != NULL) {
		p += strlen("char");
		totalBlanks++;
	}
	while ((p = strstr(p, ifs)) != NULL) {
		p += strlen("if");
		totalIfs++;
	}
	while ((p = strstr(p, elses)) != NULL) {
		p += strlen("else");
		totalElses++;
	}
	while ((p = strstr(p, fors)) != NULL) {
		p += strlen("for");
		totalFors++;
	}
	while ((p = strstr(p, switches)) != NULL) {
		p += strlen("switch");
		totalSwitches++;
	}
	while ((p = strstr(p, semicolons)) != NULL) {
		p += strlen(";");
		totalSemicolons++;
	}
	while ((p = strstr(p, structs)) != NULL) {
		p += strlen("struct");
		totalStructs++;
	}
	while ((p = strstr(p, arrays)) != NULL) {
		p += strlen("[");
		totalArrays++;
	}
	while ((p = strstr(p, blocks)) != NULL) {
		p += strlen("[");
		totalBlocks++;
	}
	totalComments = totalComment1 + totalComment2;
	totalArrays /= 2;
	totalBlocks /= 2;
	printf("%d occurences of %c\n", lines, line);
	printf("%d occurences of %c\n", totalBlanks, blank);
	printf("%d occurences of total comments", totalComments);
	printf("%d occurences of %c\n", totalInts, ints );
	printf("%d occurences of %c\n", totalLongs, longs);
	printf("%d occurences of %c\n", totalFloats, floats );
	printf("%d occurences of %c\n", totalDoubles, doubles);
	printf("%d occurences of %c\n", totalChars, chars);
	printf("%d occurences of %c\n", totalIfs, ifs);
	printf("%d occurences of %c\n", totalElses, elses);
	printf("%d occurences of %c\n", totalFors, fors);
	printf("%d occurences of %c\n", totalSwitches, switches);
	printf("%d occurences of %c\n", totalSemicolons, semicolons);
	printf("%d occurences of %c\n", totalStructs, structs);
	printf("%d occurences of %c\n", totalArrays, arrays);
	
	}
First
On line 14: The name of the file is "Battle_ship.txt". Are you sure that you want the unusual name "Battle_ship.cpp"?

Second
Since you use a relative path: Is the file actually where the program expect it? For debugging you might use an absolute path like "C:\\test\\Battle_ship.cpp" or so.

Third
Lines 21 to 24: You're reading all lines from the file into one buffer. That means: After reading from the file you process only the very last line.
Topic archived. No new replies allowed.