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));
}
}
>Solution :
I tried out your program and got the extraneous data in the printout. However, when the five work fields were initialized to zeros, ensuring that valid character terminators were in place, the data appeared clean. Following, is the additional code added to ensure the strings contained the data you want.
if(ent != NULL)
{
char nombre[15];
char Paterno[15];
char Materno[15];
char carrera[15];
char matricula[15];
for (int x=0; x < 15; x++) /* Work field initialization */
{
nombre[x] = '\0';
Paterno[x] = '\0';
Materno[x] = '\0';
carrera[x] = '\0';
matricula[x] = '\0';
}
Give that a try.
