Created
January 18, 2020 10:28
-
-
Save chaidhat/1a65bc332f777105cffe4a49bacd16db to your computer and use it in GitHub Desktop.
Counts all words excluding double spaces
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <string.h> | |
// dir /b /a-d | |
int main() { | |
FILE *fp; | |
const char *filetree = "filetree.txt"; | |
fp = fopen(filetree, "r"); | |
char c; | |
char cstring[2] = "a"; | |
char fstring[2048]; | |
strcpy(fstring, ""); | |
int filenameSz = 0; | |
char filenames[511][2048]; // first double array | |
while (!feof(fp)) | |
{ | |
c = fgetc(fp); | |
if (c == '\n' || c == '\0') | |
{ | |
// get rid of -1 files | |
int i = 0; | |
while (fstring[i] != '.') | |
i++; | |
if (fstring[i-2] == '-' && fstring[i-1] == '1') | |
{ | |
} | |
else | |
{ | |
strcpy(filenames[filenameSz], fstring); | |
filenameSz++; | |
} | |
strcpy(fstring, ""); | |
} | |
else | |
{ | |
cstring[0] = c; | |
strcat(fstring, cstring); | |
} | |
} | |
strcpy(filenames[filenameSz], fstring); | |
printf("files to read: %d\n", filenameSz); | |
const char *filedir = "dir/"; | |
char filename[2048]; | |
int totalWordcount = 0; | |
for (int i = 0; i < filenameSz; i++) | |
{ | |
strcpy(filename, filedir); | |
strcat(filename, filenames[i]); | |
fp = fopen(filename, "r"); | |
int wordcount = 1; | |
char lastSpace = 0; | |
while (!feof(fp)) | |
{ | |
c = fgetc(fp); | |
if (c == ' ' && lastSpace == 0) | |
{ | |
wordcount++; | |
lastSpace = 1; | |
} | |
else | |
{ | |
lastSpace = 0; | |
} | |
} | |
printf("filename:%s", filename); | |
// correct indentation | |
int filenameLength = 0; | |
while (filename[filenameLength] != '\0') | |
filenameLength++; | |
for (int j = filenameLength; j < 100; j++) | |
printf(" "); | |
printf("wc: %d\n", wordcount); | |
totalWordcount += wordcount; | |
} | |
printf("summary wc: %d\n", totalWordcount); | |
fclose(fp); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment