I'm reading a file in C, char by char but I need to get 2 consecutive chars. Please, can someone suggest me some options?
the main idea of the program is to read a source C file and to find the number of all significant chars (ignore ws) and to ignore '*/' & '/*'. I'm trying to write the program in really basic level because it is for course work for friend of mine. In general I'm PHP programmer.
This is the full code I managed to write.
#include <stdio.h>
#include <string.h>
int in_array(char *array, char searchedEl);
int main(){
FILE *fp;
char readChar;
char exeptChars[] = {'\n', '\t','\0'};
int charsCounter = 0;
char *openComment = "/*";
char *closeComment = "*/";
char tempHolder[2];
int j=0;
fp=fopen("testrw.txt","r");
if(!fp){
printf( "Could not open file\n" );
return 1;
}
while( (readChar = fgetc(fp)) != EOF ){
if((readChar == '*') || (readChar == '/') || (readChar == '\\')){
if(j < 1){
tempHolder[j] = readChar;
j++;
continue;
}
else{
tempHolder[j] = readChar;
if(strcmp(tempHolder, openComment) || strcmp(tempHolder, closeComment)){
charsCounter += 2;
}
tempHolder[2]='\0';
j=0;
continue;
}
}
if(!in_array(exeptChars, readChar)){
printf("Permited char: %c\n", readChar);
charsCounter++;
}
}
printf("Characters %d\n", charsCounter+1);
return 0;
}
int in_array(char *array, char searchedEl){
char tempEl;
int i=0;
while(tempEl = array[i]){
if(tempEl == searchedEl){
return 1;
}
i++;
}
return 0;
}