C/Visual C - Porque se genera una archivo de mas en mi programa en C

 
Vista:
sin imagen de perfil
Val: 21
Ha mantenido su posición en C/Visual C (en relación al último mes)
Gráfica de C/Visual C

Porque se genera una archivo de mas en mi programa en C

Publicado por mefesto (8 intervenciones) el 14/07/2020 04:57:16
tengo el siguiente codigo en el que por causas que no entiendo siempre se genera un archivo de texto mas del pedido por el usuario, porque pasa esto?
Codigo :
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
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
 
typedef struct
{
	char nombre[50];
	char direccion[50];
}EMPLEADOS;
 
char* generarTexto(char *txt,int n);
void entradaDatos(EMPLEADOS *p);
 
int main()
{
	FILE* pf,*pc,*pb;
	char des = 'S';
	do
	{
		int op;
		printf("\n******************************\n");
		printf("Menu de opciones : \n");
		printf("a).Marca 1 para anadir empleado\nb).Marca 2 para generar carta.\nOpcion : ");
		scanf("%d", &op);
		if (op == 1)
		{
			EMPLEADOS *p;
			pf = fopen("datos.bin", "ab");
			if (pf == NULL)
			{
				printf("ERROR");
				return -1;
			}
			int num;
			printf("Numero de empleados a ingresar = ");
			scanf("%d", &num);
			p = (EMPLEADOS*)malloc(num * sizeof(EMPLEADOS));
			for (int i = 0; i < num; i++)
				entradaDatos(p + i);
			fwrite(p, sizeof(EMPLEADOS), num, pf);
			fclose(pf);
		}
		else if(op==2)
		{
			pb = fopen("datos.bin","rb");
			pf = fopen("C:\\Users\\harol\\Documents\\Textos\\cartas.txt","r");
			if(pf==NULL || pb==NULL)
			{
				printf("ERROR");
				return -3;
			}
			int i = 1;
			while (feof(pb)==0)
			{
				EMPLEADOS p;
				fread(&p,sizeof(EMPLEADOS),1,pb);
				pc = fopen(generarTexto("Carta_",i),"wt");
				if(pc==NULL)
				{
					printf("ERROR, no se pudo generar el archivo #%d",i);
					return -2;
				}
				fprintf(pc,"Nombre : %s\n\n",p.nombre);
				fprintf(pc,"Direccion : %s\n\n",p.direccion);
				char cad;
				while ((cad = fgetc(pf))!=EOF)
					fputc(cad, pc);
				rewind(pf);
				fclose(pc);
				i++;
			}
			fclose(pb);
			fclose(pf);
		}
		printf("\nRepetir ? (S/N): ");
		scanf(" %c", &des);
	} while (des=='S');
	return 0;
}
 
char* generarTexto(char* txt, int n)
{
	char *t = (char*)malloc(n+1);
	_itoa(n,t,10);
	t[n + 1] = '\0';
	char* text = (char*)malloc(strlen(txt) + strlen(t) +1);
	text[strlen(txt) + strlen(t) + 1] = '\0';
	strcat(text, txt);
	strcat(text,t);
	printf("%s\n",text);
	return text;
}
 
void entradaDatos(EMPLEADOS *p)
{
	printf("\nNombre empleado : ");
	scanf(" %[^\n]s", p->nombre);
	fflush(stdin);
	printf("Direccion empleado : ");
	scanf(" %[^\n]s",p->direccion);
}
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder