Writing to text files.

Hello everyone!

My name is Dirk and i'm rather stuck with an annoying problem.
My boss asked me to write him a program that will read out a card (SiS card = holds info about your social security etc etc) with an USB card reader.
He also provided me with a sample application but apperantly it's written in vc++ 6.0

The sample app reads the info from the card and prints it out in a cmd console.
Now, since i'm starting college this year I haven't had any C++ programming experience. In high school we only worked with VB.net.

I've been trying to fool around with the project and tried to make the program, instead of printing it in the command console to write it to a text file so i can write VB.NET program and work with the text file that holds all the info from the card.

But when I add the commands to write a file it's throwing me all kinds of errors indicating that it simply doesn't know what those commands mean or w/e
And when I start a new project in Visual C++ 2008 express edition and copy/paste the code from the old project it throws errors aswell. I've already tried making a simple write to text program and it worked.

Here's the piece of code i'm trying to change so it saves it to a text file instead of printing it (the dump section)

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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#include "SisPublic.h"
#include <stdio.h>
#include <winscard.h>


//=====================================================================
//
// function prototypes
//
//=====================================================================

char *GetScardErrMsg(int code);
void Dump(const Pbdf *, const Isdf *);

//=====================================================================
//
// main function
//
//=====================================================================
int main(int argc, char **argv)
{        
    long lReturn;    
	char sis[SIS_LEN];
    Pbdf pbdf;
    Isdf isdf;
    
    printf("reading SIS card ...\n");
    if (SCARD_S_SUCCESS != (lReturn = SisPublicReadCard(sis)))
    {
        printf("An error occurred: %lx\n", lReturn);
        printf("Error message: %s\n", GetScardErrMsg(lReturn));
    }
    else
    {
        //FILE *fp = fopen("c:/temp/sis.img", "wb");
        //fwrite(sis, 1, sizeof(sis), fp);
        //fclose(fp);
        SisPublicError err;
        if (ERR_OK == (err = SisPublicParse(sis, &pbdf, &isdf)))
        {            
            Dump(&pbdf, &isdf);
        }
        else
        {
            printf("error: %d\n", err); // fixme: show error code ...
        }
    }

    return 0;
}

//=====================================================================
//
// Dump
//
//=====================================================================
void Dump(const Pbdf *pPbdf, const Isdf *pIsdf)
{
    printf("\n");
    
    printf("Contents of PBDF:\n");
    printf("-----------------\n");
    printf("File Identification Number: %s\n", pPbdf->FIDN);
    printf("File Secret Key Index: %s\n", pPbdf->FSKI);
    printf("Social Security Identification Number: %s\n", pPbdf->SSIN);
    printf("Data Capture Date: %s\n", pPbdf->DCDT);
    printf("Holder's Firstname: %s\n", pPbdf->NAME);
    printf("Holder's Surname: %s\n", pPbdf->SNME);
    printf("Holder's Initial: %s\n", pPbdf->INIT);
    printf("Holder's Sex: %s\n", pPbdf->SEXE);
    printf("Holder's Birthdate: %s\n", pPbdf->BRDT);
    printf("File Certificate: %s\n", pPbdf->FCTF);
    printf("File Checksum: %s\n", pPbdf->FCSM);

    printf("\n");
    printf("Contents of ISDF:\n");
    printf("-----------------\n");
    printf("File Identification Number: %s\n", pIsdf->FIDN);
    printf("File Secret Key Index: %s\n", pIsdf->FSKI);
    printf("Social Security Identification Number: %s\n", pIsdf->SSIN);
    printf("Data Capture Date: %s\n", pIsdf->DCDT);
    printf("Card Name: %s\n", pIsdf->CNME);
    printf("Card Language: %s\n", pIsdf->CLAN);
    printf("Card Logical Number: %s\n", pIsdf->CLGN);
    printf("Card Begin Date: %s\n", pIsdf->CBDT);
    printf("Card End Date: %s\n", pIsdf->CEDT);
    printf("File Certificate: %s\n", pIsdf->FCTF);
    printf("File Checksum: %s\n", pIsdf->FCSM);
}

