I am trying to separate the following string read from a txt:
CristinaRodriguezRiveraComputacion210302414RamiroSilvaPerezIndustrial217890453PatriciaDuranSanchezCivil215643525RaulColinGranadosComputacion215678342
but when separating it and wanting to print the code, it is omitting this:
RaulColinGranadosComputacion215678342
for some reason it is omitting only that piece of string and I would like to know what I am doing wrong in my code
#include <stdio.h>
#include <errno.h>
#include <ctype.h>
#include <string.h>
typedef struct{
char name[15];
char apPat[15];
char apMat[15];
char degree[15];
char id[15];
}Student;
Student al;
int main(){
FILE* ent = fopen("DatosEntrada.txt","r");
FILE* sal = fopen("SalidaBytes.txt","a");
if(ent != NULL){
char name[15];
char father[15];
char mother[15];
char degree[15];
char id[15];
memset(name, 0, sizeof(name));
memset(father, 0, sizeof(father));
memset(mother, 0, sizeof(mother));
memset(degree, 0, sizeof(degree));
memset(id, 0, sizeof(id));
fseek(ent, 0, SEEK_END); //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 counter=0,iterator=0;
for(int i=0;i<longitud;i++){
if( isupper(dinamic[i]) ){
if( islower(dinamic[i-1]) && islower(dinamic[i+1]) ){
counter++;
}
if(counter == 0){ //name
iterator=0;
name[iterator] = dinamic[i];
iterator++;
}else if(counter == 1){ //father
iterator=0;
father[iterator] = dinamic[i];
iterator++;
}else if(counter == 2){ //mother
iterator=0;
mother[iterator] = dinamic[i];
iterator++;
}else if(counter == 3){ //degree
iterator=0;
degree[iterator] = dinamic[i];
iterator++;
}
}else if( islower(dinamic[i]) ){
if(counter == 0){ //name
name[iterator] = dinamic[i];
iterator++;
}else if(counter == 1){ //father
father[iterator] = dinamic[i];
iterator++;
}else if(counter == 2){ //mother
mother[iterator] = dinamic[i];
iterator++;
}else if(counter == 3){ //degree
degree[iterator] = dinamic[i];
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];
counter=0;
strcpy(al.name,name);
strcpy(al.apPat,father);
strcpy(al.apMat,mother);
strcpy(al.degree,degree);
strcpy(al.id,id);
fwrite(&al,sizeof(Student), 1, sal);
memset(&al, 0, sizeof(Student));
memset(name, 0, sizeof(name));
memset(father, 0, sizeof(father));
memset(mother, 0, sizeof(mother));
memset(degree, 0, sizeof(degree));
memset(id, 0, sizeof(id));
}
if(counter == 4){ //id
id[iterator] = dinamic[i];
iterator++;
}
}
}
printf("\nCreated File\n");
fclose(ent);
fclose(sal);
}else{
fprintf(stdout, "ERROR: %s", strerror(errno));
}
}
>Solution :
Think about your loop counter…
for(int i=0;i<longitud;i++){
When you (fortunately) reach the end of the array of chars, you have buffered the last person’s info, but your loop terminates and the last person’s info is never output.
Move the "print" operation to a separate function that is called from within the loop (as you have) AND called again after the loop has terminated…