Вопрос

Thank you for all advices, I solved the problem and it is function call overflow. Yes, the code is really a mass and ugly so I managed to clean it up, I will replace the code with the new one here.

This is my final year project, I am trying to write a c program that reads multiple .c codes and modifying it, then complie and running it and finally reads the ouput.txt and comparing it with ans.txt.

I am encountering a weird problem, the code complies with no warning, but when I tried to run it at command line, it does nothing but shows a empty line.

After few testing, I find that if I comment one line of code, that is a call of fread function, the program will works fine. I do not understand the reason and how to solve it.

when the program works, it should be like this in command line:
C:\batch.exe
Welcome to the batch system
(Starts processing)...

However, I am getting this now:
C:\batch.exe

C:\

here is my the main part of my code:

int main(){
int agu = 1, fim;   // agument for controlling the program
int filesize[256];         //used on sacn()
int num = 0;                    // stores total number of files detected         
char sdir[] = "./input/", filter[]= "*.c", filename[128][256];       //Varibies used on auto-detecting
struct _finddata_t c_file;                   //Varibies used on auto-detecting
long hFile;                                  //Varibies used on auto-detecting
printf("\nWelcome to the my system");
Sleep(1000);
printf("\nPlease put all files on input folder in order to let this program read it");
Sleep(1000);
fflush(stdout);
do{                       
    _chdir(sdir);
    hFile = _findfirst(filter, &c_file);
    if (hFile != -1)
    {
        num = 0;
        do{
            sprintf(filename[num], "%s", c_file.name);
            printf("\n%s\n", filename[num]);
            num++;
        } while (_findnext(hFile, &c_file) == 0);

    }
    else{
        printf("\nNo .c files found in input folder.\nThe Program will be exit after 5 second.");
        Sleep(5000);
        return 0;
    }
    _chdir("..");
    printf("\nTotal %d file(s) will be imported", num);
    printf("\nConfirm ? (Y / N)\nYou Can Also Press Ctrl + C or Enter Q to Terminate this Program.");
    do {
        fim = getchar();
        putchar(fim);
        if (fim == 'Y' || fim == 'y')
            agu = 0;
        else if (fim == 'Q' || fim == 'q')
            return 0;
        else agu = 1;
    } while (fim != 'y' && fim != 'Y' && fim != 'N' && fim != 'n');


}while(agu==1);
mod(filename,sdir,num);  // modify , compile and run the code
printf("\nEnter Y to Start Result Comparing, or Enter N to Exit This Program.(Y/N)");
fim = 'a';
do {
    fim = getchar();
    putchar(fim);
    if (fim == 'Y' || fim == 'y')
        agu = 0;
    else agu = 1;
} while (fim != 'y' && fim != 'Y' && fim != 'N' && fim != 'n');

if (agu == 0){      //Resulting Comparing Function
    comp();
}
else{
    printf("\nThank You For Using This Software, This Program will be terminated.");
}
return 0;
 }
Это было полезно?

Решение

The printf() for filebuff[x] might print lots of (invisible) garbage due to a buffer overflow:

fread() returns the file content without trailing '\0'. Thus you get a buffer overflow, e.g. in the printf() after fread() and later in strlen() and scan(). There are two (or probably more) solutions:

  • Set a null delimiter at the end of the file content immediately after the call to fread():

    filebuff[x][fsize]='\0'
    

    before accessing the buffer as C-string.

  • Make a separate array of file-sizes for each file:

    int filesize[256]; filesize[x] = fsize; 
    

    and use this size later on in scan()

Other problems that caught my eyes:

  • sizeof(char[xxx]) always returns the size of the buffer, not the string-length of its content. Thus

    sizeof(filebuff[x])   // always returns 2048
    sizeof(inputFiles[x]) // always returns 10 
    
  • In function scan():

    if (input[curr] == 'p' && 
        input[curr + 1] == 'r' &&
        input[curr + 2] == 'i' &&
        input[curr + 3] == 'n' &&
        input[curr + 4] == 't' &&
        input[curr + 5] == 'f'
    )
    

    has a potential buffer overflow as well: If you reached nearlly the end of file input[curr + 5] reads past the end of your content. Better use strstr() instead.

Другие советы

stdout is row buffered. If you print stuff without an ending \n you need to flush stdout.

printf("\nWelcome to the batching system");
fflush(stdout);  // Added line
Sleep(1000);

Right now, your program is waiting for input, but you don't realize it is, because the prompt text is still in the print buffer.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top