Yesterday they helped me to read a csv file and remove commas (THANK YOU) now they have asked me to delete the contents of this file, they told us that the content could be loaded in an array and from there to erase the data and redo the file but the truth is that I have no idea how to do this ...: /
//1) Funcion Mostrar
void fumos(){
char opbackin[100]={};
int c=0;
FILE*lista_alumnos;
lista_alumnos=fopen("lista_alumnos.csv","r");
system("cls"); //Windows
printf("Mostrar lista de alumnos del 1CV2\n\n\n\n");
if(lista_alumnos==NULL)
{
printf("Error al abrir el fichero\n");
}
else
{
while((c = fgetc(lista_alumnos)) != EOF)
{
if (c == '\n')
printf("\n");
printf("%c", ((c == ',') ? ' ' : c));
}
}
return;
}
this is the code of the function that reads the file, please if you have an idea of how to make it possible to delete content I would appreciate it.
I enclose the file in case you find it useful in this link
EDIT
A colleague has tried this to put the data in chain and compare to be able to look for a user but it does not work as it should, sometimes it only takes out the first name in list and already, in its words
"You should extract the lines from the file and save them in a string where different data are separated by commas In a second chain you will keep each data before the comma and comparing it with the string entered by the user, in case of that it does not coincide that it crosses the comma and compare with the following data after the comma, thus until finishing the line, with all the lines "
this is your code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(void)
{
int i,j,l,x;
char cad[120]={},bolet[10],name,otro[60]={},usuario[40]={};
char boleta[30][10],nombre[30][50],ap[30][60],edad[30][2],correo[30].[60],grupo[30][4];
/*Limpieza de arreglos*/
for(i=0;i<30;i++)
{
for(j=0;j<60;j++)
{
boleta[i][j]='//1) Funcion Mostrar
void fumos(){
char opbackin[100]={};
int c=0;
FILE*lista_alumnos;
lista_alumnos=fopen("lista_alumnos.csv","r");
system("cls"); //Windows
printf("Mostrar lista de alumnos del 1CV2\n\n\n\n");
if(lista_alumnos==NULL)
{
printf("Error al abrir el fichero\n");
}
else
{
while((c = fgetc(lista_alumnos)) != EOF)
{
if (c == '\n')
printf("\n");
printf("%c", ((c == ',') ? ' ' : c));
}
}
return;
}
';
nombre[i][j]='#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(void)
{
int i,j,l,x;
char cad[120]={},bolet[10],name,otro[60]={},usuario[40]={};
char boleta[30][10],nombre[30][50],ap[30][60],edad[30][2],correo[30].[60],grupo[30][4];
/*Limpieza de arreglos*/
for(i=0;i<30;i++)
{
for(j=0;j<60;j++)
{
boleta[i][j]='%pre%';
nombre[i][j]='%pre%';
ap[i][j]='%pre%';
edad[i][j]='%pre%';
correo[i][j]='%pre%';
grupo[i][j]='%pre%';
}
}
/*El usuario ingresa lo que desea buscar y lo guarda en una cadena*/
printf("Ingrese la boleta que desea buscar: ");
gets(usuario);
/*Se abre y lee el archivo*/
FILE *archivo;
archivo=fopen("archivodatos.csv","r");
if(archivo!=NULL)
{
while (fscanf(archivo,"%[^\n]",cad)!=EOF)/*Extrae línea por línea y la guarda en una cadena*/
{
l=strlen(cad);
for(i=0;i<l;i++)
{
if(cad[i]!=',')
{
otro[x]=cad[i];
x++;
if(strcmp(otro,usuario)==0)
{
puts(cad);
}
}
if(cad[i]==',')
{
x=0;
otro[x]=cad[i+1];
x++;
if(strcmp(otro,usuario)==0)
{
puts(cad);
}
if(strcmp(otro,usuario)!=0)
printf("Dato no encontrado");
}
}
}
}
}
';
ap[i][j]='%pre%';
edad[i][j]='%pre%';
correo[i][j]='%pre%';
grupo[i][j]='%pre%';
}
}
/*El usuario ingresa lo que desea buscar y lo guarda en una cadena*/
printf("Ingrese la boleta que desea buscar: ");
gets(usuario);
/*Se abre y lee el archivo*/
FILE *archivo;
archivo=fopen("archivodatos.csv","r");
if(archivo!=NULL)
{
while (fscanf(archivo,"%[^\n]",cad)!=EOF)/*Extrae línea por línea y la guarda en una cadena*/
{
l=strlen(cad);
for(i=0;i<l;i++)
{
if(cad[i]!=',')
{
otro[x]=cad[i];
x++;
if(strcmp(otro,usuario)==0)
{
puts(cad);
}
}
if(cad[i]==',')
{
x=0;
otro[x]=cad[i+1];
x++;
if(strcmp(otro,usuario)==0)
{
puts(cad);
}
if(strcmp(otro,usuario)!=0)
printf("Dato no encontrado");
}
}
}
}
}