Почему моя программа неожиданно завершает работу перед выполнением scanf ()? - PullRequest
1 голос
/ 13 июня 2019

Я пытаюсь написать код для замены строки из текстового файла. Он успешно компилируется, но завершается из ниоткуда, как только пытается отсканировать номер строки, подлежащей замене.

Я действительно понятия не имею, что я делаю неправильно. Я также пытался с fgets (), но он все еще не работает.

#include <stdio.h>
#include <stdlib.h>

#define MAXNAME 30
#define MAXLINE 256

int main(){
    char fileName[MAXNAME];
    FILE *originalFileCheck;

    printf("Input the name of the file to be opened: ");
    scanf("%s", fileName);
    originalFileCheck = fopen(fileName, "r");

    if(originalFileCheck == NULL){
        printf("The file %s was not opened successfully. The program will now terminate.\n", fileName);
        exit(1);
    }
    else{
        FILE *tempFileWrite;
        char tempName[MAXNAME] = "temp.txt";
        tempFileWrite = fopen(tempName, "w");

        char newLine[MAXLINE];
        int lineNum;

        printf("Input the content of the new line: ");
        scanf("%s", newLine);
        printf("Input the number of the line you want to replace: ");
        scanf("%d", &lineNum); /* it terminates WITHOUT scanning this int*/

        char str[MAXLINE];
        int counter = 1;
        while(fgets(str, MAXLINE, originalFileCheck) != NULL){
            if(counter != lineNum){
                for(int i = 0; str[i] != '\0' && str[i] != '\n'; i++){
                    fputc(str[i], tempFileWrite);
                }
                fprintf(tempFileWrite, "\n");
            }
            else{
                 fprintf(newLine, "%s\n", tempFileWrite);
                }
            counter++;
        }

        fclose(tempFileWrite);
        fclose(originalFileCheck);
        ...
        return 0;
}

1 Ответ

0 голосов
/ 13 июня 2019

следующий предложенный код:

  1. чисто компилирует
  2. проверяет ошибки ввода
  3. проверяет наличие ошибок на выходе
  4. выполняет желаемую функциональность
  5. правильно очищается при возникновении ошибки

и теперь предложенный код:

#include <stdio.h>
#include <stdlib.h>

#define MAXNAME 30
#define MAXLINE 256

int main( void )
{
    char fileName[MAXNAME];
    FILE *originalFileCheck;

    printf("Input the name of the file to be opened: ");
    if( !fgets( filename, sizeof( fileName ), stdin) )
    {
        fprintf( stderr, "fgets to input 'original' file name failed\n" );
        exit( EXIT_FAILURE );
    }

    // remove trailing newline
    fileName[ strcspn( fileName, "\n" ) ] = '\0';
    originalFileCheck = fopen( fileName, "r" );
    if( !originalFileCheck )
    {
        perror( "fopen original file for read failed" );
        exit( EXIT_FAILURE );
    }


    FILE *tempFileWrite;
    char tempName[ MAXNAME ] = "temp.txt";

    tempFileWrite = fopen( tempName, "w" );
    if( !tempFileWrite )
    {
        perror( "fopen to write new file failed" );
        fclose( originalFileCheck );
        exit( EXIT_FAILURE );
    }

    char newLine[ MAXLINE ];
    int lineNum;

    printf("Input the content of the new line: ");
    if( !fgets( newLine, sizeof( newLine ), stdin ) )
    {
        perror"fgets to input new line content failed" );
        fclose( originalFileCheck );
        exit( EXIT_FAILURE );
    }

    printf("Input the number of the line you want to replace: ");
    if( scanf("%d", &lineNum) != 1 )
    {
        fprintf( stderr, "scanf for replacement line number failed\n" );
        fclose( originalFileCheck );
        fclose( tempFileWrite );
        exit( EXIT_FAILURE );
    } 

    char str[MAXLINE];
    int counter = 1;
    while( fgets(str, sizeof( str ), originalFileCheck) )
    {
        if(counter != lineNum)
        {
            if( fputs( str, tempFileWrite ) == EOF )
            {
                perror( "fputs for original line failed" );
                fclose( originalFileCheck );
                fclose( tempFileWrite );
                exit( EXIT_FAILURE );
            }
        }

        else
        {
            if( fputs( newLine, tempFileWrite ) == EOF )
            {
                perror( "fputs for replacement line failed" );
                fclose( originalFileCheck );
                fclose( tempFileWrite );
                exit( EXIT_FAILURE );
            }
        }
        counter++;
    }

    fclose(tempFileWrite);
    fclose(originalFileCheck);

    return 0;
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...