I am trying to separate the string
"CristinaRodriguezRiveraComputacion210302414RamiroSilvaPerezIndustrial217890453PatriciaDuranSanchezCivil215643525RaulColinGranadosComputacion215678342"
read from a file but when I separate and print this string, the following is not being separated correctly:
Required output:
Cristina Rodríguez Rivera Computación 210302414 //simulating that each string is inside a block of 15 bytes
I don't know what's wrong with the code, I've been trying to figure out if my logic is wrong for a while
#include <stdio.h>
#include <errno.h>
#include <stdbool.h>
#include <ctype.h>
#include <string.h>
typedef struct{
char name[15];
char father[15];
char mother[15];
char degree[15];
char id[15];
}Student;
Student al;
int main(){
FILE* ent = fopen("DatosEntrada.txt","r");
FILE* sal = fopen("longitud.txt","a");
if(ent != NULL){
char name[15];
char father[15];
char mother[15];
char degree[15];
char id[15];
fseek(ent, 0, SEEK_END); //getting file length
int longarch = ftell(ent);
rewind(ent); //go back to the start
char dinamic[longarch];
fscanf(ent,"%s",&dinamic);
int longitud = strlen(dinamic);
int contador=0,iterador=0;
for(int i=0;i<longarch;i++){
if( isupper(dinamic[i]) ){
if( islower(dinamic[i-1]) && islower(dinamic[i+1]) ){
iterator=0;
counter++;
}
if(counter== 0){ //name
iterator=0;
name[iterator] = dinamic[i];
//printf("%c",name[iterator]);
iterator++;
}else if(counter== 1){ //father
father[iterator] = dinamic[i];
//printf("%c",father[iterator] );
iterator++;
}else if(counter== 2){ //mother
mother[iterator] = dinamic[i];
//printf("%c",mother[iterator]);
iterator++;
}else if(counter== 3){ //degree
degree[iterator] = dinamic[i];
//printf("%c",degree[iterator]);
iterator++;
}
}else if( islower(dinamic[i]) ){
if(counter== 0){ //name
name[iterator] = dinamic[i];
//printf("%c",name[iterator]);
iterator++;
}else if(counter== 1){ //father
father[iterator] = dinamic[i];
//printf("%c",father[iterator]);
iterator++;
}else if(counter== 2){ //mother
mother[iterator] = dinamic[i];
//printf("%c",mother[iterator]);
iterator++;
}else if(counter== 3){ //degree
degree[iterator] = dinamic[i];
//printf("%c",degree[iterator]);
iterator++;
}
}else if( isdigit(dinamic[i]) ){
if( islower(dinamic[i-1]) && isdigit(dinamic[i+1]) ){
iterator=0;
counter++;
}else if( isupper(dinamic[i+1]) && isdigit(dinamic[i-1]) ){
id[iterator] = dinamic[i];
//printf("%c",id[iterator]);
counter=0;
printf("(%s,%s,%s,%s,%s)\n",name,father,mother,degree,id);
strcpy(al.name,name);
strcpy(al.father,father);
strcpy(al.mother,mother);
strcpy(al.degree,degree);
strcpy(al.id,id);
fwrite(&al,sizeof(Student), 1, sal);
}
if(counter== 4){ //id
id[iterator] = dinamic[i];
// printf("%c",id[iterator]);
iterator++;
}
}
}
fclose(ent);
fclose(sal);
}else{
fprintf(stdout, "ERROR: %s", strerror(errno));
}
}