if条件内的文件操作无法正常工作 [英] File operation isn't working properly inside the if condition

查看:91
本文介绍了if条件内的文件操作无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  #include   <   stdio.h  >  
#include < string.h >
main()
{
int ch;
printf( 1 =写入\\\
2 = Read \ n
);
scanf( %d,& ch);
if (ch == 1
{
char mm [ 100 ];
FILE * fp;

fp = fopen( file.txt w +);

printf( 输入你的字符串:\ n);
得(mm); // 获取此处不起作用。

fprintf(fp,mm );

fclose(fp);

}
}





打印后程序终止输入字符串。它在if条件之外正常工作。为什么会这样?我该如何解决?我也尝试过scanf(),但是scanf没有把整个字符串用空格。

解决方案

如果重要的是要让它工作,那么试试这个...... 。

 #include< stdio.h> 
#include< string.h>
#include stdafx.h

int main( int argc,char * argv [])
{
int ch;
printf( 1 =写入\\\
2 = Read \ n
);

char chc [ 3 ];
得到(chc);
// scanf(%d,& ch);
if (chc [ 0 ] == ' 1'
{
char mm [ 100 ];
FILE * fp;

fp = fopen( file.txt w +);

printf( 输入你的字符串:);
得(mm); // 获取此处不起作用。

fprintf(fp,mm );

fclose(fp);

}
return 0 );
}



我所做的唯一改变是你如何捕获1或2(以及我如何在if语句中使用它)



我只发布这个,因为已经发布了一个你说不起作用的解决方案。我现在也要走了。稍后会看到你是怎么过的。


问题



1.给ch一个默认值。

2.在检查ch == 1之前检查scanf的返回值。

3.应该初始化变量mm。 char mm [100] = {0};

4.检查fp是否为NULL。

5.每次交互式阅读后使用fflush。

6.不要在printf中使用输出文本作为格式字符串(用户可以键入%s并导致崩溃)。



#包括< stdio.h> 
#include< string.h>

int main()
{
int ch = 0;
printf(1 = Write \\\
2 = Read \ n);
if(fscanf(stdin,%d,& ch)== 1)
{
fflush(stdin);
if(ch == 1)
{
FILE * fp = fopen(file.txt,w +);
if(fp!= NULL)
{
char mm [100] = {0};
printf(输入你的字符串:\ n);
fgets(mm,sizeof mm,stdin);
fflush(stdin);
fprintf(fp,%s,mm);
fclose(fp);
}
}
else if(ch == 2)
{
// TODO
}
}
}


#include <stdio.h>
#include <string.h>
main()
{
	int ch;
	printf("1 = Write\n2 = Read\n");
	scanf("%d",&ch);
	if(ch == 1)
	{
	 char mm[100];
	 FILE *fp;
     
     fp = fopen("file.txt","w+"); 

	 printf("Enter your string:\n");
	 gets(mm); //Gets isn't working here.

	 fprintf(fp, mm);

     fclose(fp);

	}
}



The program terminates after printing Enter your string. It was working properly outside the if condition. Why is this happening? How can I fix it? I also tried scanf() but scanf isn't taking the whole string with space.

解决方案

If the important thing is to get this working then try this instead ...

#include <stdio.h>
#include <string.h>
#include "stdafx.h"

int main(int argc, char* argv[])
{
    int ch;
    printf("1 = Write\n2 = Read\n");

    char chc[3];
    gets(chc);
    //scanf("%d",&ch);
    if(chc[0] == '1' )
    {
         char mm[100];
         FILE *fp;

         fp = fopen("file.txt","w+");

         printf("Enter your string:");
         gets(mm); //Gets isn't working here.

         fprintf(fp, mm);

         fclose(fp);

    }
    return(0);
}


The only change I've made is around how you capture the 1 or 2 (and how I use it in the if statement)

I'm only posting this as there is already a solution posted that you say does not work. Afraid I have to go now too. Will drop by later to see how you got on.


Issues

1. give ch a default value.
2. check the return value for scanf before checking ch == 1.
3. variable "mm" should be initialized eg. char mm[100] = {0};
4. check fp for NULL.
5. use fflush after every interactive read.
6. don't use a output text for format string in printf (user could type "%s" and cause crash).

#include <stdio.h>
#include <string.h>

int main()
{
	int ch = 0;
	printf("1 = Write\n2 = Read\n");
	if ( fscanf(stdin, "%d",&ch) == 1)
	{
		fflush(stdin);
		if(ch == 1)
		{
			FILE *fp = fopen("file.txt","w+"); 
			if (fp != NULL)
			{
				char mm[100] = {0};
				printf("Enter your string:\n");
				fgets(mm, sizeof mm, stdin);
				fflush(stdin);
				fprintf(fp, "%s", mm); 
				fclose(fp);
			}
		}
		else if (ch == 2)
		{
			// TODO
		}
	}
}


这篇关于if条件内的文件操作无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