【问题标题】:Why is my program suddenly terminating before executing scanf()?为什么我的程序在执行 scanf() 之前突然终止?
【发布时间】:2019-10-28 02:02:00
【问题描述】:

我正在尝试编写代码来替换文本文件中的一行。它编译成功,但是一旦尝试扫描要替换的行号,它就会突然终止。

我真的不知道我做错了什么。我也试过 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;
}

【问题讨论】:

  • newLine 输入了多少个字符?最多可以使用255 字符。
  • 无法复制。请显示从终端剪切和粘贴的程序示例输入文件和输入/输出。
  • 新行文本是否包含空格?
  • 它不会突然终止。它根据您的输入执行while循环并成功退出。在scanf() 之后,没有printf() 可以在控制台上打印任何消息/输出。您在执行程序后检查过temp.txt 吗?尽管您的程序缺少错误处理,但对于有效输入,它应该可以工作。
  • 您需要显示temp.txt 的最小样本以及您提供的重现问题的输入。 Edit你的问题。

标签: c scanf terminate


【解决方案1】:

以下建议的代码:

  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;
}

【讨论】:

  • 非常感谢!我不知道如何使用stderr,现在我清楚了。
  • 注意:我做了一些小的修改,所以请确保您使用我的答案的最新版本
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-31
  • 1970-01-01
相关资源
最近更新 更多