1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
|
// File2Include.cpp : Defines the entry point for the console application.
//
#include <stdio.h>
#include <string.h>
int main(int argc, char* argv[])
{
if( argc < 3 )
{
printf( "Incorrect number of command line arguments. Expected arguments: src file, dst file\n");
return -1;
}
auto SrcFile = argv[1];
auto DstFile = argv[2];
if (strcmp(SrcFile, DstFile) == 0)
{
printf( "Source and destination files must be different\n");
return -1;
}
FILE *pSrcFile = fopen( SrcFile, "r" );
if( pSrcFile == nullptr )
{
printf( "Failed to open source file %s\n", SrcFile );
return -1;
}
FILE *pDstFile = fopen( DstFile, "w" );
if( pDstFile == nullptr )
{
printf( "Failed to open destination file %s\n", DstFile );
fclose(pSrcFile);
return -1;
}
char Buff[2048];
char SpecialChars[] = "\'\"\\";
while( !feof( pSrcFile ) )
{
auto* Line = fgets( Buff, sizeof( Buff )/sizeof(Buff[0]) , pSrcFile );
if( Line == nullptr )
break;
fputc( '\"', pDstFile );
auto* CurrChar = Line;
while( *CurrChar != 0 && *CurrChar != '\n' && *CurrChar != '\r' )
{
if( strchr( SpecialChars, *CurrChar) )
fputc( '\\', pDstFile );
fputc( *CurrChar, pDstFile );
++CurrChar;
}
fputs( "\\n\"\n", pDstFile );
}
fclose(pDstFile);
fclose(pSrcFile);
printf( "File2String: sucessfully converted %s to %s\n", SrcFile, DstFile );
return 0;
}
|