Click here to Skip to main content
11,493,948 members (65,336 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: 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;
}
"
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 29-Dec-12 6:41am
Edited 29-Dec-12 7:41am
v2
Comments
Richard MacCutchan at 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 at 29-Dec-12 12:50pm
   
Yes, sorry :-(
But the program still cracks...
Richard MacCutchan at 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 variant of these functions. They should read:
scanf_s("%s", name, maxim); and
scanf_s("%s", surname, maxim);.
MariusBV at 30-Dec-12 8:02am
   
Dosen't work :-(
Richard MacCutchan at 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.
Richard MacCutchan at 30-Dec-12 8:29am
   
I just tested it with my suggested changes and it works fine.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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");
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.
  Permalink  
Comments
Espen Harlinn at 29-Dec-12 12:11pm
   
That should do the trick :-D
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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>

[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.
  Permalink  
v2
Comments
MariusBV at 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
ProgramFOX at 29-Dec-12 12:12pm
   
I updated my answer.
MariusBV at 29-Dec-12 12:29pm
   
Thank you)
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

#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;
}
 
  Permalink  
v2

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 448
1 F-ES Sitecore 229
2 Nakul Chaudhari 225
3 OriginalGriff 225
4 CPallini 215
0 Sergey Alexandrovich Kryukov 10,237
1 OriginalGriff 8,730
2 Sascha Lefèvre 3,664
3 Maciej Los 3,392
4 Richard Deeming 2,500


Advertise | Privacy | Mobile
Web04 | 2.8.150520.1 | Last Updated 6 Jan 2013
Copyright © CodeProject, 1999-2015
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