我如何在Visual C ++中使用新函数fopen_s [英] how i use the new function fopen_s in Visual C++

查看:123
本文介绍了我如何在Visual C ++中使用新函数fopen_s的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hy,



请帮我解决以下用C语言编写的程序,它似乎在Visual C ++中不起作用。



Hy,

please help me with the following program written in C which seems to not work in Visual C++.

#include "stdafx.h"
#include "string.h"
# include "process.h"
# include "malloc.h"

#define maxim 20

int _tmain(int argc, _TCHAR* argv[])
{
	FILE *fis;
	int v=20;
	float i=1.7;
	char name[maxim];
	char surname[maxim];
	printf_s("name=",name);
	scanf_s("%s",&name);
	printf_s("surname",surname);
	scanf_s("%s",&surname);
	fis=fopen_s(fis,"C:\test","w");
	fprintf (fis, "#Personal data#\n");
	fprintf (fis, "name=%s\n",name);
	fprintf (fis, "surname=%s\n",surname);
	fprintf (fis, "age=%d\n",v);
	fprintf (fis, "height=%.2f\n",i);
	fprintf (fis, "#end#");
	fclose (fis);
	
	return 0;
}





我需要更改才能使其正常工作?



真诚地,





谢谢主席先生,

这些程序现在看起来像这样:







What I need to change in order to make it work?

Sincerly,

[edit moved from comment]
Thank you Sir,
The programs looks now like that:
"

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

#define maxim 20

int _tmain(int argc, _TCHAR* argv[])
{
	FILE *fis;
	int v=20;
	int i=17;
	char name[maxim];
	char surname[maxim];
	printf_s("name=",name);
	scanf_s("%s",&name);
	printf_s("surname",surname);
	scanf_s("%s",&surname);
	errno_t errorCode =fopen_s(&fis,"C:\\test","w");
	fprintf(fis, "#Personal datas#\n");
	fprintf(fis, "name=%s\n",name);
	fprintf(fis, "surname=%s\n",surname);
	fprintf (fis, "ages""=%d\n",v);
    fprintf (fis, "height=%d\n",i);
	fprintf(fis, "#end#");
	fclose(fis);
	
	return 0;
}



但是出现以下错误:

1> ------ Build build:Project:ConsoleApplication45 ,配置:调试Win32 ------

1> ConsoleApplication45.cpp

1> c:\users\marius \documents\visual studio 2012\projects\consoleapplication45\consoleapplication45\consoleapplication45.cpp(28):error C2001:常线中的换行符

1> c:\ usersrs \ marius \documents\visual studio 2012 \projects\consoleapplication45\consoleapplication45\consoleapplication45.cpp(29):错误C2146:语法错误:在标识符''fprintf'之前缺少'')''

==========构建:0成功,1失败,0最新,0跳过了==========

如果我评论带有错误的行,程序会在名称或姓氏之后发生故障

下面我有链接错误的打印屏幕:http://s9.postimage.org/sd4oudbyn/err_C.jpg

[/ edit]

"
But with the following errors:
"1>------ Build started: Project: ConsoleApplication45, Configuration: Debug Win32 ------
1> ConsoleApplication45.cpp
1>c:\users\marius\documents\visual studio 2012\projects\consoleapplication45\consoleapplication45\consoleapplication45.cpp(28): error C2001: newline in constant
1>c:\users\marius\documents\visual studio 2012\projects\consoleapplication45\consoleapplication45\consoleapplication45.cpp(29): error C2146: syntax error : missing '')'' before identifier ''fprintf''
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped =========="
If I comment the lines with the errors, the programs craks after the name or surname
Below I have link with a print screen of the error: http://s9.postimage.org/sd4oudbyn/err_C.jpg
[/edit]

推荐答案

首先要做的是检查文档 [ ^ ]。如您所见,第一个参数应该是指向 FILE * 变量的指针,因此代码中的行应为:

First thing to do in all cases is check the documentation[^]. As you can see, the first parameter should be a pointer to a FILE* variable, so the line in your code should read:
errno_t errorCode = fopen_s(&fis, "C:\\test", "w");



请注意,该函数返回错误代码而不是 FILE * 。你应该检查这个返回零表示成功。另请注意,字符串中的反斜杠是转义字符,需要以反斜杠开头,以便将其存储在结果字符串中。


Note that the function returns an error code rather than a FILE*. you should check this returns zero to indicate success. Note also that backslash in a string is the escape character and needs to be preceded by a backslash for it to be stored in the resultant string.






改变这个:

Hi,

Change this:
#include "stdafx.h"
#include "string.h"
# include "process.h"
# include "malloc.h"



进入:


Into this:

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









我在你的代码中看到你使用一些引号,如。别用这样的引号。只能使用这个引号:



希望这会有所帮助。





I see in your code that you use some quotation marks like " and ". Don''t use quotation marks like that. Only use this quotation marks: "

Hope this helps.


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

#define maxim 20
 
int _tmain(int argc, _TCHAR* argv[])
{
	FILE *fis;
	int v=20;
	int i=17;
	char name[maxim];
	char surname[maxim];
	printf("name=");
	gets(name);
	printf_s("surname");
	gets(surname);
	fis=fopen("test.txt","w");
	fprintf(fis, "#Personal datas#\n");
	fprintf(fis, "name=%s\n",name);
	fprintf(fis, "surname=%s\n",surname);
	fprintf (fis, "ages""=%d\n",v);
    fprintf (fis, "height=%d\n",i);
	fprintf(fis, "#end#");
	fclose(fis);
	
	return 0;
}


这篇关于我如何在Visual C ++中使用新函数fopen_s的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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