Lectura y escritura en lenguaje C en el Bloc de notas
Revisar según sus necesidades: #includelt; stdio.hgt;
#includelt; p>
#includelt;conio.hgt;
#includelt;string.hgt;
#define?LEN?101?//El número máximo de caracteres en una línea (LEN- 1)/2
typedef?struct?rowStr//Almacenar una fila de datos
{
char?str[LEN]; p>
struct?rowStr?*next;
}RSTR;
void?writeFile(FILE?*fp, RSTR?*rstrHead);
RSTR*?readFile (FILE?*fp, int?nRow); //Parámetro: nRow: ¿Cuántas líneas leer
RSTR?*strBA(RSTR?*rstrHeadA, RSTR?*rstrHeadB); //Integrar caracteres BA Cadena, devuelve el primer nodo de la lista enlazada integrada
void?meError(void?*p); //Error en la aplicación de memoria
int?main()
{
ARCHIVO?*fp;
char?filePath[]="c:\\list.txt";
RSTR?*rstrHeadA=NULL,* rstrHeadB=NULL, *rstrHeadBA=NULL;
if((fp=fopen(filePath, "rt "))==NULL)
{
printf ("¿No se puede?abrir?archivo?¿golpear?¿cualquier?clave?¡salir!");
salir(1);
}
fseek(fp, ?0,?SEEK_SET); //Restablece el puntero de flujo del archivo al principio
printf("----Comienza a leer la primera línea del archivo en la lista vinculada A----\n");
rstrHeadA=readFile(fp, 1);
printf("----Comience a leer las líneas restantes del archivo a vincular list B----\n");
rstrHeadB=readFile(fp, 100); //El segundo parámetro aquí debe ser mayor o igual que las líneas de texto restantes
printf("----Integrar una línea de cadenas de A en B Al final de cada línea de cadena----\n");
rstrHeadBA=strBA(rstrHeadA, rstrHeadB) ;
printf("----Eliminar el archivo original y crear uno nuevo Archivo en blanco----\n");
if(remove(filePath)== 0)
if((fp=fopen(filePath, "rt ")) ==NULL)
{
printf("¿No se puede?abrir?archivo ?golpear?cualquier?tecla?¡salir!");
salir(1) ;
}
fseek(fp,?0,?SEEK_SET);
printf("----Escribe la cadena integrada primero en Archivo----\n");
writeFile(fp, rstrHeadBA)
;
fclose(fp);
return?0;
}
RSTR?*strBA(RSTR?*rstrHeadA, RSTR ?*rstrHeadB)
{
RSTR?*rstrHeadBA=(RSTR?*)malloc(sizeof(RSTR)), *rstrTailBA=NULL, *rstrNew=NULL;
meError(rstrHeadBA);
rstrHeadBA-gt; siguiente=NULL;
mientras(rstrHeadB-gt;siguiente)
{
rstrNew=(RSTR?*)malloc(sizeof(RSTR));
meError(rstrNew);
rstrNew-gt; /p>
rstrNew-gt; siguiente=NULL;
strcat(rstrNew-gt; str, rstrHeadB-gt; siguiente-gt; strcat(rstrNew); -gt;str, rstrHeadA-gt;next-gt;str);
if(rstrHeadBA-gt;next==NULL)
rstrHeadBA-gt;next=rstrNew;
else
rstrTailBA-gt;next=rstrNuevo;
rstrTailBA=rstrNuevo;
rstrHeadB=rstrHeadB-gt;siguiente; p> p>
}
return?rstrHeadBA;
}
RSTR*?readFile(FILE?*fp, int?nRow) p>
{
int?i=0;
RSTR?*rstrHead=(RSTR?*)malloc(sizeof(RSTR)), *rstrTail=NULL, * rstrNew= NULL;
meError(rstrHead);
rstrHead-gt; next=NULL;
rstrNew=(RSTR?*)malloc(sizeof(RSTR )) ;
meError(rstrNew);
rstrNew-gt; siguiente=NULL;
rstrNew-gt; str[0]=0;
rstrNew-gt; p>
while(fscanf(fp, "[^\n]*c", rstrNew-gt; str)!=-1)
{
if(rstrHead -gt ;next==NULL)
rstrHead-gt;next=rstrNuevo;
else
rstrTail-gt;next=rstrNuevo;
rstrTail=rstrNew;
rstrNew=(RSTR?*)malloc(sizeof(RSTR));
meError(rstrNew);
rstrNew- gt; siguiente=NULL;
rstrNew-gt; str[0]=0
i
nRow--; >
if(nRow==0)
break;
}
if(i==0)//No se leen datos, elimina el espacio previo a la solicitud
rstrHead-gt; siguiente=NULL, rstrTail=NULL;
free(rstrNew);
return?rstrHead;
}
void?writeFile(FILE?*fp, RSTR?*rstrHead)//Escribe la información del producto una línea a la vez
{
printf("-- -- Comience a escribir el archivo----\n");
while(rstrHead-gt;next)
{
fprintf(fp, "s \ n", rstrHead-gt; siguiente-gt; str);
rstrHead=rstrHead-gt; siguiente;
}
}
void?meError(void?*p)//Error en la aplicación de memoria
{
if(p==NULL)
{
printf("\nExcepción: ¡Error en la aplicación de memoria! ¡Ingrese para finalizar el programa!\n");
while(getch()!='\r');
salir(0);
}
}