Click here to Skip to main content
12,829,669 members (44,672 online)

how i use the new function fopen_s in Visual C++

MariusBV asked:

Open original thread
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;
}
"
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]
Tags: C++

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the The Code Project Open License (CPOL).



Advertise | Privacy | Mobile
Web02 | 2.8.170326.1 | Last Updated 26 Mar 2009
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100