Click here to Skip to main content
15,879,095 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hy,

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

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:
"

C++
#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]
Posted
Updated 29-Dec-12 6:41am
v2
Comments
Richard MacCutchan 29-Dec-12 12:43pm    
You are using some strange character to close your string, which is not recognised as a double quote, on the line fprintf (fis, "height=%d\n”,i);.
MariusBV 29-Dec-12 12:50pm    
Yes, sorry :-(
But the program still cracks...
Richard MacCutchan 30-Dec-12 3:16am    
Once again you need to read the documentation for the functions you are trying to use, rather than guessing at the parameters. You are using the addressof operator on your array names in your scanf statements, which is wrong. You also need to specify the buffer lengths when using the <code_s< code=""> variant of these functions. They should read:
<code>scanf_s("%s", name, maxim); and
scanf_s("%s", surname, maxim);.
MariusBV 30-Dec-12 8:02am    
Dosen't work :-(
Richard MacCutchan 30-Dec-12 8:25am    
What doesn't work? Please stop posting these useless comments and make an effort to do some proper diagnosis of your code. If it does not build, then use the error messages and documentation to check what you are doing wrong. If it builds but does not produce the results you expect, then check your logic and data to see what is going wrong. Learn to use the debugger to step through the code and check it at every stage to identify any problems.

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:
C++
errno_t errorCode = fopen_s(&fis, "C:\\test", "w");

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.
 
Share this answer
 
Comments
Espen Harlinn 29-Dec-12 12:11pm    
That should do the trick :-D
Hi,

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

Into this:
C++
#include "stdafx.h"
#include "malloc.h"
#include <string.h>
#include <process.h>
#include <stdio.h>


[EDIT]

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.
 
Share this answer
 
v2
Comments
MariusBV 29-Dec-12 12:08pm    
It not works :-(

I get a lot of error messages:
"1>------ Build started: Project: ConsoleApplication45, Configuration: Debug Win32 ------
1> ConsoleApplication45.cpp
1>c:\users\marius\documents\visual studio 2012\projects\consoleapplication45\consoleapplication45\consoleapplication45.cpp(16): warning C4305: 'initializing' : truncation from 'double' to 'float'
1>c:\users\marius\documents\visual studio 2012\projects\consoleapplication45\consoleapplication45\consoleapplication45.cpp(23): error C2660: 'fopen' : function does not take 3 arguments
1>c:\users\marius\documents\visual studio 2012\projects\consoleapplication45\consoleapplication45\consoleapplication45.cpp(24): error C2014: preprocessor command must start as first nonwhite space
1>c:\users\marius\documents\visual studio 2012\projects\consoleapplication45\consoleapplication45\consoleapplication45.cpp(25): error C2065: '“' : undeclared identifier
1>c:\users\marius\documents\visual studio 2012\projects\consoleapplication45\consoleapplication45\consoleapplication45.cpp(25): error C2146: syntax error : missing ')' before identifier 'fprintf'
1>c:\users\marius\documents\visual studio 2012\projects\consoleapplication45\consoleapplication45\consoleapplication45.cpp(25): error C2017: illegal escape sequence
1>c:\users\marius\documents\visual studio 2012\projects\consoleapplication45\consoleapplication45\consoleapplication45.cpp(26): error C2065: '“surname' : undeclared identifier
1>c:\users\marius\documents\visual studio 2012\projects\consoleapplication45\consoleapplication45\consoleapplication45.cpp(26): error C2017: illegal escape sequence
1>c:\users\marius\documents\visual studio 2012\projects\consoleapplication45\consoleapplication45\consoleapplication45.cpp(26): error C2065: 's' : undeclared identifier
1>c:\users\marius\documents\visual studio 2012\projects\consoleapplication45\consoleapplication45\consoleapplication45.cpp(26): error C2146: syntax error : missing ')' before identifier 'n”'
1>c:\users\marius\documents\visual studio 2012\projects\consoleapplication45\consoleapplication45\consoleapplication45.cpp(26): error C2059: syntax error : ')'
1>c:\users\marius\documents\visual studio 2012\projects\consoleapplication45\consoleapplication45\consoleapplication45.cpp(27): error C2065: '“age' : undeclared identifier
1>c:\users\marius\documents\visual studio 2012\projects\consoleapplication45\consoleapplication45\consoleapplication45.cpp(27): error C2017: illegal escape sequence
1>c:\users\marius\documents\visual studio 2012\projects\consoleapplication45\consoleapplication45\consoleapplication45.cpp(27): error C2065: 'd' : undeclared identifier
1>c:\users\marius\documents\visual studio 2012\projects\consoleapplication45\consoleapplication45\consoleapplication45.cpp(27): error C2146: syntax error : missing ')' before identifier 'n”'
1>c:\users\marius\documents\visual studio 2012\projects\consoleapplication45\consoleapplication45\consoleapplication45.cpp(27): error C2059: syntax error : ')'
1>c:\users\marius\documents\visual studio 2012\projects\consoleapplication45\consoleapplication45\consoleapplication45.cpp(28): error C2065: '“height' : undeclared identifier
1>c:\users\marius\documents\visual studio 2012\projects\consoleapplication45\consoleapplication45\consoleapplication45.cpp(28): error C2017: illegal escape sequence
1>c:\users\marius\documents\visual studio 2012\projects\consoleapplication45\consoleapplication45\consoleapplication45.cpp(28): error C3071: operator '%' can only be applied to an instance of a ref class or a value-type
1>c:\users\marius\documents\visual studio 2012\projects\consoleapplication45\consoleapplication45\consoleapplication45.cpp(28): error C2146: syntax error : missing ')' before identifier 'n”'
1>c:\users\marius\documents\visual studio 2012\projects\consoleapplication45\consoleapplication45\consoleapplication45.cpp(28): error C2059: syntax error : ')'
1>c:\users\marius\documents\visual studio 2012\projects\consoleapplication45\consoleapplicatio
Thomas Daniels 29-Dec-12 12:12pm    
I updated my answer.
MariusBV 29-Dec-12 12:29pm    
Thank you)
C++
#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;
}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900