gpt4 book ai didi

c - 单独的部分可以正常工作,但是当合并时会导致崩溃

转载 作者:太空宇宙 更新时间:2023-11-03 23:22:13 24 4
gpt4 key购买 nike

该程序在单独编辑时有效,在单独读取时也有效:

    printf("Enter the name of file you wish to edit:\n");
gets(file_name); //file_name = input

file = fopen(file_name,"w"); // write mode

if( file == NULL ) //If file couldn't be opened
{
perror("Error while opening the file!\n");
exit(EXIT_FAILURE);
}

printf("Enter name: \n"); scanf("%s",name1);
printf("Enter second name if applicable: \n"); scanf("%s",name2);
printf("Enter grade: \n"); scanf("%s",grade);
fprintf(file, "%s%s%s\t%s%s%s", name1, " ", name2, "=", " ", grade);
fclose(file);
printf("File write was successful\n");

   printf("Enter the name of file you wish to see:\n");
gets(file_name); //file_name = input

file = fopen(file_name,"r"); // read mode

if( file == NULL ) //If file couldn't be opened
{
perror("Error while opening the file!\n");
exit(EXIT_FAILURE);
}

printf("The contents of %s file are:\n", file_name);

while( ( character = fgetc(file) ) != EOF /*EOF = End Of File*/)
printf("%c",character); //print c (character)

fclose(file); //remove the file from RAM

但是,当它们与 if 放在一起时,只要将 1 或 2 输入到第一部分,程序就会崩溃:

 printf("Edit or Read file? (1 for edit, 2 or read)\n"); scanf("%s",RW);

完整代码如下:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
char character, file_name[25];
int RW;
const char *quit;
FILE *file; //"file" stores file stream
char data [100000];
char name1 [100000];
char name2 [100000];
char grade [100000];

printf("Edit or Read file? (1 for edit, 2 or read)\n"); scanf("%s",RW);

if (RW == 1)
{
printf("Enter the name of file you wish to see:\n");
gets(file_name); //file_name = input

file = fopen(file_name,"r"); // read mode

if( file == NULL ) //If file couldn't be opened
{
perror("Error while opening the file!\n");
exit(EXIT_FAILURE);
}

printf("The contents of %s file are:\n", file_name);

while( ( character = fgetc(file) ) != EOF /*EOF = End Of File*/)
printf("%c",character); //print c (character)

fclose(file); //remove the file from RAM
} else if ( RW == 2) {

printf("Enter the name of file you wish to edit:\n");
gets(file_name); //file_name = input

file = fopen(file_name,"w"); // write mode

if( file == NULL ) //If file couldn't be opened
{
perror("Error while opening the file!\n");
exit(EXIT_FAILURE);
}

printf("Enter name: \n"); scanf("%s",name1);
printf("Enter second name if applicable: \n"); scanf("%s",name2);
printf("Enter grade: \n"); scanf("%s",grade);
fprintf(file, "%s%s%s\t%s%s%s", name1, " ", name2, "=", " ", grade);
fclose(file);
printf("File write was successful\n");
}

printf(" \n");
printf("Close window?\n"); scanf("%s",quit);
if (quit == "y")
{
printf("Bye!\n");
}
return (0);
}

最佳答案

发布的程序因为这一行而崩溃:

scanf("%s",RW);

变量 RW 声明为 int

scanf() 的调用需要一个指向 char 数组的指针。

所以代码试图将 RW 作为一个指针,并跟随该指针(它包含堆栈中 RW 位置的垃圾。那是导致崩溃的原因。

建议将语句写成:

scanf("%u",&RW);

关于c - 单独的部分可以正常工作,但是当合并时会导致崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36211425/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com