//=====================================================================
//
// GetScardErrMsg
//
//=====================================================================
char *GetScardErrMsg(int code)
{
	switch(code)
	{
	// Smartcard Reader interface errors
	case SCARD_E_CANCELLED:
		return "The action was canceled by an SCardCancel request.";
	case SCARD_E_CANT_DISPOSE:
		return "The system could not dispose of the media in the requested manner.";
	case SCARD_E_CARD_UNSUPPORTED:
		return "The smart card does not meet minimal requirements for support.";		
	case SCARD_E_DUPLICATE_READER:
		return "The reader driver didn't produce a unique reader name.";	
	case SCARD_E_INSUFFICIENT_BUFFER:
		return "The data buffer for returned data is too small for the returned data.";		
	case SCARD_E_INVALID_ATR:
		return "An ATR string obtained from the registry is not a valid ATR string.";		
	case SCARD_E_INVALID_HANDLE:
		return "The supplied handle was invalid.";		
	case SCARD_E_INVALID_PARAMETER:
		return "One or more of the supplied parameters could not be properly interpreted.";		
	case SCARD_E_INVALID_TARGET:
		return "Registry startup information is missing or invalid.";		
	case SCARD_E_INVALID_VALUE:
		return "One or more of the supplied parameter values could not be properly interpreted.";		
	case SCARD_E_NOT_READY:
		return "The reader or card is not ready to accept commands.";		
	case SCARD_E_NOT_TRANSACTED:
		return "An attempt was made to end a non-existent transaction.";		
	case SCARD_E_NO_MEMORY:
		return "Not enough memory available to complete this command.";		
	case SCARD_E_NO_SERVICE:
		return "The smart card resource manager is not running.";		
	case SCARD_E_NO_SMARTCARD:
		return "The operation requires a smart card, but no smart card is currently in the device.";		
	case SCARD_E_PCI_TOO_SMALL:
		return "The PCI receive buffer was too small.";		
	case SCARD_E_PROTO_MISMATCH:
		return "The requested protocols are incompatible with the protocol currently in use with the card.";		
	case SCARD_E_READER_UNAVAILABLE:
		return "The specified reader is not currently available for use.";		
	case SCARD_E_READER_UNSUPPORTED:
		return "The reader driver does not meet minimal requirements for support.";		
	case SCARD_E_SERVICE_STOPPED:
		return "The smart card resource manager has shut down.";		
	case SCARD_E_SHARING_VIOLATION:
		return "The smart card cannot be accessed because of other outstanding connections.";		
	case SCARD_E_SYSTEM_CANCELLED:
		return "The action was canceled by the system, presumably to log off or shut down.";		
	case SCARD_E_TIMEOUT:
		return "The user-specified timeout value has expired.";		
	case SCARD_E_UNKNOWN_CARD:
		return "The specified smart card name is not recognized.";		
	case SCARD_E_UNKNOWN_READER:
		return "The specified reader name is not recognized.";		
	case SCARD_F_COMM_ERROR:
		return "An internal communications error has been detected.";		
	case SCARD_F_INTERNAL_ERROR:
		return "An internal consistency check failed.";		
	case SCARD_F_UNKNOWN_ERROR:
		return "An internal error has been detected, but the source is unknown.";		
	case SCARD_F_WAITED_TOO_LONG:
		return "An internal consistency timer has expired.";		
	case SCARD_W_REMOVED_CARD:
		return "The smart card has been removed and no further communication is possible.";		
	case SCARD_W_RESET_CARD:
		return "The smart card has been reset, so any shared state information is invalid.";
	case SCARD_W_UNPOWERED_CARD:
		return "Power has been removed from the smart card and no further communication is possible.";
	case SCARD_W_UNRESPONSIVE_CARD:
		return "The smart card is not responding to a reset.";	
	case SCARD_W_UNSUPPORTED_CARD:
		return "The reader cannot communicate with the card due to ATR string configuration conflicts.";
	}

	return "Error is not documented.";
}
Last edited on
The first thign you need to get working is the compile / build of the old project without any changes - until you do this you will have no idea if the problems are due to changes you made, or a configuration issue.
You should be able to open the old project in VC++ 2008 and have it update /convert things (I'm not sure on this though), if not, you will need to make sure you create the right kind of project in VC++ 2008 to paste into.
It look like a Win32 Console app - try that (and remember to tick 'empty project' if the option appears).
Well, i've done some snooping around and when i googled that stdio.h i found out about the functions inside etc so i manged to get it to work using fprintf() function.

So now it writes the data to a txt file, and from that point on i'll be writing a vb.net program to work with that data ^^

The only thing i need to know now, is how to add specific chars to that string...

Cuz right now it only writes:

example text,example value

but i want it to save it as

"example text",example value

Cuz it's easier to work with the txt file in vb like that ^^
So in other words, how do i add the "s infront and after the string?

This is how one line looks now:

fprintf(pFile,"PBDF-Bestand identificatie nummer,%s\n", pPbdf->FIDN);


Best regards,

Dirk
Okay, solved the problem :)
Topic archived. No new replies allowed.