Question

I have this simple C program that reads characters from the standard input and displays a table with numbered rows and columns inside which are the characters we wrote. The code is:

#include <stdio.h>

#define COLS 6
#define WIDTH 5

int main(int argc, char *argv[]){

char buffer[COLS]={0};

int c;
int cols;
for(cols=0;cols<COLS && (c=getchar())!=EOF;cols++){
    buffer[cols]=c;
}
if(cols!=0){
    int a;
    printf("%-*s",WIDTH,"");
    for(a=0;a<cols;a++){
        (cols!=(a+1)) ? printf("%-*d",WIDTH,(a+1)) : printf("%-*d\n",WIDTH,(a+1));
    }
    printf("%-*d",WIDTH,1);
    for(a=0;a<cols;a++){
        (cols!=(a+1)) ? printf("%-*c",WIDTH,buffer[a]) : printf("%-*c\n",WIDTH,buffer[a]);
    }

    cols=0;
    int rows=2;
    while((c=getchar())!=EOF){
        if(cols==0){
            printf("%-*d%-*c",WIDTH,rows,WIDTH,c);
            cols++;
        }
        else{
            if(COLS!=(cols+1)){
                printf("%-*c%",WIDTH,c);
                cols++;
            }
            else{
                printf("%-*c\n",WIDTH,c);
                cols=0;
                rows++;
            }
        }
    }
    printf("End-Of-File\n");
}
else{
    printf("No valid characters input\n");
}


return 0;
}

I compile the program with cc on Windows XP and if, in Windows console (cmd), I execute it with:

program.exe

then it reads the characters I input with the keyboard. It works fine. The problem occurs when I decide that the characters are to be read from a txt file. So I create a txt file with notepad (in the same folder of program.exe) and I write a couple of sentences inside it. Then in console I execute:

program.exe<file.txt

but the output that I receive is "No valid characters input" as if the very first character read by the program was EOF.

Any ideas?

Thank you.

SOLUTION: I've just found out what the problem was. It was Comodo Internet Security that was preventing the program from reading characters from txt file. Actually Comodo was sandboxing the program. So after putting the program inside the "Safe Files" list everything worked like it should. I hope this solution will help anyone with a similar problem.

Was it helpful?

Solution

SOLUTION: I've just found out what the problem was. It was Comodo Internet Security that was preventing the program from reading characters from txt file. Actually Comodo was sandboxing the program. So after putting the program inside the "Safe Files" list everything worked like it should.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top