In C remove commas from a csv file and jump line

1

Hello, I have this program that reads and prints the csv file which is a list of students that goes in this format name, surname, ballot, age, email and prints the list like this:

name, surname, ballot, age, email
name, surname, ballot, age, email
name, surname, ballot, age, email

What I want is for you to print them like that without commas and with a jump

name last names ballot age email

name last names ballot age email

name last names ballot age email

this is the code

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

//1) Funcion Recuperar
void furec(){

    char opbackin[100]={};

    FILE*lista_alumnos;
    lista_alumnos=fopen("lista_alumnos.csv","r");

    system("cls"); //Windows
    //system("clear"); //Linux

    printf("Recuperar lista de alumnos del 1CV2\n\n");

    if(lista_alumnos==NULL)
    {
        printf("Error al abrir el fichero\n");
    }
    else
    {
        while(!feof(lista_alumnos))
        printf("%c",getc(lista_alumnos));
    }

    printf("\n\nMenu:\n\n1) regresar | 2) Salir (Presionar cualquier tecla)\n\n");
    printf("Terminal> ");

    scanf("%s",&opbackin);

    //Regresa al programa inicial '1' o 'recuperar'
    if(strcmp(opbackin,"regresar")==0) {
        main();
    }
    else {
        if(strcmp(opbackin,"1")==0) {
            main();
        }
    }

    return;
    }


//Programa principal
int main(void) {

    char opin[100]={};

    system("cls"); //Windows
    //system("clear"); //Linux

    //Textos de inicio
    printf("Lista de alumnos del grupo 1CV2\n\nIngresa una opcion (numero o texto):\n\n1) recuperar\n\n");
    printf("Terminal> ");

    scanf("%s",&opin);

    //Recupera informacion de alumno ingresando '1' o 'recuperar'
    if(strcmp(opin,"recuperar")==0) {
        furec();
    }
    else {
        if(strcmp(opin,"1")==0) {
            furec();
        }
    }

    return 0;
}

As you can see, just read the file, I do not know how to do it at least to remove the commas: /

    
asked by CarlosDayan 01.05.2017 в 23:20
source

1 answer

1

You just have to confirm that the current character is a comma or a line break, in the following:

while(!feof(lista_alumnos))
    printf("%c",getc(lista_alumnos));

You could change it by:

int c = 0;
while ((c = fgetc(lista_alumnos)) != EOF) { // Verifica si no es el final.
    if (c == '\n')  // Si es un salto de linea, imprime 2
        fputc('\n', stdout);
    fputc(((c == ',') ? ' ' : c), stdout); // Imprime espacio si es una coma.
}

And ready :), I have made some changes to your code, I hope you feel comfortable with them.

Here is the complete code:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(void);
void furec(void);

void clrscrn(void) {
#ifdef _WIN32  // Si es windows, usa cls, de lo contrario clear.
    system("cls");
#else
    system("clear");
#endif
}

//1) Funcion Recuperar
void furec(void) {
    char opbackin[100];
    FILE* lista_alumnos = NULL;

    lista_alumnos = fopen("lista_alumnos.csv", "r");
    clrscrn();

    printf("Recuperar lista de alumnos del 1CV2\n\n");
    if (!lista_alumnos)
        printf("Error al abrir el fichero\n");
    else
    {
        int c = 0;
        while((c = fgetc(lista_alumnos)) != EOF) {
            if (c == '\n')
                printf("\n");
            printf("%c", (c == ',') ? ' ' : c);
        }
    }

    printf("\n\nMenu:\n\n1) regresar | 2) Salir (Presionar cualquier tecla)\n\n");
    printf("Terminal> ");
    scanf("%s", opbackin);

    //Regresa al programa inicial '1' o 'recuperar'
    if(strcmp(opbackin, "regresar")==0) 
        main();
    else {
        if(strcmp(opbackin, "1") == 0) {
            main();
        }
    }
}


//Programa principal
int main(void) {
    char opin[100];
    clrscrn();

    //Textos de inicio
    printf("Lista de alumnos del grupo 1CV2\n\nIngresa una opcion (numero o texto):\n\n1) recuperar\n\n");
    printf("Terminal> ");
    scanf("%s", opin);

    //Recupera informacion de alumno ingresando '1' o 'recuperar'
    if(strcmp(opin, "recuperar") == 0) 
        furec();
    else {
        if(strcmp(opin, "1") == 0) {
            furec();
        }
    }
    return 0;
}

With the test example that you put in the question, throw me the following result:

Recuperar lista de alumnos del 1CV2

nombre apellidos boleta edad email

nombre apellidos boleta edad email

nombre apellidos boleta edad email

Menu:

1) regresar | 2) Salir (Presionar cualquier tecla)

Terminal> ^C
    
answered by 02.05.2017 / 00:33
source