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));
}
}
CodePudding user response:
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...
CodePudding user response:
Now that your code is working, here is a version (bypassing the "read from file" part) for educational purposes.
If you go very slowly and think about what's happening at each step, perhaps you will see how to condense code and avoid mistakes/errors that can creep in when there's just too much code to scroll through.
Good luck on your journey.
void showFlds( char flds[][15], int n ) {
printf( "****\n" );
for( int i = 0; i < n; i )
printf( "'%-15s'\n", flds[i] );
printf( "****\n" );
}
int main() {
char *in =
"CristinaRodriguezRiveraComputacion210302414"
"RamiroSilvaPerezIndustrial217890453"
"PatriciaDuranSanchezCivil215643525"
"RaulColinGranadosComputacion215678342";
const int nFlds = 5;
int fldCnt = 0;
char fldCopys[5][15];
int cCnt = 0;
putchar( '(' );
bool firstDig = true;
for( char *cp = in; *cp; cp ) {
if( cp > in && ( isupper( *cp ) || ( isdigit( *cp ) && firstDig ) ) ) {
fldCopys[ fldCnt ][ cCnt ] = '\0';
if( fldCnt < nFlds ) {
putchar( ' ' );
} else {
printf( ")\n" );
showFlds( fldCopys, fldCnt );
putchar( '(' );
fldCnt = 0;
}
cCnt = 0;
}
putchar( *cp );
fldCopys[ fldCnt ][ cCnt ] = *cp;
firstDig = !isdigit( *cp );
}
printf( ")\n" );
fldCopys[ fldCnt ][ cCnt ] = '\0';
showFlds( fldCopys, fldCnt );
return 0;
}
Output:
(Cristina Rodriguez Rivera Computacion 210302414)
****
'Cristina '
'Rodriguez '
'Rivera '
'Computacion '
'210302414 '
****
(Ramiro Silva Perez Industrial 217890453)
****
'Ramiro '
'Silva '
'Perez '
'Industrial '
'217890453 '
****
(Patricia Duran Sanchez Civil 215643525)
****
'Patricia '
'Duran '
'Sanchez '
'Civil '
'215643525 '
****
(Raul Colin Granados Computacion 215678342)
****
'Raul '
'Colin '
'Granados '
'Computacion '
'215678342 '
****