11// File2Include.cpp : Defines the entry point for the console application.
22//
33
4- #include " stdafx.h"
5- #include " stdio.h"
6- #include " tchar.h"
4+ #include < stdio.h>
5+ #include < string.h>
76
8- int _tmain (int argc, _TCHAR * argv[])
7+ int main (int argc, char * argv[])
98{
109 if ( argc < 3 )
1110 {
@@ -14,45 +13,51 @@ int _tmain(int argc, _TCHAR* argv[])
1413 }
1514 auto SrcFile = argv[1 ];
1615 auto DstFile = argv[2 ];
17- FILE *pSrcFile = nullptr ;
18- if ( _tfopen_s ( &pSrcFile, SrcFile, _T ( " r" ) ) != 0 )
16+ if (strcmp (SrcFile, DstFile) == 0 )
1917 {
20- _tprintf ( _T ( " Failed to open source file %s \n " ), SrcFile );
18+ printf ( " Source and destination files must be different \n " );
2119 return -1 ;
2220 }
2321
24- FILE *pDstFile = nullptr ;
25- if ( _tfopen_s (&pDstFile, DstFile, _T ( " w " ) ) != 0 )
22+ FILE *pSrcFile = fopen ( SrcFile, " r " ) ;
23+ if ( pSrcFile == nullptr )
2624 {
27- _tprintf ( _T (" Failed to open destination file %s\n " ), DstFile );
25+ printf ( " Failed to open source file %s\n " , SrcFile );
26+ return -1 ;
27+ }
28+
29+ FILE *pDstFile = fopen ( DstFile, " w" );
30+ if ( pDstFile == nullptr )
31+ {
32+ printf ( " Failed to open destination file %s\n " , DstFile );
2833 fclose (pSrcFile);
2934 return -1 ;
3035 }
3136
3237
33- _TCHAR Buff[1024 ];
34- _TCHAR SpecialChars[] = _T ( " \'\"\\ " ) ;
38+ char Buff[2048 ];
39+ char SpecialChars[] = " \'\"\\ " ;
3540 while ( !feof ( pSrcFile ) )
3641 {
37- auto Line = _fgetts ( Buff, sizeof ( Buff )/sizeof (Buff[0 ]) , pSrcFile );
42+ auto Line = fgets ( Buff, sizeof ( Buff )/sizeof (Buff[0 ]) , pSrcFile );
3843 if ( Line == nullptr )
3944 break ;
40- _fputtc ( _T ( ' \" ' ) , pDstFile );
45+ fputc ( ' \" ' , pDstFile );
4146 auto CurrChar = Line;
4247 while ( CurrChar && *CurrChar != ' \n ' )
4348 {
44- if ( _tcschr ( SpecialChars, *CurrChar) )
45- _fputtc ( _T ( ' \\ ' ) , pDstFile );
46- _fputtc ( *CurrChar, pDstFile );
49+ if ( strchr ( SpecialChars, *CurrChar) )
50+ fputc ( ' \\ ' , pDstFile );
51+ fputc ( *CurrChar, pDstFile );
4752 ++CurrChar;
4853 }
49- _fputts ( _T ( " \\ n\"\n " ) , pDstFile );
54+ fputs ( " \\ n\"\n " , pDstFile );
5055 }
5156
5257 fclose (pDstFile);
5358 fclose (pSrcFile);
5459
55- _tprintf ( _T ( " File2String: sucessfully converted %s to %s\n " ) , SrcFile, DstFile );
60+ printf ( " File2String: sucessfully converted %s to %s\n " , SrcFile, DstFile );
5661
5762 return 0 ;
5863}
0 commit comments